From patchwork Fri Nov 21 09:35:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 41293 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 53C0423C27 for ; Fri, 21 Nov 2014 09:36:25 +0000 (UTC) Received: by mail-wi0-f198.google.com with SMTP id r20sf3058109wiv.9 for ; Fri, 21 Nov 2014 01:36:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:date:from:to:cc:subject:message-id :references:mime-version:content-type:content-disposition :in-reply-to:user-agent:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=G0RBzzVfQjmUz5Apq1V4KpPlAhp5csJNfrCrYGGjyeA=; b=laRZTvms0Tmi+T1K6y+mOrERSeFh5xshrbwUDxDAlABUxgfIUF/ELk7RX+IcrIHKPn h+krLydtR0qJv5xV5pDM30jRbmKVgaI+RVfbjCrSjDlErkhdkVPiPLbCgeVSwU+SpA7E dFjDNj2/ehVnfsdf/9+koHAa7bo0IQNdo4ToekmXr9xGxhld3O+tnnmqIaStLXs8XXvK pItwx1TMsSIU5TCBf/keT3EDWipZJxD4W4bqwQhPiXRLGxPzfDUCdh9haET1ef+/qXiw ZrNyRKB47Sa4GxSXn8De9+G9s287VbEMSgI7tnqKYnb4sDV/dbvLKD8UjdExFoKG6G48 0pHw== X-Gm-Message-State: ALoCoQkpJzHO6SHMX6wsUlijop/tMVY+d4luGmO0MkvAzP5G1hwQHTAKwO59288RpgQ7LXuccv1H X-Received: by 10.152.26.72 with SMTP id j8mr1192267lag.3.1416562584600; Fri, 21 Nov 2014 01:36:24 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.10.39 with SMTP id f7ls156663lab.75.gmail; Fri, 21 Nov 2014 01:36:24 -0800 (PST) X-Received: by 10.112.63.70 with SMTP id e6mr2032049lbs.93.1416562584330; Fri, 21 Nov 2014 01:36:24 -0800 (PST) Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com. [209.85.217.177]) by mx.google.com with ESMTPS id s4si4459131las.118.2014.11.21.01.36.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 21 Nov 2014 01:36:24 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) client-ip=209.85.217.177; Received: by mail-lb0-f177.google.com with SMTP id 10so835679lbg.8 for ; Fri, 21 Nov 2014 01:36:24 -0800 (PST) X-Received: by 10.152.6.228 with SMTP id e4mr3057702laa.71.1416562584075; Fri, 21 Nov 2014 01:36:24 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp52864lbc; Fri, 21 Nov 2014 01:36:23 -0800 (PST) X-Received: by 10.70.61.136 with SMTP id p8mr5262478pdr.98.1416562582241; Fri, 21 Nov 2014 01:36:22 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id pr4si7399291pdb.56.2014.11.21.01.36.19 for ; Fri, 21 Nov 2014 01:36:22 -0800 (PST) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932310AbaKUJf0 (ORCPT + 26 others); Fri, 21 Nov 2014 04:35:26 -0500 Received: from foss-mx-na.foss.arm.com ([217.140.108.86]:36258 "EHLO foss-mx-na.foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758064AbaKUJfU (ORCPT ); Fri, 21 Nov 2014 04:35:20 -0500 Received: from foss-smtp-na-1.foss.arm.com (unknown [10.80.61.8]) by foss-mx-na.foss.arm.com (Postfix) with ESMTP id 98263D6; Fri, 21 Nov 2014 03:35:16 -0600 (CST) Received: from collaborate-mta1.arm.com (highbank-bc01-b06.austin.arm.com [10.112.81.134]) by foss-smtp-na-1.foss.arm.com (Postfix) with ESMTP id 13F785FAD7; Fri, 21 Nov 2014 03:35:14 -0600 (CST) Received: from e104818-lin.cambridge.arm.com (e104818-lin.cambridge.arm.com [10.1.203.148]) by collaborate-mta1.arm.com (Postfix) with ESMTPS id CA62913F827; Fri, 21 Nov 2014 03:35:12 -0600 (CST) Date: Fri, 21 Nov 2014 09:35:10 +0000 From: Catalin Marinas To: Arnd Bergmann Cc: "linux-arm-kernel@lists.infradead.org" , Ding Tianhong , Will Deacon , "linux-kernel@vger.kernel.org" Subject: Re: For the problem when using swiotlb Message-ID: <20141121093509.GA19783@e104818-lin.cambridge.arm.com> References: <5469E26B.2010905@huawei.com> <1535751.CcvIi3DN4F@wuerfel> <546D58B1.60108@huawei.com> <2522857.bNQToYpBNt@wuerfel> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <2522857.bNQToYpBNt@wuerfel> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: catalin.marinas@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , On Thu, Nov 20, 2014 at 07:40:00AM +0000, Arnd Bergmann wrote: > On Thursday 20 November 2014 10:57:53 Ding Tianhong wrote: > > On 2014/11/19 16:45, Arnd Bergmann wrote: > > > On Wednesday 19 November 2014 11:17:15 Ding Tianhong wrote: > > >> On 2014/11/18 2:09, Catalin Marinas wrote: > > >>> On Mon, Nov 17, 2014 at 12:18:42PM +0000, Arnd Bergmann wrote: > > >> Thanks everyone, I think I found the way to fix it, need to enable DMA_CMA, to reserve a big memory > > >> for CMA and set coherent mask for dev, then dma_alloc and dma_mapping will not use the swiotlb until > > >> the memory out of mask or swiotlb_force is enabled. > > >> > > >> If I still understand uncorrectly, please inform me. > > > > > > Please do not use CMA to work around the problem, but fix the underlying bug > > > instead. > > > > > > The driver should call 'dma_set_mask_and_coherent()' with the appropriate > > > dma mask, and check whether that succeeded. However, the code implementing > > > dma_set_mask_and_coherent on arm64 also needs to be changed to look up > > > the dma-ranges property (see of_dma_configure()), and check if the mask > > > is possible. > > > > > The dma_pfn_offset looks only support arm32, but my platform is > > aarch64 and I check the latest kernel version, I think the dma-rangs > > still could not work for aarch64, so maybe we should add > > dma_pfn_offset for aarch64 first. > > I didn't mean the dma_pfn_offset. The problem is that the of_dma_configure > code currently doesn't look at the mask. As I explained in my reply to > Catalin, it should set the mask to the size of the dma-ranges if that is > 32-bit or smaller, and dma_set_mask should look at the same dma-ranges > property to decide what to set the mask to when a driver asks for a > mask larger than 64-bit. But this wouldn't help Ding's case, here the driver needs to set the wider DMA mask. Anyway, back to your point, to make sure I understand what you meant (I can send a proper patch with log afterwards): --- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 1e0e4671dd25..d6a4b4619174 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -120,6 +120,9 @@ config HAVE_GENERIC_RCU_GUP config ARCH_DMA_ADDR_T_64BIT def_bool y +config ARCH_HAS_DMA_SET_COHERENT_MASK + def_bool y + config NEED_DMA_MAP_STATE def_bool y diff --git a/arch/arm64/include/asm/dma-mapping.h b/arch/arm64/include/asm/dma-mapping.h index adeae3f6f0fc..92dcd251e549 100644 --- a/arch/arm64/include/asm/dma-mapping.h +++ b/arch/arm64/include/asm/dma-mapping.h @@ -18,6 +18,7 @@ #ifdef __KERNEL__ +#include #include #include @@ -88,11 +89,24 @@ static inline int dma_set_mask(struct device *dev, u64 mask) { if (!dev->dma_mask || !dma_supported(dev, mask)) return -EIO; + /* if asking for bigger dma mask, limit it to the bus dma ranges */ + if (mask > *dev->dma_mask) + mask &= of_dma_get_range_mask(dev); *dev->dma_mask = mask; return 0; } +static inline int dma_set_coherent_mask(struct device *dev, u64 mask) +{ + if (!dma_supported(dev, mask)) + return -EIO; + if (mask > dev->coherent_dma_mask) + mask &= of_dma_get_range_mask(dev); + dev->coherent_dma_mask = mask; + return 0; +} + static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) { if (!dev->dma_mask) diff --git a/drivers/of/address.c b/drivers/of/address.c index afdb78299f61..89c04abdf9bb 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -979,6 +980,19 @@ out: } EXPORT_SYMBOL_GPL(of_dma_get_range); +u64 of_dma_get_range_mask(struct device *dev) +{ + u64 dma_addr, paddr, size; + + /* no dma mask limiting if no of_node or no dma-ranges property */ + if (!dev->of_node || + of_dma_get_range(dev->of_node, &dma_addr, &paddr, &size) < 0) + return DMA_BIT_MASK(64); + + return DMA_BIT_MASK(ilog2(size)); +} +EXPORT_SYMBOL_GPL(of_dma_get_range_mask); + /** * of_dma_is_coherent - Check if device is coherent * @np: device node diff --git a/drivers/of/platform.c b/drivers/of/platform.c index 3b64d0bf5bba..50d1ac4739e6 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -200,6 +200,10 @@ static void of_dma_configure(struct device *dev) /* DMA ranges found. Calculate and set dma_pfn_offset */ dev->dma_pfn_offset = PFN_DOWN(paddr - dma_addr); dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", dev->dma_pfn_offset); + + /* limit the coherent_dma_mask to the dma-ranges size property */ + if (size < (1ULL << 32)) + dev->coherent_dma_mask = DMA_BIT_MASK(ilog2(size)); } /** diff --git a/include/linux/of_address.h b/include/linux/of_address.h index 8cb14eb393d6..fffb1a49a1a7 100644 --- a/include/linux/of_address.h +++ b/include/linux/of_address.h @@ -55,6 +55,8 @@ extern struct of_pci_range *of_pci_range_parser_one( struct of_pci_range *range); extern int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size); +extern u64 of_dma_get_range_mask(struct device *dev); + extern bool of_dma_is_coherent(struct device_node *np); #else /* CONFIG_OF_ADDRESS */ static inline struct device_node *of_find_matching_node_by_address(