devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trent Piepho <tpiepho-cgc2CodaaHDQT0dZR+AlfA@public.gmane.org>
To: "linux-mtd-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org"
	<linux-mtd-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org>,
	"linux-I+IVW8TIWO2tmTQ+vhA3Yw@public.gmane.org"
	<linux-I+IVW8TIWO2tmTQ+vhA3Yw@public.gmane.org>,
	"broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org"
	<broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	"cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org"
	<cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org>,
	"dwmw2-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org"
	<dwmw2-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>,
	"computersforpeace-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
	<computersforpeace-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	"boris.brezillon-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org"
	<boris.brezillon-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>,
	"vigneshr-l0cyMroinI0@public.gmane.org"
	<vigneshr-l0cyMroinI0@public.gmane.org>,
	"richard-/L3Ra7n9ekc@public.gmane.org"
	<richard-/L3Ra7n9ekc@public.gmane.org>,
	"marek.vasut-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
	<marek.vasut-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: "linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"nicolas.ferre-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org"
	<nicolas.ferre-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>,
	"robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org"
	<robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	"radu.pirea-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org"
	<radu.pirea-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org>,
	"devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH 1/3] mtd: spi-nor: add optional DMA-safe bounce buffer for data transfer
Date: Tue, 26 Dec 2017 19:43:05 +0000	[thread overview]
Message-ID: <1514317385.26695.39.camel@impinj.com> (raw)
In-Reply-To: <fc2440c6f52877f28286d89691049e5cba10e6a7.1514087323.git.cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org>

On Sun, 2017-12-24 at 05:36 +0100, Cyrille Pitchen wrote:
> 
> Then the patch adds two hardware capabilities for SPI flash controllers,
> SNOR_HWCAPS_WR_BOUNCE and SNOR_HWCAPS_RD_BOUNCE.

Are there any drivers for which a bounce buffer is NOT needed when the
tx/rx buffer is not in DMA safe memory?  Maybe it would make more sense
to invert the sense of these flags, so that they indicate the driver
does not need DMA safe buffers, if that is the uncommon/non-existent
case, so that fewer drivers need to be modified to to be fixed?

