Received: by 10.213.65.68 with SMTP id h4csp1418109imn; Mon, 19 Mar 2018 03:44:35 -0700 (PDT) X-Google-Smtp-Source: AG47ELv0yvk7u1dJ8MhrdXCG7HH10RTS6HzBWqd2pKXN2xRGYnQnUXoe8LE5TePbfeBdXWP7m4U6 X-Received: by 10.98.77.197 with SMTP id a188mr1803943pfb.42.1521456275032; Mon, 19 Mar 2018 03:44:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521456274; cv=none; d=google.com; s=arc-20160816; b=wOvXOdnlbjIxccCCaM+5IQK8W6LTtB94BA++TnZ41Pw/5dYTK8/VfqTNhpJAfJBXD6 THAJlScSIuK9Hn3KeUlUKl1wVX78U9+98FAx7W/nSxvF/8Q7MwjAmnLwugmD0kMpwGwA p/3bd0923DAERf6eaRrP1WSeP7BvmYHcMm6dNMGD57eqkz9yqQwutk4vKKcUSSirjaBS KMaH3eYeDHVvrkd+PvHb59zN1a6yZxAud4QCvYrInkjqoIzEIjUmF/7FKuXwOg2weI4z IELeRy+NMqYfgh7wvgLxxl96xopBvxJCnkqV0F25ZUibZC73RC/ed/MVoloo+0SNH0Kb DuGA== 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=LcrI1Di/rF/agoObn/uT0Pq0kjgrsvVbjGgcgr40Hq2VGbwLeP3iQH62DoRsZlKtlX uIE2zD+AZGNwx/hJZtNbPEIWo4vbb6pGw/Td01SmYzdXfV0KgvziaK6mhqB4KYM1yCi1 BSJwu07FFngRYEghqNu4fguz2y+b1/WrXdh2/gZKX5Upq0jixWjBWpoNYPxtcr6cGaW+ aNHfgQdoQzFXYpDTAujYyh4czq9eAwpIucxM+wYm21VhPiEfuDAYo/WYk+uePrMjWStE dSsxToKAcFvLeJOSR5Ckgw3VgfPepoLnAs2pJlFm88mX/SOJVZ/DW3fFpgy8+gQP/LrS XUJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=dZ0IC0df; 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 f1-v6si11918261plb.73.2018.03.19.03.44.21; Mon, 19 Mar 2018 03:44:34 -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=dZ0IC0df; 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 S933132AbeCSKlt (ORCPT + 99 others); Mon, 19 Mar 2018 06:41:49 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:55138 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932894AbeCSKjH (ORCPT ); Mon, 19 Mar 2018 06:39:07 -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=wbz+ZaP2Xj1/jnXwqNyauzMJXT/4ClNXlTo8rhLGQqM=; b=dZ0IC0dfEsCsF147GrdCUI/xS iX/rODxJJkUddkkH1ZeLiLRjYI5v6MageP4cgqj1xK5PHYETUlM7lG3czqFNZfx15E94yUorhR0RB jjllgNKPSDgON+MHarTswWXJlGYJ9N7FnWaLMwROWvFG1M7z0mqPrlnLx9mYXk855Ps1CUBcP2OFc UuAjGycEP6Hb6j0cs8OKeERT5MdHKwLVFrXDwTHRVkmLKmybnMaH6su7CTIxARexPzuMUhhIlDSlZ qnP6XBa+16Yaki101hgXKlE06R4TQJLaGa5srVn0FiLL/xgBEsHnZjeXXbUGWvsgVy/wK3L0bBltq KYU366udg==; 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 1exsBv-0005s2-Dh; Mon, 19 Mar 2018 10:39:00 +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/14] iommu/intel-iommu: cleanup intel_{alloc,free}_coherent Date: Mon, 19 Mar 2018 11:38:20 +0100 Message-Id: <20180319103826.12853-9-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 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