Received: by 10.213.65.68 with SMTP id h4csp663652imn; Fri, 23 Mar 2018 12:54:12 -0700 (PDT) X-Google-Smtp-Source: AG47ELuaoI0weVy3MjuyQ2a/hkSW9uy6rqIkwy3Ep6DPY7hRI1aysumIaevPMSKm5iiCZ9M9Jx4q X-Received: by 10.99.116.22 with SMTP id p22mr7589080pgc.265.1521834852766; Fri, 23 Mar 2018 12:54:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521834852; cv=none; d=google.com; s=arc-20160816; b=1EGzuqCnuIfMQk8V+Uoj/qrPErSTnk1nhO6XRD/cKw4XGa41zqGiRVgN/l4bEG/N0o iPIgcgJDaKcW/Vz2d1UytgawkTX7zzJWciTvPcAMVL7SVdTbcoTZlQUkyg+HZEHy8Qec 1bwMEBvSJrxb532fSc59SDG520W7Dm9wXBCVL/cN8BDy0QJYIk/OrZbIzqy4IVJ5jqGS AobLY6f2utx4pZo1iqGW6RPyL83MiL1Ry80bkUklPY+pG3zV8UtL+/GuFjxz2epll7B/ 60+a/pYbZfCwfu/TxEeC2ur2ZF8ZgDXbjtHdAvGRu5t3NEKEM3ogn5V6L44eBnXg9HGR ANlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=EdUBn0yrVjEJSDsEmkpEfFoZ8zsWZ5P2BsWcPq8A+uc=; b=pkRdjk557HGfJEhoYQCKPMMCXuRfgrXMVUXMx1YT93wuz1Km6ZR7LE/ilNlKtwEo/N b+tg7ADnGh1uuDUdgT61UvnhTRmbXg7NDrGSEHKvfkQZf6vYorzj6n5IcwuZHpIIojE1 GyYJGWL6EQ1Uw5M34BObTjQ0kzzNAbzy0qL+c6IblS/sqQeAIPFkpnwUbkLHKbt10cSW GP4ZgUOvFTihoPnaJvtV+LFlHYB3q1LeIwFS9oTPvpoAwC8e+1H3qKw7nT7znuu0mRho mJ56E4f7zL+OqHda0D26VemScpr1RLAnrn6KuDMaBua32k07QdorhHOe+KuDU6/plfAW gl7w== ARC-Authentication-Results: i=1; mx.google.com; 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 j2-v6si8929295pli.501.2018.03.23.12.53.58; Fri, 23 Mar 2018 12:54:12 -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; 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 S1752225AbeCWTvi (ORCPT + 99 others); Fri, 23 Mar 2018 15:51:38 -0400 Received: from terminus.zytor.com ([198.137.202.136]:54293 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751930AbeCWTvh (ORCPT ); Fri, 23 Mar 2018 15:51:37 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w2NJp7kU019825; Fri, 23 Mar 2018 12:51:07 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w2NJp6oN019822; Fri, 23 Mar 2018 12:51:06 -0700 Date: Fri, 23 Mar 2018 12:51:06 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Christoph Hellwig Message-ID: Cc: mingo@kernel.org, konrad.wilk@oracle.com, hch@lst.de, dwmw2@infradead.org, thomas.lendacky@amd.com, peterz@infradead.org, hpa@zytor.com, tglx@linutronix.de, torvalds@linux-foundation.org, jdmason@kudzu.us, joro@8bytes.org, mulix@mulix.org, jroedel@suse.de, linux-kernel@vger.kernel.org Reply-To: linux-kernel@vger.kernel.org, jdmason@kudzu.us, joro@8bytes.org, mulix@mulix.org, jroedel@suse.de, torvalds@linux-foundation.org, hpa@zytor.com, peterz@infradead.org, tglx@linutronix.de, dwmw2@infradead.org, thomas.lendacky@amd.com, mingo@kernel.org, konrad.wilk@oracle.com, hch@lst.de In-Reply-To: <20180319103826.12853-8-hch@lst.de> References: <20180319103826.12853-8-hch@lst.de> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/dma] iommu/amd_iommu: Use CONFIG_DMA_DIRECT_OPS=y and dma_direct_{alloc,free}() Git-Commit-ID: b468620f2a1dfdcfddfd6fa54367b8bcc1b51248 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: b468620f2a1dfdcfddfd6fa54367b8bcc1b51248 Gitweb: https://git.kernel.org/tip/b468620f2a1dfdcfddfd6fa54367b8bcc1b51248 Author: Christoph Hellwig AuthorDate: Mon, 19 Mar 2018 11:38:19 +0100 Committer: Ingo Molnar CommitDate: Tue, 20 Mar 2018 10:01:58 +0100 iommu/amd_iommu: Use CONFIG_DMA_DIRECT_OPS=y and dma_direct_{alloc,free}() This cleans up the code a lot by removing duplicate logic. Tested-by: Tom Lendacky Tested-by: Joerg Roedel Signed-off-by: Christoph Hellwig Reviewed-by: Thomas Gleixner Acked-by: Joerg Roedel Cc: David Woodhouse Cc: Joerg Roedel Cc: Jon Mason Cc: Konrad Rzeszutek Wilk Cc: Linus Torvalds Cc: Muli Ben-Yehuda Cc: Peter Zijlstra Cc: iommu@lists.linux-foundation.org Link: http://lkml.kernel.org/r/20180319103826.12853-8-hch@lst.de Signed-off-by: Ingo Molnar --- 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); } /*