blob: 9ba11815e0a16b93ec0e04735a3486ac9dc6bfe4 [file] [log] [blame]
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +02001#include <linux/virtio_byteorder.h>
2#include <linux/virtio.h>
3#include <uapi/linux/virtio_config.h>
4
5/*
6 * __virtio_test_bit - helper to test feature bits. For use by transports.
7 * Devices should normally use virtio_has_feature,
8 * which includes more checks.
9 * @vdev: the device
10 * @fbit: the feature bit
11 */
12static inline bool __virtio_test_bit(const struct virtio_device *vdev,
13 unsigned int fbit)
14{
15 return vdev->features & (1ULL << fbit);
16}
17
18/**
19 * __virtio_set_bit - helper to set feature bits. For use by transports.
20 * @vdev: the device
21 * @fbit: the feature bit
22 */
23static inline void __virtio_set_bit(struct virtio_device *vdev,
24 unsigned int fbit)
25{
26 vdev->features |= (1ULL << fbit);
27}
28
29/**
30 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
31 * @vdev: the device
32 * @fbit: the feature bit
33 */
34static inline void __virtio_clear_bit(struct virtio_device *vdev,
35 unsigned int fbit)
36{
37 vdev->features &= ~(1ULL << fbit);
38}
Rusty Russell61d0b5a42013-03-18 13:22:19 +103039
40#define virtio_has_feature(dev, feature) \
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020041 (__virtio_test_bit((dev), feature))
Rusty Russell61d0b5a42013-03-18 13:22:19 +103042
Michael S. Tsirkin6be3ffa2016-08-15 04:50:55 +030043/**
44 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
45 * @vdev: the device
46 */
47static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
48{
49 /*
50 * Note the reverse polarity of the quirk feature (compared to most
51 * other features), this is for compatibility with legacy systems.
52 */
53 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
54}
55
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020056static inline bool virtio_is_little_endian(struct virtio_device *vdev)
57{
58 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
59 virtio_legacy_is_little_endian();
60}
61
62/* Memory accessors */
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020063static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
64{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020065 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020066}
67
68static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
69{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020070 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020071}
72
73static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
74{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020075 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020076}
77
78static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
79{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020080 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020081}
82
83static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
84{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020085 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020086}
87
88static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
89{
Michael S. Tsirkin55564a02015-11-29 13:03:23 +020090 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
Michael S. Tsirkin2d7ce0e2014-12-15 23:47:07 +020091}