Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp406966ybg; Tue, 9 Jun 2020 03:51:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1ocNIRM0b48/huxPdc1IK/aq4j8FxhjR6CU7loviKty0JUPr+XCynJhymavvvM4iz8BEL X-Received: by 2002:a17:906:e115:: with SMTP id gj21mr23959819ejb.528.1591699869099; Tue, 09 Jun 2020 03:51:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591699869; cv=none; d=google.com; s=arc-20160816; b=nuZxT4GGOUt5qCGNkOYYhJDkGVkyR25UR6oVV+/m6BHe40ynZ686PMac2UXfhsvyip 1+yA/ADVy1y3MEIdeS9sxlytzdsP/QMnlOzMDHB9iYnuGZ/noGwf+TolfkQtEKBVAEuM /7unON5/ZzktMaO6tZ2Q7nkUqe7ydWLvgZet7ZfegFmCOPUQuN2n1BpXLUm6XmavIXar BwYnbe+9wBvMvBpj5kBqE737lkYUPIoSZv37uKOQOnuv1IXryqj4aFGtk1KiZZD6c/5T 7bpOlCss3UjX+AOCwLwXEP+DC7cl/XnXeiVHzHtwu8aPzptVmYniZz5aa+k1oNWbKLdS 7xlw== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=o/+n7zy+l1MmIWp2cHiuRhnQMU7eD6FUWQF3Fmyqgdg=; b=ozi0IEhlL5o8z1VaNIl/IYl8DAErcV77JuF5UTiRaM9VjJQc7dZzaT/SeyL09ANSmj QIfVaxoYoF/bVqg8/JHkY6rTEJbQWxSKq66wHW5XYXPu/dax5Uoh0EwnF5YObchBaeRv 0CjcFttDY+b44ccdBRJ/oDVtydbhWh6OPVkDgWY1BPw8ru7G7Q0hGgUZFRIkZpVXq2jz OFO6+iwV5fpGAaah3WdcAAv6ag2KtO3X4IBzCVucKOR2p/9gM8oeTu6h/+Awd5bDvsll jRiVLpC9c20svVj3/nMsOxf4mE3eOaUKlQ35WyOixgKshQ5npZ4iUe1h7IfZcRU1ldEF ZLwA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dt16si14126122ejc.525.2020.06.09.03.50.46; Tue, 09 Jun 2020 03:51:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728588AbgFIKA5 (ORCPT + 99 others); Tue, 9 Jun 2020 06:00:57 -0400 Received: from foss.arm.com ([217.140.110.172]:39456 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726765AbgFIKA5 (ORCPT ); Tue, 9 Jun 2020 06:00:57 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2F73A1FB; Tue, 9 Jun 2020 03:00:56 -0700 (PDT) Received: from [10.57.11.57] (unknown [10.57.11.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 850DF3F73D; Tue, 9 Jun 2020 03:00:47 -0700 (PDT) Subject: Re: [PATCH v9 RESEND 01/13] spi: imx: add dma_sync_sg_for_device after fallback from dma To: Robin Gong , Mark Brown Cc: "mark.rutland@arm.com" , "devicetree@vger.kernel.org" , "matthias.schiffer@ew.tq-group.com" , "martin.fuzzey@flowbird.group" , "catalin.marinas@arm.com" , "s.hauer@pengutronix.de" , "will.deacon@arm.com" , "linux-kernel@vger.kernel.org" , "linux-spi@vger.kernel.org" , "vkoul@kernel.org" , "robh+dt@kernel.org" , dl-linux-imx , "festevam@gmail.com" , "u.kleine-koenig@pengutronix.de" , "dmaengine@vger.kernel.org" , "dan.j.williams@intel.com" , "shawnguo@kernel.org" , "kernel@pengutronix.de" , "linux-arm-kernel@lists.infradead.org" References: <1591485677-20533-1-git-send-email-yibin.gong@nxp.com> <1591485677-20533-2-git-send-email-yibin.gong@nxp.com> <20200608143458.GH4593@sirena.org.uk> <20200608153139.GI4593@sirena.org.uk> <59ce3620-00b9-bac1-30e1-011a29583642@arm.com> From: Robin Murphy Message-ID: Date: Tue, 9 Jun 2020 11:00:33 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-06-09 06:21, Robin Gong wrote: > On 2020/06/09 0:44 Robin Murphy wrote: >> On 2020-06-08 16:31, Mark Brown wrote: >>> On Mon, Jun 08, 2020 at 03:08:45PM +0000, Robin Gong wrote: >>> >>>>>> + if (transfer->rx_sg.sgl) { >>>>>> + struct device *rx_dev = spi->controller->dma_rx->device->dev; >>>>>> + >>>>>> + dma_sync_sg_for_device(rx_dev, transfer->rx_sg.sgl, >>>>>> + transfer->rx_sg.nents, DMA_TO_DEVICE); >>>>>> + } >>>>>> + >>> >>>>> This is confusing - why are we DMA mapping to the device after doing >>>>> a PIO transfer? >>> >>>> 'transfer->rx_sg.sgl' condition check that's the case fallback PIO >>>> after DMA transfer failed. But the spi core still think the buffer >>>> should be in 'device' while spi driver touch it by PIO(CPU), so sync it back to >> device to ensure all received data flush to DDR. >>> >>> So we sync it back to the device so that we can then do another sync >>> to CPU? TBH I'm a bit surprised that there's a requirement that we >>> explicitly undo a sync and that a redundant double sync in the same >>> direction might be an issue but I've not had a need to care so I'm >>> perfectly prepared to believe there is. >>> >>> At the very least this needs a comment. >> >> Yeah, something's off here - at the very least, syncing with DMA_TO_DEVICE on >> the Rx buffer that was mapped with DMA_FROM_DEVICE is clearly wrong. >> CONFIG_DMA_API_DEBUG should scream about that. >> >> If the device has written to the buffer at all since dma_map_sg() was called >> then you do need a dma_sync_sg_for_cpu() call before touching it from a CPU >> fallback path, but if nobody's going to touch it from that point until it's >> unmapped then there's no point syncing it again. The >> my_card_interrupt_handler() example in DMA-API_HOWTO.txt demonstrates >> this. > Thanks for you post, but sorry, that's not spi-imx case now, because the rx data in device memory is not truly updated from 'device'/DMA, but from PIO, so that dma_sync_sg_for_cpu with DMA_FROM_DEVICE can't be used, otherwise the fresh data in cache will be invalidated. > But you're right, kernel warning comes out if CONFIG_DMA_API_DEBUG enabled... Ah, I think I understand what's going on now. That's... really ugly :( Looking at the SPI core code, I think a better way to handle this would be to have your fallback path call spi_unmap_buf() directly (or perform the same actions, if exporting that to drivers is unacceptable), then make sure ->can_dma() returns false after that such that spi_unmap_msg() won't try to unmap it again. That's a lot more reasonable than trying to fake up a DMA_TO_DEVICE transfer in the middle of a DMA_FROM_DEVICE operation on the same buffer. Alternatively, is it feasible to initiate a dummy DMA request during probe, such that you can detect the failure condition and give up on the DMA channel early, and not have to deal with it during a real SPI transfer? Robin.