> +static bool spi_nor_is_dma_safe(const void *buf)
> +{
> +	if (is_vmalloc_addr(buf))
> +		return false;
> +
> +#ifdef CONFIG_HIGHMEM
> +	if ((unsigned long)buf >= PKMAP_BASE &&
> +	    (unsigned long)buf < (PKMAP_BASE + (LAST_PKMAP * PAGE_SIZE)))
> +		return false;
> +#endif

It looks like:

(unsigned long)addr >= PKMAP_ADDR(0) &&
(unsigned long)addr < PKMAP_ADDR(LAST_PKMAP)

is the expression used in the highmem code.  But really, isn't this
begging for is_highmem_addr() in include/linux/mm.h that can always
return false when highmem is not enabled?

In order to be safe, this must be called when nor->lock is held, right?
 Otherwise it could race against two callers allocating the buffer at
the same time.  That should probably be noted in the kerneldoc comments
for this function, which should also be written.

> +static int spi_nor_get_bounce_buffer(struct spi_nor *nor,
> +				     u_char **buffer,
> +				     size_t *buffer_size)
> +{

> +
> +	*buffer = nor->bounce_buffer;
> +	*buffer_size = size;

So the buffer is returned via the parameter, and also via a field
inside nor.  Seems redundant.  Consider address could be returned via
the function return value coupled with PTR_ERR() for the error cases. 
Or not return address at all since it's available via nor-
>bounce_buffer.

>  {
>  	struct spi_nor *nor = mtd_to_spi_nor(mtd);
> +	bool use_bounce = (nor->flags & SNOR_F_USE_RD_BOUNCE) &&
> +			  !spi_nor_is_dma_safe(buf);
> +	u_char *buffer = buf;
> +	size_t buffer_size = 0;
>  	int ret;
>  
>  	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
> @@ -1268,13 +1324,23 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
>  	if (ret)
>  		return ret;
>  
> +	if (use_bounce) {
> +		ret = spi_nor_get_bounce_buffer(nor, &buffer, &buffer_size);
> +		if (ret < 0)
> +			goto read_err;
> +	}

This pattern, check if bounce is enabled, check if address is dma-
unsafe, get bounce buffer, seems to be very common.  Could it be
refactored into one helper?

u_char *buffer = spi_nor_check_bounce(nor, buf, len, &buffer_size);
if (IS_ERR(buffer)) 
    return PTR_ERR(buffer);
// buffer = nor->bounce_buffer or buf, whichever is correct
// buffer_size = len or bounce buffer size, whichever is correct

Could spi_nor_read_sfdp_dma_unsafe() also use this buffer?

  parent reply	other threads:[~2017-12-26 19:43 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-24  4:36 [PATCH 0/3] mtd: spi-nor: fix DMA-unsafe buffer issue between MTD and SPI Cyrille Pitchen
2017-12-24  4:36 ` [PATCH 1/3] mtd: spi-nor: add optional DMA-safe bounce buffer for data transfer Cyrille Pitchen
     [not found]   ` <fc2440c6f52877f28286d89691049e5cba10e6a7.1514087323.git.cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org>
2017-12-26 13:42     ` Vignesh R
     [not found]       ` <1126731d-cbf7-8fbf-34ab-8ccf1cc8241f-l0cyMroinI0@public.gmane.org>
2017-12-26 13:59         ` Cyrille Pitchen
     [not found]           ` <ae977280-28b4-d746-da4b-0f807dad609d-yU5RGvR974pGWvitb5QawA@public.gmane.org>
2018-01-07 20:07             ` Boris Brezillon
2017-12-26 19:43     ` Trent Piepho [this message]
     [not found]       ` <1514317385.26695.39.camel-cgc2CodaaHDQT0dZR+AlfA@public.gmane.org>
2017-12-28 10:39         ` Cyrille Pitchen
2017-12-28 18:54           ` Trent Piepho
     [not found]             ` <1514487276.26695.94.camel-cgc2CodaaHDQT0dZR+AlfA@public.gmane.org>
2017-12-29 10:16               ` Vignesh R
     [not found]                 ` <08be8b42-732a-bf28-40c4-f46bf9d71c80-l0cyMroinI0@public.gmane.org>
2017-12-29 18:03                   ` Trent Piepho
2018-01-02 10:00                     ` Vignesh R
2018-01-07 20:49     ` Boris Brezillon
2017-12-24  4:36 ` [PATCH 2/3] dt-bindings: mtd: atmel-quadspi: add an optional property 'dmacap, memcpy' Cyrille Pitchen
     [not found]   ` <143542c61ca674d53da4985bbabc142e8e6ebefc.1514087323.git.cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org>
2017-12-26 23:23     ` [PATCH 2/3] dt-bindings: mtd: atmel-quadspi: add an optional property 'dmacap,memcpy' Rob Herring
2017-12-27 21:40       ` Cyrille Pitchen
     [not found]         ` <0149ba05-64b1-2739-e61f-78d5170775fb-yU5RGvR974pGWvitb5QawA@public.gmane.org>
2018-01-02 10:22           ` Ludovic Desroches
     [not found]             ` <20180102102234.GG2612-fC2aZbRiGnsAAwgTK2POJVCF2YAvjlIb@public.gmane.org>
2018-01-02 19:18               ` Trent Piepho
     [not found]                 ` <1514920738.26695.171.camel-cgc2CodaaHDQT0dZR+AlfA@public.gmane.org>
2018-01-03  6:51                   ` ludovic.desroches-UWL1GkI3JZL3oGB3hsPCZA
2018-01-03 11:51           ` Mark Brown
2017-12-24  4:36 ` [PATCH 3/3] mtd: atmel-quadspi: add support of DMA memcpy() Cyrille Pitchen
2017-12-26 18:45 ` [PATCH 0/3] mtd: spi-nor: fix DMA-unsafe buffer issue between MTD and SPI Trent Piepho
2017-12-27 10:36   ` Mark Brown
2017-12-27 20:15     ` Trent Piepho
     [not found]       ` <1514405711.26695.67.camel-cgc2CodaaHDQT0dZR+AlfA@public.gmane.org>
2017-12-28  9:36         ` Cyrille Pitchen
2018-01-03 11:49         ` Mark Brown
2017-12-29  9:16   ` Vignesh R

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1514317385.26695.39.camel@impinj.com \
    --to=tpiepho-cgc2codaahdqt0dzr+alfa@public.gmane.org \
    --cc=boris.brezillon-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org \
    --cc=broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=computersforpeace-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=cyrille.pitchen-yU5RGvR974pGWvitb5QawA@public.gmane.org \
    --cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=dwmw2-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org \
    --cc=linux-I+IVW8TIWO2tmTQ+vhA3Yw@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-mtd-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=marek.vasut-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=nicolas.ferre-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org \
    --cc=radu.pirea-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org \
    --cc=richard-/L3Ra7n9ekc@public.gmane.org \
    --cc=robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=vigneshr-l0cyMroinI0@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).