| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 |  | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 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 | } | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 20 | 		return be16_to_cpu((__force __be16)val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 28 | 		return (__force __virtio16)cpu_to_be16(val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 36 | 		return be32_to_cpu((__force __be32)val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 44 | 		return (__force __virtio32)cpu_to_be32(val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 52 | 		return be64_to_cpu((__force __be64)val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 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 | 
| Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 60 | 		return (__force __virtio64)cpu_to_be64(val); | 
| Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 61 | } | 
 | 62 |  | 
 | 63 | #endif /* _LINUX_VIRTIO_BYTEORDER */ |