Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 2 | #include <linux/virtio_byteorder.h> |
| 3 | #include <linux/virtio.h> |
| 4 | #include <uapi/linux/virtio_config.h> |
| 5 | |
| 6 | /* |
| 7 | * __virtio_test_bit - helper to test feature bits. For use by transports. |
| 8 | * Devices should normally use virtio_has_feature, |
| 9 | * which includes more checks. |
| 10 | * @vdev: the device |
| 11 | * @fbit: the feature bit |
| 12 | */ |
| 13 | static inline bool __virtio_test_bit(const struct virtio_device *vdev, |
| 14 | unsigned int fbit) |
| 15 | { |
| 16 | return vdev->features & (1ULL << fbit); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * __virtio_set_bit - helper to set feature bits. For use by transports. |
| 21 | * @vdev: the device |
| 22 | * @fbit: the feature bit |
| 23 | */ |
| 24 | static inline void __virtio_set_bit(struct virtio_device *vdev, |
| 25 | unsigned int fbit) |
| 26 | { |
| 27 | vdev->features |= (1ULL << fbit); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * __virtio_clear_bit - helper to clear feature bits. For use by transports. |
| 32 | * @vdev: the device |
| 33 | * @fbit: the feature bit |
| 34 | */ |
| 35 | static inline void __virtio_clear_bit(struct virtio_device *vdev, |
| 36 | unsigned int fbit) |
| 37 | { |
| 38 | vdev->features &= ~(1ULL << fbit); |
| 39 | } |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 40 | |
| 41 | #define virtio_has_feature(dev, feature) \ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 42 | (__virtio_test_bit((dev), feature)) |
Rusty Russell | 61d0b5a | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 43 | |
Michael S. Tsirkin | 6be3ffa | 2016-08-15 04:50:55 +0300 | [diff] [blame] | 44 | /** |
| 45 | * virtio_has_iommu_quirk - determine whether this device has the iommu quirk |
| 46 | * @vdev: the device |
| 47 | */ |
| 48 | static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev) |
| 49 | { |
| 50 | /* |
| 51 | * Note the reverse polarity of the quirk feature (compared to most |
| 52 | * other features), this is for compatibility with legacy systems. |
| 53 | */ |
| 54 | return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); |
| 55 | } |
| 56 | |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 57 | static inline bool virtio_is_little_endian(struct virtio_device *vdev) |
| 58 | { |
| 59 | return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) || |
| 60 | virtio_legacy_is_little_endian(); |
| 61 | } |
| 62 | |
| 63 | /* Memory accessors */ |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 64 | static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) |
| 65 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 66 | return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) |
| 70 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 71 | return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) |
| 75 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 76 | return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) |
| 80 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 81 | return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) |
| 85 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 86 | return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) |
| 90 | { |
Michael S. Tsirkin | 55564a0 | 2015-11-29 13:03:23 +0200 | [diff] [blame] | 91 | return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | 2d7ce0e | 2014-12-15 23:47:07 +0200 | [diff] [blame] | 92 | } |