blob: ce63a2c3a612936737ae14236294e93f44a2474b [file] [log] [blame]
Michael S. Tsirkineef960a02014-10-22 15:35:56 +03001#ifndef _LINUX_VIRTIO_BYTEORDER_H
2#define _LINUX_VIRTIO_BYTEORDER_H
3#include <linux/types.h>
4#include <uapi/linux/virtio_types.h>
5
Greg Kurz7d824102015-04-24 14:26:24 +02006static inline bool virtio_legacy_is_little_endian(void)
7{
8#ifdef __LITTLE_ENDIAN
9 return true;
10#else
11 return false;
12#endif
13}
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030014
15static 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
Greg Kurz7d824102015-04-24 14:26:24 +020020 return be16_to_cpu((__force __be16)val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030021}
22
23static 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
Greg Kurz7d824102015-04-24 14:26:24 +020028 return (__force __virtio16)cpu_to_be16(val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030029}
30
31static 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
Greg Kurz7d824102015-04-24 14:26:24 +020036 return be32_to_cpu((__force __be32)val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030037}
38
39static 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
Greg Kurz7d824102015-04-24 14:26:24 +020044 return (__force __virtio32)cpu_to_be32(val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030045}
46
47static 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
Greg Kurz7d824102015-04-24 14:26:24 +020052 return be64_to_cpu((__force __be64)val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030053}
54
55static 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
Greg Kurz7d824102015-04-24 14:26:24 +020060 return (__force __virtio64)cpu_to_be64(val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +030061}
62
63#endif /* _LINUX_VIRTIO_BYTEORDER */