iomap: Use non-raw io functions for io{read|write}XXbe

This change “iomap: Use non-raw io functions for io{read|write}XXbe” in Linux kernel is authored by Logan Gunthorpe <logang [at]> on Wed Jan 16 11:25:18 2019 -0700.

iomap: Use non-raw io functions for io{read|write}XXbe

Fix an asymmetry in the io{read|write}XXbe functions in that the
big-endian variants make use of the raw io accessors while the
little-endian variants use the regular accessors. Some architectures
implement barriers to order against both spinlocks and DMA accesses
and for these case, the big-endian variant of the API would not be

Thus, change the mmio_XXXXbe macros to use the appropriate swab() function
wrapping the regular accessor. This is similar to what was done for PIO.

When this code was originally written, barriers in the IO accessors were
not common and the accessors simply wrapped the raw functions in a
conversion to CPU endianness. Since then, barriers have been added in
some architectures and are now missing in the big endian variant of the

This also manages to silence a few sparse warnings that check
for using the correct endian types which the original code did
not annotate correctly.

Signed-off-by: Logan Gunthorpe <>
Cc: Thomas Gleixner <>
Cc: Kate Stewart <>
Cc: Philippe Ombredanne <>
Cc: Greg Kroah-Hartman <>
Cc: Arnd Bergmann <>
Signed-off-by: Greg Kroah-Hartman <>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit aecc787.

There are 8 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 lib/iomap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/iomap.c b/lib/iomap.c
index 541d926..2c293b2 100644
--- a/lib/iomap.c
+++ b/lib/iomap.c
@@ -65,8 +65,8 @@ static void bad_io_access(unsigned long port, const char *access)
 #ifndef mmio_read16be
-#define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
-#define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
+#define mmio_read16be(addr) swab16(readw(addr))
+#define mmio_read32be(addr) swab32(readl(addr))
 unsigned int ioread8(void __iomem *addr)
@@ -106,8 +106,8 @@ unsigned int ioread32be(void __iomem *addr)
 #ifndef mmio_write16be
-#define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
-#define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
+#define mmio_write16be(val,port) writew(swab16(val),port)
+#define mmio_write32be(val,port) writel(swab32(val),port)
 void iowrite8(u8 val, void __iomem *addr)

The commit for this change in Linux stable tree is aecc787 (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *