All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@free-electrons.com>
To: Arnd Bergmann <arnd@arndb.de>
Cc: linux-arm-kernel@lists.infradead.org,
	David Woodhouse <dwmw2@infradead.org>,
	Brian Norris <computersforpeace@gmail.com>,
	linux-mtd@lists.infradead.org,
	Maxime Ripard <maxime.ripard@free-electrons.com>,
	stable@vger.kernel.org, linux-sunxi@googlegroups.com
Subject: Re: [PATCH v4] mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions
Date: Mon, 14 Sep 2015 11:41:13 +0200	[thread overview]
Message-ID: <20150914114113.4aa7ea03@bbrezillon> (raw)
In-Reply-To: <1472478.qiY0UY0ao8@wuerfel>

Hi Arnd,

On Mon, 14 Sep 2015 10:59:02 +0200
Arnd Bergmann <arnd@arndb.de> wrote:

> On Monday 14 September 2015 10:41:03 Boris Brezillon wrote:
> >                 /* Fill OOB data in */
> > -               if (oob_required) {
> > -                       tmp = 0xffffffff;
> > -                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
> > -                                   4);
> > -               } else {
> > -                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
> > -                                   chip->oob_poi + offset - mtd->writesize,
> > -                                   4);
> > -               }
> > +               writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
> > +                                           layout->oobfree[i].offset),
> > +                      nfc->regs + NFC_REG_USER_DATA_BASE);
> 
> This looks like you are changing the endianess of the data that gets written.
> Is that intentional?

Hm, the real goal of this patch was to avoid accessing the
NFC_REG_USER_DATA_BASE register using byte accessors (writeb()).
The first version of this series was directly copying data from the
buffer into a temporary u32 variable, thus forcing the data to be stored
in little endian (tell me if I'm wrong), and then changing endianness
using le32_to_cpu().
Brian suggested to use __raw_writel() (as you seem to suggest too), but
I was worried about the missing mem barrier in this function.
That's why I made my own macro doing the little endian to CPU conversion
manually, but still using the standard writel() accessor (which will
do the conversion in reverse order).
Maybe I should use __raw_writel() and add an explicit memory barrier.

> 
> memcpy_toio() uses the same endianess for source and destination, while writel()
> assumes that the destination is a little-endian register, and that could break
> if the kernel is built to run as big-endian. I also see that sunxi_nfc_write_buf()
> uses memcpy_toio() for writing the actual data, and you are not changing that.

AFAIU the peripheral is always in little endian, and only the CPU can
be switched to big endian, right?
Are you saying that memcpy_toio() uses writel? Because according to
this implementation [2] it uses writeb, which should be safe (accessing
the internal SRAM using byte accessors is authorized).

> 
> If all hardware can do 32-bit accesses here and the size is guaranteed to be a
> multiple of four bytes, you can probably improve performance by using a
> __raw_writel() loop there. Using __raw_writel() in general is almost always
> a bug, but here it actually makes sense. See also the powerpc implementation
> of _memcpy_toio().

AFAICT, buffer passed to ->write_bu() are not necessarily aligned on
32bits, so using writel here might require copying data in temporary
buffers :-/.

Don't hesitate to point where I'm wrong ;-).

Best Regards,

Boris

[1]https://patchwork.ozlabs.org/patch/502041/
[2]http://lxr.free-electrons.com/source/arch/arm/kernel/io.c#L56


-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

WARNING: multiple messages have this Message-ID (diff)
From: boris.brezillon@free-electrons.com (Boris Brezillon)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4] mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions
Date: Mon, 14 Sep 2015 11:41:13 +0200	[thread overview]
Message-ID: <20150914114113.4aa7ea03@bbrezillon> (raw)
In-Reply-To: <1472478.qiY0UY0ao8@wuerfel>

Hi Arnd,

On Mon, 14 Sep 2015 10:59:02 +0200
Arnd Bergmann <arnd@arndb.de> wrote:

> On Monday 14 September 2015 10:41:03 Boris Brezillon wrote:
> >                 /* Fill OOB data in */
> > -               if (oob_required) {
> > -                       tmp = 0xffffffff;
> > -                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
> > -                                   4);
> > -               } else {
> > -                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
> > -                                   chip->oob_poi + offset - mtd->writesize,
> > -                                   4);
> > -               }
> > +               writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
> > +                                           layout->oobfree[i].offset),
> > +                      nfc->regs + NFC_REG_USER_DATA_BASE);
> 
> This looks like you are changing the endianess of the data that gets written.
> Is that intentional?

Hm, the real goal of this patch was to avoid accessing the
NFC_REG_USER_DATA_BASE register using byte accessors (writeb()).
The first version of this series was directly copying data from the
buffer into a temporary u32 variable, thus forcing the data to be stored
in little endian (tell me if I'm wrong), and then changing endianness
using le32_to_cpu().
Brian suggested to use __raw_writel() (as you seem to suggest too), but
I was worried about the missing mem barrier in this function.
That's why I made my own macro doing the little endian to CPU conversion
manually, but still using the standard writel() accessor (which will
do the conversion in reverse order).
Maybe I should use __raw_writel() and add an explicit memory barrier.

> 
> memcpy_toio() uses the same endianess for source and destination, while writel()
> assumes that the destination is a little-endian register, and that could break
> if the kernel is built to run as big-endian. I also see that sunxi_nfc_write_buf()
> uses memcpy_toio() for writing the actual data, and you are not changing that.

AFAIU the peripheral is always in little endian, and only the CPU can
be switched to big endian, right?
Are you saying that memcpy_toio() uses writel? Because according to
this implementation [2] it uses writeb, which should be safe (accessing
the internal SRAM using byte accessors is authorized).

> 
> If all hardware can do 32-bit accesses here and the size is guaranteed to be a
> multiple of four bytes, you can probably improve performance by using a
> __raw_writel() loop there. Using __raw_writel() in general is almost always
> a bug, but here it actually makes sense. See also the powerpc implementation
> of _memcpy_toio().

AFAICT, buffer passed to ->write_bu() are not necessarily aligned on
32bits, so using writel here might require copying data in temporary
buffers :-/.

Don't hesitate to point where I'm wrong ;-).

Best Regards,

Boris

[1]https://patchwork.ozlabs.org/patch/502041/
[2]http://lxr.free-electrons.com/source/arch/arm/kernel/io.c#L56


-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

  reply	other threads:[~2015-09-14  9:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-14  8:41 [PATCH v4] mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions Boris Brezillon
2015-09-14  8:41 ` Boris Brezillon
2015-09-14  8:59 ` Arnd Bergmann
2015-09-14  8:59   ` Arnd Bergmann
2015-09-14  9:41   ` Boris Brezillon [this message]
2015-09-14  9:41     ` Boris Brezillon
2015-09-14 11:49     ` Arnd Bergmann
2015-09-14 11:49       ` Arnd Bergmann
2015-09-14 12:36       ` Arnd Bergmann
2015-09-14 12:36         ` Arnd Bergmann
2015-09-14 17:02 ` Brian Norris
2015-09-14 17:02   ` Brian Norris
2015-09-21 20:43   ` Brian Norris
2015-09-21 20:43     ` Brian Norris

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=20150914114113.4aa7ea03@bbrezillon \
    --to=boris.brezillon@free-electrons.com \
    --cc=arnd@arndb.de \
    --cc=computersforpeace@gmail.com \
    --cc=dwmw2@infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=linux-sunxi@googlegroups.com \
    --cc=maxime.ripard@free-electrons.com \
    --cc=stable@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.