Received: by 10.223.185.116 with SMTP id b49csp2859425wrg; Mon, 5 Mar 2018 09:48:46 -0800 (PST) X-Google-Smtp-Source: AG47ELueIRdMBh9ws2QySBipABltUHuAH8kYmwFqGcTjtPHsugCJyTBqZjg1hgYOQ5DU3q1z6lq5 X-Received: by 10.99.184.1 with SMTP id p1mr12692835pge.96.1520272126486; Mon, 05 Mar 2018 09:48:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520272126; cv=none; d=google.com; s=arc-20160816; b=PutbldeUkW3Zm6qAV1rYeJC7rAtlXhi451C3IFygF1rZrFod4i9mC9eVOa8mPM+1S9 GLsqHW7tFW6xBhNxwrTVWq9tyq535FpprcnaWrd8tyzvgAUUIbV1guImoeo8h7ygsmEr QesodVUpi3jQQ1FZPQuff72G1L+c+3gFdL6zXQV22IPDNBjjBORreJCBEvKsIpPi62+v wwlUWKeN3um6712kiew0MAMMOc/PKyHGi0+JNXHNapqpSvg9V4rRAPgB5VymwmFCxsRr fkO5wKGXRARzPEN1C4R/4BFwX8ifV0/yTI3iaoeQdSU/nbQBieOMq559zl2qdIS/6wH3 LmuQ== 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=wbz+ZaP2Xj1/jnXwqNyauzMJXT/4ClNXlTo8rhLGQqM=; b=gK5jy3GCzRq11Zl66lbpgd+ua8i0JTaGADs30IKa2YLUvzX9+4F6VnmaQsri3cPf39 aFpSQbdkBV5x5QwjlXpIzUGmd4LsQ7g82uLPt+58kAgp3N08CSdYTCYN4nLE5nivE6ce wvKu9WKDkY5m06sVRPHhyaWMNPxsdPvvk4yHiZeuL13D9acWF0YGn0cZnTa8cCZUPD3W vqGksZ1p80ha89EXe0F9q5TE2fYa0BVBQqzmexZJ8l/fiL6fX/APvBnwX8ZYdnH/hwIc 7CIHIuEBYR5FdN2e2OmTR3vVjEoL3D5l9yNmKidMjMzD4zYWXbtoromy6kILORulMfrL +Kew== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=tvgqMpjw; 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 k6-v6si9919812pln.103.2018.03.05.09.48.31; Mon, 05 Mar 2018 09:48:46 -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=tvgqMpjw; 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 S1752787AbeCERrY (ORCPT + 99 others); Mon, 5 Mar 2018 12:47:24 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:60414 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752677AbeCERrQ (ORCPT ); Mon, 5 Mar 2018 12:47:16 -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=wbz+ZaP2Xj1/jnXwqNyauzMJXT/4ClNXlTo8rhLGQqM=; b=tvgqMpjwWdRndVEMuie/I5TSX Ckg41i8zigpVdL9RZiOxXU3GAOReDozwqucu6HKtYqv7Wonkrqmcz82p5rdBf+3zF45kGz7eUnUB2 6ATPNgAJq41IGpb0rnIm67RepokqT9rriBL/XXKkEiyMTDOq3/jsg2z6K0e765sYwO37fxj7l4469 F9pCMvglrd+o68KTk5iWjkjsk6cpcfZQ1OQqHGBsFOYIIwRF+PZA7WJd51t9v80Var/iChjIst+79 4o/y4WTkABxLks1JiGb5Kk+dq6i9WGk4ywnr8qdKHiWdwzN7tvFCSH7zU8V0TigHNxYRXrzk6WUcO lDNKnjOlA==; Received: from [209.116.154.70] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.89 #1 (Red Hat Linux)) id 1esuCV-0002v7-2K; Mon, 05 Mar 2018 17:47:03 +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 08/13] iommu/intel-iommu: cleanup intel_{alloc,free}_coherent Date: Mon, 5 Mar 2018 09:46:50 -0800 Message-Id: <20180305174655.9878-9-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 Use the dma_direct_* helpers and cleanup the code flow. Signed-off-by: Christoph Hellwig --- drivers/iommu/Kconfig | 1 + drivers/iommu/intel-iommu.c | 62 ++++++++++++--------------------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index dc7c1914645d..df171cb85822 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -143,6 +143,7 @@ config DMAR_TABLE config INTEL_IOMMU bool "Support for Intel IOMMU using DMA Remapping Devices" depends on PCI_MSI && ACPI && (X86 || IA64_GENERIC) + select DMA_DIRECT_OPS select IOMMU_API select IOMMU_IOVA select DMAR_TABLE diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index fd899b2a12bb..24d1b1b42013 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -3708,61 +3709,30 @@ static void *intel_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs) { - struct page *page = NULL; - int order; - - size = PAGE_ALIGN(size); - order = get_order(size); - - if (!iommu_no_mapping(dev)) - flags &= ~(GFP_DMA | GFP_DMA32); - else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) { - if (dev->coherent_dma_mask < DMA_BIT_MASK(32)) - flags |= GFP_DMA; - else - flags |= GFP_DMA32; - } + void *vaddr; - if (gfpflags_allow_blocking(flags)) { - unsigned int count = size >> PAGE_SHIFT; + vaddr = dma_direct_alloc(dev, size, dma_handle, flags, attrs); + if (iommu_no_mapping(dev) || !vaddr) + return vaddr; - page = dma_alloc_from_contiguous(dev, count, order, flags); - if (page && iommu_no_mapping(dev) && - page_to_phys(page) + size > dev->coherent_dma_mask) { - dma_release_from_contiguous(dev, page, count); - page = NULL; - } - } - - if (!page) - page = alloc_pages(flags, order); - if (!page) - return NULL; - memset(page_address(page), 0, size); - - *dma_handle = __intel_map_single(dev, page_to_phys(page), size, - DMA_BIDIRECTIONAL, - dev->coherent_dma_mask); - if (*dma_handle) - return page_address(page); - if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) - __free_pages(page, order); + *dma_handle = __intel_map_single(dev, virt_to_phys(vaddr), + PAGE_ALIGN(size), DMA_BIDIRECTIONAL, + dev->coherent_dma_mask); + if (!*dma_handle) + goto out_free_pages; + return vaddr; +out_free_pages: + dma_direct_free(dev, size, vaddr, *dma_handle, attrs); return NULL; } static void intel_free_coherent(struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle, unsigned long attrs) { - int order; - struct page *page = virt_to_page(vaddr); - - size = PAGE_ALIGN(size); - order = get_order(size); - - intel_unmap(dev, dma_handle, size); - if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) - __free_pages(page, order); + if (!iommu_no_mapping(dev)) + intel_unmap(dev, dma_handle, PAGE_ALIGN(size)); + dma_direct_free(dev, size, vaddr, dma_handle, attrs); } static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist, -- 2.14.2