xref: /linux/include/linux/virtio_byteorder.h (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 #ifndef _LINUX_VIRTIO_BYTEORDER_H
2 #define _LINUX_VIRTIO_BYTEORDER_H
3 #include <linux/types.h>
4 #include <uapi/linux/virtio_types.h>
5 
6 static inline bool virtio_legacy_is_little_endian(void)
7 {
8 #ifdef __LITTLE_ENDIAN
9 	return true;
10 #else
11 	return false;
12 #endif
13 }
14 
15 static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
16 {
17 	if (little_endian)
18 		return le16_to_cpu((__force __le16)val);
19 	else
20 		return be16_to_cpu((__force __be16)val);
21 }
22 
23 static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
24 {
25 	if (little_endian)
26 		return (__force __virtio16)cpu_to_le16(val);
27 	else
28 		return (__force __virtio16)cpu_to_be16(val);
29 }
30 
31 static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
32 {
33 	if (little_endian)
34 		return le32_to_cpu((__force __le32)val);
35 	else
36 		return be32_to_cpu((__force __be32)val);
37 }
38 
39 static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
40 {
41 	if (little_endian)
42 		return (__force __virtio32)cpu_to_le32(val);
43 	else
44 		return (__force __virtio32)cpu_to_be32(val);
45 }
46 
47 static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
48 {
49 	if (little_endian)
50 		return le64_to_cpu((__force __le64)val);
51 	else
52 		return be64_to_cpu((__force __be64)val);
53 }
54 
55 static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
56 {
57 	if (little_endian)
58 		return (__force __virtio64)cpu_to_le64(val);
59 	else
60 		return (__force __virtio64)cpu_to_be64(val);
61 }
62 
63 #endif /* _LINUX_VIRTIO_BYTEORDER */
64