Skip to content

Commit 10d4434

Browse files
joabreuvineetgarc
authored andcommitted
ARC: io.h: Implement reads{x}()/writes{x}()
Some ARC CPU's do not support unaligned loads/stores. Currently, generic implementation of reads{b/w/l}()/writes{b/w/l}() is being used with ARC. This can lead to misfunction of some drivers as generic functions do a plain dereference of a pointer that can be unaligned. Let's use {get/put}_unaligned() helpers instead of plain dereference of pointer in order to fix. The helpers allow to get and store data from an unaligned address whilst preserving the CPU internal alignment. According to [1], the use of these helpers are costly in terms of performance so we added an initial check for a buffer already aligned so that the usage of the helpers can be avoided, when possible. [1] Documentation/unaligned-memory-access.txt Cc: Alexey Brodkin <abrodkin@synopsys.com> Cc: Joao Pinto <jpinto@synopsys.com> Cc: David Laight <David.Laight@ACULAB.COM> Tested-by: Vitor Soares <soares@synopsys.com> Signed-off-by: Jose Abreu <joabreu@synopsys.com> Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
1 parent b7cc40c commit 10d4434

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

  • arch/arc/include/asm

‎arch/arc/include/asm/io.h‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/types.h>
1313
#include <asm/byteorder.h>
1414
#include <asm/page.h>
15+
#include <asm/unaligned.h>
1516

1617
#ifdef CONFIG_ISA_ARCV2
1718
#include <asm/barrier.h>
@@ -94,6 +95,42 @@ static inline u32 __raw_readl(const volatile void __iomem *addr)
9495
return w;
9596
}
9697

98+
/*
99+
* {read,write}s{b,w,l}() repeatedly access the same IO address in
100+
* native endianness in 8-, 16-, 32-bit chunks {into,from} memory,
101+
* @count times
102+
*/
103+
#define __raw_readsx(t,f) \
104+
static inline void __raw_reads##f(const volatile void __iomem *addr, \
105+
void *ptr, unsigned int count) \
106+
{ \
107+
bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
108+
u##t *buf = ptr; \
109+
\
110+
if (!count) \
111+
return; \
112+
\
113+
/* Some ARC CPU's don't support unaligned accesses */ \
114+
if (is_aligned) { \
115+
do { \
116+
u##t x = __raw_read##f(addr); \
117+
*buf++ = x; \
118+
} while (--count); \
119+
} else { \
120+
do { \
121+
u##t x = __raw_read##f(addr); \
122+
put_unaligned(x, buf++); \
123+
} while (--count); \
124+
} \
125+
}
126+
127+
#define __raw_readsb __raw_readsb
128+
__raw_readsx(8, b)
129+
#define __raw_readsw __raw_readsw
130+
__raw_readsx(16, w)
131+
#define __raw_readsl __raw_readsl
132+
__raw_readsx(32, l)
133+
97134
#define __raw_writeb __raw_writeb
98135
static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
99136
{
@@ -126,6 +163,35 @@ static inline void __raw_writel(u32 w, volatile void __iomem *addr)
126163

127164
}
128165

166+
#define __raw_writesx(t,f) \
167+
static inline void __raw_writes##f(volatile void __iomem *addr, \
168+
const void *ptr, unsigned int count) \
169+
{ \
170+
bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
171+
const u##t *buf = ptr; \
172+
\
173+
if (!count) \
174+
return; \
175+
\
176+
/* Some ARC CPU's don't support unaligned accesses */ \
177+
if (is_aligned) { \
178+
do { \
179+
__raw_write##f(*buf++, addr); \
180+
} while (--count); \
181+
} else { \
182+
do { \
183+
__raw_write##f(get_unaligned(buf++), addr); \
184+
} while (--count); \
185+
} \
186+
}
187+
188+
#define __raw_writesb __raw_writesb
189+
__raw_writesx(8, b)
190+
#define __raw_writesw __raw_writesw
191+
__raw_writesx(16, w)
192+
#define __raw_writesl __raw_writesl
193+
__raw_writesx(32, l)
194+
129195
/*
130196
* MMIO can also get buffered/optimized in micro-arch, so barriers needed
131197
* Based on ARM model for the typical use case
@@ -141,10 +207,16 @@ static inline void __raw_writel(u32 w, volatile void __iomem *addr)
141207
#define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
142208
#define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
143209
#define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
210+
#define readsb(p,d,l) ({ __raw_readsb(p,d,l); __iormb(); })
211+
#define readsw(p,d,l) ({ __raw_readsw(p,d,l); __iormb(); })
212+
#define readsl(p,d,l) ({ __raw_readsl(p,d,l); __iormb(); })
144213

145214
#define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); })
146215
#define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); })
147216
#define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); })
217+
#define writesb(p,d,l) ({ __iowmb(); __raw_writesb(p,d,l); })
218+
#define writesw(p,d,l) ({ __iowmb(); __raw_writesw(p,d,l); })
219+
#define writesl(p,d,l) ({ __iowmb(); __raw_writesl(p,d,l); })
148220

149221
/*
150222
* Relaxed API for drivers which can handle barrier ordering themselves

0 commit comments

Comments
 (0)