1 #ifndef __ASM_SH_UNALIGNED_SH4A_H
2 #define __ASM_SH_UNALIGNED_SH4A_H
5 * SH-4A has support for unaligned 32-bit loads, and 32-bit loads only.
6 * Support for 16 and 64-bit accesses are done through shifting and
7 * masking relative to the endianness. Unaligned stores are not supported
8 * by the instruction encoding, so these continue to use the packed
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__ (
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 __get_unaligned_cpu32(p
) & 0xffff;
46 return __get_unaligned_cpu32(p
) >> 16;
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
);
61 return (u64
)__get_unaligned_cpu32(p
) << 32 |
62 __get_unaligned_cpu32(p
+ 4);
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
)
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
)
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
;
137 __put_le16_noalign(p
, val
);
141 static inline void put_unaligned_le32(u32 val
, void *p
)
143 #ifdef __LITTLE_ENDIAN
144 ((struct __una_u32
*)p
)->x
= val
;
146 __put_le32_noalign(p
, val
);
150 static inline void put_unaligned_le64(u64 val
, void *p
)
152 #ifdef __LITTLE_ENDIAN
153 ((struct __una_u64
*)p
)->x
= val
;
155 __put_le64_noalign(p
, val
);
159 static inline void put_unaligned_be16(u16 val
, void *p
)
162 ((struct __una_u16
*)p
)->x
= val
;
164 __put_be16_noalign(p
, val
);
168 static inline void put_unaligned_be32(u32 val
, void *p
)
171 ((struct __una_u32
*)p
)->x
= val
;
173 __put_be32_noalign(p
, val
);
177 static inline void put_unaligned_be64(u64 val
, void *p
)
180 ((struct __una_u64
*)p
)->x
= val
;
182 __put_be64_noalign(p
, val
);
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))) { \
212 *(u8 *)__gu_p = (__force u8)(val); \
215 put_unaligned_le16((__force u16)(val), __gu_p); \
218 put_unaligned_le32((__force u32)(val), __gu_p); \
221 put_unaligned_le64((__force u64)(val), __gu_p); \
224 __bad_unaligned_access_size(); \
229 #define __put_unaligned_be(val, ptr) ({ \
230 void *__gu_p = (ptr); \
231 switch (sizeof(*(ptr))) { \
233 *(u8 *)__gu_p = (__force u8)(val); \
236 put_unaligned_be16((__force u16)(val), __gu_p); \
239 put_unaligned_be32((__force u32)(val), __gu_p); \
242 put_unaligned_be64((__force u64)(val), __gu_p); \
245 __bad_unaligned_access_size(); \
250 #ifdef __LITTLE_ENDIAN
251 # define get_unaligned __get_unaligned_le
252 # define put_unaligned __put_unaligned_le
254 # define get_unaligned __get_unaligned_be
255 # define put_unaligned __put_unaligned_be
258 #endif /* __ASM_SH_UNALIGNED_SH4A_H */