Received: by 10.223.185.116 with SMTP id b49csp2862227wrg; Mon, 5 Mar 2018 09:51:47 -0800 (PST) X-Google-Smtp-Source: AG47ELu5bIgZ11tuASYhSetiqQsRxYwE8S/CyIrpudtpI31VzsTQ7pLqgyiAAbwPVWHnm+3Q2kAk X-Received: by 2002:a17:902:ab91:: with SMTP id f17-v6mr3637346plr.114.1520272307812; Mon, 05 Mar 2018 09:51:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520272307; cv=none; d=google.com; s=arc-20160816; b=FbSI5GLDf5FSgkRzALhNqYTvEiAf769GZs5Lmn3Vk1yUXkM4fMtNXjHGE7nIrEPekp huiIPDBpNkihrj3LnZAk10BqswgynlhRm5Zf/1CsDcWhVCosfMn/3pqRJUNu3ARpiXOq zLR6AOpD097e6sTP7Sp/g+JRaf5FCflZ4wSmjMSgkQFGky63Ugrv2VwJiSBJUgHAgAyJ lMrrn7HD8/Le/zdumwC6d2Zmq79rohGa3B+/zK9MhJi19bMSgd7J4iZyHAZMyEtdOdB7 MQKpAiDFIOO5tSZ19kNXyFASqsdYorNCTi/eSgPtWBocb2nd28OyRm4OsOk7jbAXgeEZ y/7Q== 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=I86JGjvZsXQe3g4TgNhKbb5Q9E0/Mx7sJYTK56cvMZc=; b=r0qT35X8XnHRdo4FDrPc7xuKjsc8ADQgN5EvmmpFB0PixRL7Gn9Sjqw5SEZtpCWvSQ oA79eq0LOtbm4hSSMAhShEIyfg6KdATKr0nSPb1+OUUHDc/cySBhua5AupYu0fgNGJKk Tg7rolmJjqfmA30i8JQEYdBu2auAIjtgEEA89B8XpGC/401qfSEcU0oZsmgNOOC4A/85 XfldWkttcEFjEfjo2KuqsrBbyZUna/EeLoiLTEvvr2Cx6+rN0S6eNSYnQ31h5d/hRLvm xfjARAF88E2lJ3qW6iY5aRwBQ0EA+TSGnTpjSuuaJdDgMujyyHbn+e1C8sFM/QxzdDqO ghRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=OOxg+uwD; 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 f15si8569827pga.22.2018.03.05.09.51.33; Mon, 05 Mar 2018 09:51:47 -0800 (PST) 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=OOxg+uwD; 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 S1752873AbeCERs2 (ORCPT + 99 others); Mon, 5 Mar 2018 12:48:28 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:60372 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752676AbeCERrP (ORCPT ); Mon, 5 Mar 2018 12:47:15 -0500 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=I86JGjvZsXQe3g4TgNhKbb5Q9E0/Mx7sJYTK56cvMZc=; b=OOxg+uwDJ864FGPsBbX9MOiRi k0K8hFQwqgpCVlmZGNviRajDqqDFbMmsi9JsNSKSQq7CXp2pNR9qf3Ptv4NTaI+j8QF2bt6K3BSfy m0E3uAUKJPIIxZi1Urv5y0hCAE0a+r6GpskR+ezjLETGUGMsaTjauwWlv1yXhTajqwyCI1MCewlqm yuAMrSnsuctGSBT0oqplT9/J7s52GX2J/nffLnh/foyQbMjIdo7qXb3ZGHMjMYLAaeQEjmbOM3g05 OQ2tufoTtyStH1+Kcr/RYxSiyt8WNvfEdveP8tVsQqS/9kzDxVDgYPCowTc4ZJO3auFNOo1Bbrylw 8gNPRxQ0A==; Received: from [209.116.154.70] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.89 #1 (Red Hat Linux)) id 1esuCU-0002ud-6u; Mon, 05 Mar 2018 17:47:02 +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/13] iommu/amd_iommu: use dma_direct_{alloc,free} Date: Mon, 5 Mar 2018 09:46:49 -0800 Message-Id: <20180305174655.9878-8-hch@lst.de> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20180305174655.9878-1-hch@lst.de> References: <20180305174655.9878-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 --- 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