Received: by 10.213.65.68 with SMTP id h4csp1447584imn; Mon, 19 Mar 2018 04:34:33 -0700 (PDT) X-Google-Smtp-Source: AG47ELtV6lWHLa5w7rUHNc3XmJN2lc2bYvFRB0KFXTiVBBJZrOecfDkFm5Swv3ujbjKwj4hwAruP X-Received: by 2002:a17:902:4581:: with SMTP id n1-v6mr12010610pld.135.1521459273509; Mon, 19 Mar 2018 04:34:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521459273; cv=none; d=google.com; s=arc-20160816; b=UVk7DayVtfDE/8hnpOxO7ml0GrYF5vA6dtwccMTPdT0oMahrT/nkoiNgOEgg55FoU6 hl1raCINJV7ki9m6tzrPZH/qaxrmjlnLDsNz8wh4WoMZqbbw4HLiS7lAsnEfJ1RHTInX HnTWt6mqdDioFUQ76wPFzz03ZpgodxOX5KgqjJnv1xGf65Wkg3itEWb9bjdJeMscGchi 9E3bRXS0qryzPvHapCU5lQrxFKxDPk3JB/K8wE5fC3CUMcv0VbdY3ooNe9PJhipPv3Ha yS47i3sX+bDDRJVEy9NAlk4j+W6cBCMpll77xx9s34LKqat3H0/LWdT5Geg2UGQPDrmX gUwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ZdHAfnWdorwl16s3nPokjTfpbqIaAuRefLfg+dBdtk4=; b=o+Oo3FUOtOFZkuar75/ZbBKLVSoQXxccBzeqC/uNrDZtOkET2F3GeVZxM5nRIwGpfm suHMTJQgR4erlnOkFndreGwULONRnggWRjDG5zC8xsPUU+yeFliMYnFSdgn0ptgW7enW RxZCiJl0vyY8GIdldJq9CUyw2/PAB6xhoqhChI6EnGt3ARHyaTcS8Nu/9tsfLQ8abcul IB6dI5VgwsFxEeoHrkQmIw8us2NwsDOiKxTtlM6hcIpqv20cTpLEZCz64Wu1w3PKB0LE Azc1Pq4QMfqxHJ2ZFkBKC1r4USfoSnJXhdItvRBtZLvhxfqnvks4OjPk8wgEA+miWwDR UdbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=cWd+0CNs; 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 y40-v6si12420659pla.367.2018.03.19.04.34.19; Mon, 19 Mar 2018 04:34:33 -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=cWd+0CNs; 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 S932475AbeCSKjM (ORCPT + 99 others); Mon, 19 Mar 2018 06:39:12 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:55112 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932882AbeCSKjD (ORCPT ); Mon, 19 Mar 2018 06:39:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding: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=ZdHAfnWdorwl16s3nPokjTfpbqIaAuRefLfg+dBdtk4=; b=cWd+0CNs932qwZSFauOwWObBS WOzEvVWeQ5fhp/+GOAX0p9ga12ezlyLHtEYl1Jh//94KkmZAOV8lab8xNk4XRjQU4U7giJiVzZt4e JnCBjFqKT9WZFrIxxMLwJOmkjo/dKC6cUDT5jWmyze2z5m/n2LvGQcRWk2Ff1/a2zxPPJSwELS1AG llR5dML5nEPuPjJQkBi/ljFRDOJchHpFOJHdeXLgee2+WQALzpUMN00mosn6SJlAyvbSVwvBcHASe NnX4TUqj8jNG3KfPTGpIZFAiFlWm/tVZovxD1Hh+ISUZnmmJx4Rhrw9CU+ANkJMG+PaImSJDqcNpp LghAU3l+w==; Received: from 178.114.187.156.wireless.dyn.drei.com ([178.114.187.156] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1exsBr-0005of-5c; Mon, 19 Mar 2018 10:38:55 +0000 From: Christoph Hellwig To: x86@kernel.org Cc: Konrad Rzeszutek Wilk , Tom Lendacky , David Woodhouse , Muli Ben-Yehuda , Jon Mason , Joerg Roedel , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH 07/14] iommu/amd_iommu: use dma_direct_{alloc,free} Date: Mon, 19 Mar 2018 11:38:19 +0100 Message-Id: <20180319103826.12853-8-hch@lst.de> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20180319103826.12853-1-hch@lst.de> References: <20180319103826.12853-1-hch@lst.de> 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 cleans up the code a lot by removing duplicate logic. Signed-off-by: Christoph Hellwig Tested-by: Joerg Roedel Acked-by: Joerg Roedel --- drivers/iommu/Kconfig | 1 + drivers/iommu/amd_iommu.c | 68 +++++++++++++++-------------------------------- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index f3a21343e636..dc7c1914645d 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -107,6 +107,7 @@ config IOMMU_PGTABLES_L2 # AMD IOMMU support config AMD_IOMMU bool "AMD IOMMU support" + select DMA_DIRECT_OPS select SWIOTLB select PCI_MSI select PCI_ATS diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 0bf19423b588..83819d0cbf90 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -2600,51 +2600,32 @@ static void *alloc_coherent(struct device *dev, size_t size, unsigned long attrs) { u64 dma_mask = dev->coherent_dma_mask; - struct protection_domain *domain; - struct dma_ops_domain *dma_dom; - struct page *page; - - domain = get_domain(dev); - if (PTR_ERR(domain) == -EINVAL) { - page = alloc_pages(flag, get_order(size)); - *dma_addr = page_to_phys(page); - return page_address(page); - } else if (IS_ERR(domain)) - return NULL; + struct protection_domain *domain = get_domain(dev); + bool is_direct = false; + void *virt_addr; - dma_dom = to_dma_ops_domain(domain); - size = PAGE_ALIGN(size); - dma_mask = dev->coherent_dma_mask; - flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); - flag |= __GFP_ZERO; - - page = alloc_pages(flag | __GFP_NOWARN, get_order(size)); - if (!page) { - if (!gfpflags_allow_blocking(flag)) - return NULL; - - page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT, - get_order(size), flag); - if (!page) + if (IS_ERR(domain)) { + if (PTR_ERR(domain) != -EINVAL) return NULL; + is_direct = true; } + virt_addr = dma_direct_alloc(dev, size, dma_addr, flag, attrs); + if (!virt_addr || is_direct) + return virt_addr; + if (!dma_mask) dma_mask = *dev->dma_mask; - *dma_addr = __map_single(dev, dma_dom, page_to_phys(page), - size, DMA_BIDIRECTIONAL, dma_mask); - + *dma_addr = __map_single(dev, to_dma_ops_domain(domain), + virt_to_phys(virt_addr), PAGE_ALIGN(size), + DMA_BIDIRECTIONAL, dma_mask); if (*dma_addr == AMD_IOMMU_MAPPING_ERROR) goto out_free; - - return page_address(page); + return virt_addr; out_free: - - if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) - __free_pages(page, get_order(size)); - + dma_direct_free(dev, size, virt_addr, *dma_addr, attrs); return NULL; } @@ -2655,24 +2636,17 @@ static void free_coherent(struct device *dev, size_t size, void *virt_addr, dma_addr_t dma_addr, unsigned long attrs) { - struct protection_domain *domain; - struct dma_ops_domain *dma_dom; - struct page *page; + struct protection_domain *domain = get_domain(dev); - page = virt_to_page(virt_addr); size = PAGE_ALIGN(size); - domain = get_domain(dev); - if (IS_ERR(domain)) - goto free_mem; + if (!IS_ERR(domain)) { + struct dma_ops_domain *dma_dom = to_dma_ops_domain(domain); - dma_dom = to_dma_ops_domain(domain); - - __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL); + __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL); + } -free_mem: - if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) - __free_pages(page, get_order(size)); + dma_direct_free(dev, size, virt_addr, dma_addr, attrs); } /* -- 2.14.2