Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp566415imm; Fri, 14 Sep 2018 02:59:08 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbML5l6UWd/5zwtT0lF70tbGR+DwPyOzgXMIlFhNEj14RuM9zpn9q7g7YZHfG/P74mpGmPZ X-Received: by 2002:a63:1921:: with SMTP id z33-v6mr11254893pgl.302.1536919148291; Fri, 14 Sep 2018 02:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536919148; cv=none; d=google.com; s=arc-20160816; b=gvLF7iLvW5cJGQYjVJ3FLTopImmzA/XW/2M2BjNu2TmWpDx4hGT4lhXQlXxUHiJZuq EWJ83Iz5SikJ4dD1fXwLSz3fhhpzKf4F4MNHzOoGyGmUr66rJNcnVDqIntNjEt2OTlhn psPJ2O0fc8zErxa/YmmYTIh+K+onnIdrtR/Ab1BMYmf5ClReA6sjwqznHSiwoLBWbWoE 4kAS9Pp7wpNOGH/etTIwDUvaJ5woTa50WOxkmlgzutb7Dom2U3dhLLWgBmR95KRz2Jov azMGI6nVL/IVFKQZ3rx6ccitpxHOmBRduAfw9MUxUXQbAyqZXQiawF6AhesN2WlILE8M F+5Q== 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; bh=mBKIY7ltGFEPcnkQoxokdxXsUUzg3Mh9YZOdV8lUC6U=; b=laBZVmwm67ZzghJ/1f33odPZUWRCugA4IU19GBktu3agRuPyMFtT6D9nIwhhc+Hvhy DEldWuspQtyNElUbOZ1boMTTjrdxseNXAV9HsdxrOUBhgz8E3OjAe4KiWEgJfcxP9rt2 BzGsvTI85OFsd35E2Y9IOuNIUdrPOsVbnCdRAApgTMYjLaVjm0rHFYZFgJBOeQ8k3Bnb xe4P5n7X/YDak2Oej3wQZBmcsYh4N3+tYQcAW4HYVgcQSwBDJm4g+3Ji4NG7vzFybbMR 9b+znt2u3F2NX3QzGCtIl9ApcgwADZKpZXiPAhyNm8Q9shmDl+lmrt2Xr9J1y8ppsrc1 8EQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=q6Omu+u0; 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 b18-v6si6875490pgb.612.2018.09.14.02.58.53; Fri, 14 Sep 2018 02:59:08 -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=q6Omu+u0; 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 S1728310AbeINPMY (ORCPT + 99 others); Fri, 14 Sep 2018 11:12:24 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:59378 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726911AbeINPMX (ORCPT ); Fri, 14 Sep 2018 11:12:23 -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=mBKIY7ltGFEPcnkQoxokdxXsUUzg3Mh9YZOdV8lUC6U=; b=q6Omu+u0v/0sd/sOmEeoyvQpd otGJu+0kaoBUVIURSPcVZDksnr/blh8BbnCQd23h/NG96FAU2z/uOROQaul6tzpACka4QhKFvYll+ 3pr4PUovZY1+hXg/3CDp9P2RqupgHoSGY0bd2uThXwkgqB1vpaAn3aQJ8eMJH5o7CSyVReKAFfaXB KRSpoIJ+7T5ylie8mOtxyiaLpsQdLH7bi/IIP0jchfisedMh1dxR2caL2+3C5otzuZeCpEGPGyUVR wsKUicUZFXVwigGgsr/bRyIpIcbkHwG5BmSyOZcv9ab7gKY+63z6kobOMTR6ZlGeueC6BDUJzx21J MYXuR3Vaw==; Received: from 089144198037.atnat0007.highway.a1.net ([89.144.198.37] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1g0krx-0000Y1-52; Fri, 14 Sep 2018 09:58:33 +0000 From: Christoph Hellwig To: iommu@lists.linux-foundation.org Cc: Marek Szyprowski , Robin Murphy , Paul Burton , Greg Kroah-Hartman , linux-mips@linux-mips.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] dma-mapping: support non-coherent devices in dma_common_get_sgtable Date: Fri, 14 Sep 2018 11:58:08 +0200 Message-Id: <20180914095808.22202-7-hch@lst.de> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180914095808.22202-1-hch@lst.de> References: <20180914095808.22202-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 We can use the arch_dma_coherent_to_pfn hook to provide a ->get_sgtable implementation. Note that this isn't an endorsement of this interface (which is a horrible bad idea), but it is required to move arm64 over to the generic code without a loss of functionality. Signed-off-by: Christoph Hellwig --- drivers/xen/swiotlb-xen.c | 2 +- include/linux/dma-mapping.h | 7 ++++--- kernel/dma/mapping.c | 23 ++++++++++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 470757ddddea..28819a0e61d0 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -689,7 +689,7 @@ xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, handle, size, attrs); } #endif - return dma_common_get_sgtable(dev, sgt, cpu_addr, handle, size); + return dma_common_get_sgtable(dev, sgt, cpu_addr, handle, size, attrs); } static int xen_swiotlb_mapping_error(struct device *dev, dma_addr_t dma_addr) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index c3378d4e0d57..bd81e74cca7b 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -483,8 +483,8 @@ dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0) int -dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, - void *cpu_addr, dma_addr_t dma_addr, size_t size); +dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, + dma_addr_t dma_addr, size_t size, unsigned long attrs); static inline int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, @@ -496,7 +496,8 @@ dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, if (ops->get_sgtable) return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs); - return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size); + return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size, + attrs); } #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0) diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 42fd73aca305..58dec7a92b7b 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -202,17 +202,26 @@ EXPORT_SYMBOL(dmam_release_declared_memory); * Create scatter-list for the already allocated DMA buffer. */ int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, - void *cpu_addr, dma_addr_t handle, size_t size) + void *cpu_addr, dma_addr_t dma_addr, size_t size, + unsigned long attrs) { - struct page *page = virt_to_page(cpu_addr); + struct page *page; int ret; - ret = sg_alloc_table(sgt, 1, GFP_KERNEL); - if (unlikely(ret)) - return ret; + if (!dev_is_dma_coherent(dev)) { + if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN)) + return -ENXIO; - sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); - return 0; + page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr, + dma_addr)); + } else { + page = virt_to_page(cpu_addr); + } + + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + if (!ret) + sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); + return ret; } EXPORT_SYMBOL(dma_common_get_sgtable); -- 2.18.0