Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp489622imu; Mon, 5 Nov 2018 04:20:34 -0800 (PST) X-Google-Smtp-Source: AJdET5eo4NjyrgnsLqUjro9e5RBAYBGRfG6oKzkGvQ+N0yhazhtD+yyiP4il3OimDbAgM1Wf1iQ8 X-Received: by 2002:a17:902:1021:: with SMTP id b30-v6mr22166051pla.23.1541420434798; Mon, 05 Nov 2018 04:20:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541420434; cv=none; d=google.com; s=arc-20160816; b=YLM26JHuaFqrSNtjoACedj6vgziv5W63LgLATrHnGF17RxrUJUN77S2lFvZl617+aP 4dgfXief8kNI5bkpDdD3suGiGqa+f1Z4n35LwxSvGY0qses6SU6epoBBQowgzeJhQNff oO76j4K1rJ0ip+Yt054WKH49IEO81ZWkCbHjo1ZnnqQD21qrlHS1luDB00BOfQ7KaGN3 TkujRe+PCnBEOIiw5iNMibtifjl74hPS2DwQ6GvGpl4lK1+qpKuQNYq4JHrs+vuN43qp fKJwUwHwqbbJTIKPjgn1moB+EemaXl9lAR5UifMbmFo2odXMphtDBhAZtA7KgygBUxEQ Sp5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Swg4ga2b+JoWl4Ai2D+474TmSQL6g6gFWeOwcBB8yaE=; b=te3Ero2NO5/lQSyNE2ei13wr8BRxhfHVT8dvp3EPPruJtYG3Vc00Wfjap4PUWy2ZBQ sMmX4IKpIuNUbE4SiR4q4EgqUSLEXHFTYp1RsI1rO2VOPQBw5ykG79V5Y1CAE07P07Xu YdfcOTgZVhgTbVfOkRQNMX2Vxwt4/MkHZQofHudy2c4UkDoq2/q7PNBIOmovfa0NIUl4 kwFyIconsA/p3ejNgLcsY3R8ipulZhzkcmMB0+38aRyt+DjjzI/VUzBQkslakah2fFmz 51CWFo3LzO44DxigUpp2dDhb6jEYt3B7PMfcYS5io54Aswso18V0wFttna6ifwv2Y/MJ WAmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=UD+nTlIH; 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 p64-v6si5053914pfg.45.2018.11.05.04.20.09; Mon, 05 Nov 2018 04:20:34 -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=UD+nTlIH; 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 S1729535AbeKEVjL (ORCPT + 99 others); Mon, 5 Nov 2018 16:39:11 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:40694 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726358AbeKEVjK (ORCPT ); Mon, 5 Nov 2018 16:39:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type: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=Swg4ga2b+JoWl4Ai2D+474TmSQL6g6gFWeOwcBB8yaE=; b=UD+nTlIH71KDUFCQZpCRDN+XIh yKJmGjycIKuAQ0YVbxyNc+jz6ac7ueA3tvtI47bvNuodaOe5bqAUDBFoJy/oLGbVddeUqp9ZKeZdb Jn/IGTXa9pL7aRhr5mVpfG2NFQ7LR5oSmXFrI77XB34y9zmGxy4TDPks1KkQ/4ITnq2xeHxjkt47V uvhIzyVXt8og1deboMCa3pIqtdbS0OXL7CwJ8m7nNgjTB4Gmne1ZOrXrSp8BEVdfAlFefy1xG78na 14xhNzv1XekEQfWgfblClHGi3kYfse5kiHgo35jICgnaSOuW9ZeuJSfyhyKTJCbvXOBoagr2zOSol 8O+tNzlg==; Received: from 213-225-37-95.nat.highway.a1.net ([213.225.37.95] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJdr0-0003Hd-2J; Mon, 05 Nov 2018 12:19:38 +0000 From: Christoph Hellwig To: iommu@lists.linux-foundation.org Cc: Robin Murphy , Catalin Marinas , Will Deacon , Guo Ren , Laura Abbott , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/9] dma-direct: provide page based alloc/free helpers Date: Mon, 5 Nov 2018 13:19:23 +0100 Message-Id: <20181105121931.13481-2-hch@lst.de> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181105121931.13481-1-hch@lst.de> References: <20181105121931.13481-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Some architectures support remapping highmem into DMA coherent allocations. To use the common code for them we need variants of dma_direct_{alloc,free}_pages that do not use kernel virtual addresses. Signed-off-by: Christoph Hellwig --- include/linux/dma-direct.h | 3 +++ kernel/dma/direct.c | 32 ++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h index bd73e7a91410..5a7a3bbb912f 100644 --- a/include/linux/dma-direct.h +++ b/include/linux/dma-direct.h @@ -67,6 +67,9 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs); void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs); +struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs); +void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page); dma_addr_t dma_direct_map_page(struct device *dev, struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir, unsigned long attrs); diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 22a12ab5a5e9..680287779b0a 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -103,14 +103,13 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) min_not_zero(dev->coherent_dma_mask, dev->bus_dma_mask); } -void *dma_direct_alloc_pages(struct device *dev, size_t size, +struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) { unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; int page_order = get_order(size); struct page *page = NULL; u64 phys_mask; - void *ret; if (attrs & DMA_ATTR_NO_WARN) gfp |= __GFP_NOWARN; @@ -150,11 +149,22 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, } } + return page; +} + +void *dma_direct_alloc_pages(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) +{ + struct page *page; + void *ret; + + page = __dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs); if (!page) return NULL; + ret = page_address(page); if (force_dma_unencrypted()) { - set_memory_decrypted((unsigned long)ret, 1 << page_order); + set_memory_decrypted((unsigned long)ret, 1 << get_order(size)); *dma_handle = __phys_to_dma(dev, page_to_phys(page)); } else { *dma_handle = phys_to_dma(dev, page_to_phys(page)); @@ -163,20 +173,22 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, return ret; } -/* - * NOTE: this function must never look at the dma_addr argument, because we want - * to be able to use it as a helper for iommu implementations as well. - */ +void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page) +{ + unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; + + if (!dma_release_from_contiguous(dev, page, count)) + __free_pages(page, get_order(size)); +} + void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) { - unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; unsigned int page_order = get_order(size); if (force_dma_unencrypted()) set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order); - if (!dma_release_from_contiguous(dev, virt_to_page(cpu_addr), count)) - free_pages((unsigned long)cpu_addr, page_order); + __dma_direct_free_pages(dev, size, virt_to_page(cpu_addr)); } void *dma_direct_alloc(struct device *dev, size_t size, -- 2.19.1