temp: SR: IO_ADDRESS conversion
[linux-ginger.git] / arch / sh / include / asm / unaligned-sh4a.h
blob9f4dd252c9813e985ebbd5f8cc052256c10b6633
1 #ifndef __ASM_SH_UNALIGNED_SH4A_H
2 #define __ASM_SH_UNALIGNED_SH4A_H
4 /*
5 * SH-4A has support for unaligned 32-bit loads, and 32-bit loads only.
6 * Support for 64-bit accesses are done through shifting and masking
7 * relative to the endianness. Unaligned stores are not supported by the
8 * instruction encoding, so these continue to use the packed
9 * struct.
11 * The same note as with the movli.l/movco.l pair applies here, as long
12 * as the load is gauranteed to be inlined, nothing else will hook in to
13 * r0 and we get the return value for free.
15 * NOTE: Due to the fact we require r0 encoding, care should be taken to
16 * avoid mixing these heavily with other r0 consumers, such as the atomic
17 * ops. Failure to adhere to this can result in the compiler running out
18 * of spill registers and blowing up when building at low optimization
19 * levels. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34777.
21 #include <linux/types.h>
22 #include <asm/byteorder.h>
24 static __always_inline u32 __get_unaligned_cpu32(const u8 *p)
26 unsigned long unaligned;
28 __asm__ __volatile__ (
29 "movua.l @%1, %0\n\t"
30 : "=z" (unaligned)
31 : "r" (p)
34 return unaligned;
37 struct __una_u16 { u16 x __attribute__((packed)); };
38 struct __una_u32 { u32 x __attribute__((packed)); };
39 struct __una_u64 { u64 x __attribute__((packed)); };
41 static inline u16 __get_unaligned_cpu16(const u8 *p)
43 #ifdef __LITTLE_ENDIAN
44 return p[0] | p[1] << 8;
45 #else
46 return p[0] << 8 | p[1];
47 #endif
51 * Even though movua.l supports auto-increment on the read side, it can
52 * only store to r0 due to instruction encoding constraints, so just let
53 * the compiler sort it out on its own.
55 static inline u64 __get_unaligned_cpu64(const u8 *p)
57 #ifdef __LITTLE_ENDIAN
58 return (u64)__get_unaligned_cpu32(p + 4) << 32 |
59 __get_unaligned_cpu32(p);
60 #else
61 return (u64)__get_unaligned_cpu32(p) << 32 |
62 __get_unaligned_cpu32(p + 4);
63 #endif
66 static inline u16 get_unaligned_le16(const void *p)
68 return le16_to_cpu(__get_unaligned_cpu16(p));
71 static inline u32 get_unaligned_le32(const void *p)
73 return le32_to_cpu(__get_unaligned_cpu32(p));
76 static inline u64 get_unaligned_le64(const void *p)
78 return le64_to_cpu(__get_unaligned_cpu64(p));
81 static inline u16 get_unaligned_be16(const void *p)
83 return be16_to_cpu(__get_unaligned_cpu16(p));
86 static inline u32 get_unaligned_be32(const void *p)
88 return be32_to_cpu(__get_unaligned_cpu32(p));
91 static inline u64 get_unaligned_be64(const void *p)
93 return be64_to_cpu(__get_unaligned_cpu64(p));
96 static inline void __put_le16_noalign(u8 *p, u16 val)
98 *p++ = val;
99 *p++ = val >> 8;
102 static inline void __put_le32_noalign(u8 *p, u32 val)
104 __put_le16_noalign(p, val);
105 __put_le16_noalign(p + 2, val >> 16);
108 static inline void __put_le64_noalign(u8 *p, u64 val)
110 __put_le32_noalign(p, val);
111 __put_le32_noalign(p + 4, val >> 32);
114 static inline void __put_be16_noalign(u8 *p, u16 val)
116 *p++ = val >> 8;
117 *p++ = val;
120 static inline void __put_be32_noalign(u8 *p, u32 val)
122 __put_be16_noalign(p, val >> 16);
123 __put_be16_noalign(p + 2, val);
126 static inline void __put_be64_noalign(u8 *p, u64 val)
128 __put_be32_noalign(p, val >> 32);
129 __put_be32_noalign(p + 4, val);
132 static inline void put_unaligned_le16(u16 val, void *p)
134 #ifdef __LITTLE_ENDIAN
135 ((struct __una_u16 *)p)->x = val;
136 #else
137 __put_le16_noalign(p, val);
138 #endif
141 static inline void put_unaligned_le32(u32 val, void *p)
143 #ifdef __LITTLE_ENDIAN
144 ((struct __una_u32 *)p)->x = val;
145 #else
146 __put_le32_noalign(p, val);
147 #endif
150 static inline void put_unaligned_le64(u64 val, void *p)
152 #ifdef __LITTLE_ENDIAN
153 ((struct __una_u64 *)p)->x = val;
154 #else
155 __put_le64_noalign(p, val);
156 #endif
159 static inline void put_unaligned_be16(u16 val, void *p)
161 #ifdef __BIG_ENDIAN
162 ((struct __una_u16 *)p)->x = val;
163 #else
164 __put_be16_noalign(p, val);
165 #endif
168 static inline void put_unaligned_be32(u32 val, void *p)
170 #ifdef __BIG_ENDIAN
171 ((struct __una_u32 *)p)->x = val;
172 #else
173 __put_be32_noalign(p, val);
174 #endif
177 static inline void put_unaligned_be64(u64 val, void *p)
179 #ifdef __BIG_ENDIAN
180 ((struct __una_u64 *)p)->x = val;
181 #else
182 __put_be64_noalign(p, val);
183 #endif
187 * Cause a link-time error if we try an unaligned access other than
188 * 1,2,4 or 8 bytes long
190 extern void __bad_unaligned_access_size(void);
192 #define __get_unaligned_le(ptr) ((__force typeof(*(ptr)))({ \
193 __builtin_choose_expr(sizeof(*(ptr)) == 1, *(ptr), \
194 __builtin_choose_expr(sizeof(*(ptr)) == 2, get_unaligned_le16((ptr)), \
195 __builtin_choose_expr(sizeof(*(ptr)) == 4, get_unaligned_le32((ptr)), \
196 __builtin_choose_expr(sizeof(*(ptr)) == 8, get_unaligned_le64((ptr)), \
197 __bad_unaligned_access_size())))); \
200 #define __get_unaligned_be(ptr) ((__force typeof(*(ptr)))({ \
201 __builtin_choose_expr(sizeof(*(ptr)) == 1, *(ptr), \
202 __builtin_choose_expr(sizeof(*(ptr)) == 2, get_unaligned_be16((ptr)), \
203 __builtin_choose_expr(sizeof(*(ptr)) == 4, get_unaligned_be32((ptr)), \
204 __builtin_choose_expr(sizeof(*(ptr)) == 8, get_unaligned_be64((ptr)), \
205 __bad_unaligned_access_size())))); \
208 #define __put_unaligned_le(val, ptr) ({ \
209 void *__gu_p = (ptr); \
210 switch (sizeof(*(ptr))) { \
211 case 1: \
212 *(u8 *)__gu_p = (__force u8)(val); \
213 break; \
214 case 2: \
215 put_unaligned_le16((__force u16)(val), __gu_p); \
216 break; \
217 case 4: \
218 put_unaligned_le32((__force u32)(val), __gu_p); \
219 break; \
220 case 8: \
221 put_unaligned_le64((__force u64)(val), __gu_p); \
222 break; \
223 default: \
224 __bad_unaligned_access_size(); \
225 break; \
227 (void)0; })
229 #define __put_unaligned_be(val, ptr) ({ \
230 void *__gu_p = (ptr); \
231 switch (sizeof(*(ptr))) { \
232 case 1: \
233 *(u8 *)__gu_p = (__force u8)(val); \
234 break; \
235 case 2: \
236 put_unaligned_be16((__force u16)(val), __gu_p); \
237 break; \
238 case 4: \
239 put_unaligned_be32((__force u32)(val), __gu_p); \
240 break; \
241 case 8: \
242 put_unaligned_be64((__force u64)(val), __gu_p); \
243 break; \
244 default: \
245 __bad_unaligned_access_size(); \
246 break; \
248 (void)0; })
250 #ifdef __LITTLE_ENDIAN
251 # define get_unaligned __get_unaligned_le
252 # define put_unaligned __put_unaligned_le
253 #else
254 # define get_unaligned __get_unaligned_be
255 # define put_unaligned __put_unaligned_be
256 #endif
258 #endif /* __ASM_SH_UNALIGNED_SH4A_H */