Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_VIRTIO_FEATURES_H
3#define _LINUX_VIRTIO_FEATURES_H
4
5#include <linux/bits.h>
6
7#define VIRTIO_FEATURES_U64S 2
8#define VIRTIO_FEATURES_BITS (VIRTIO_FEATURES_U64S * 64)
9
10#define VIRTIO_BIT(b) BIT_ULL((b) & 0x3f)
11#define VIRTIO_U64(b) ((b) >> 6)
12
13#define VIRTIO_DECLARE_FEATURES(name) \
14 union { \
15 u64 name; \
16 u64 name##_array[VIRTIO_FEATURES_U64S];\
17 }
18
19static inline bool virtio_features_chk_bit(unsigned int bit)
20{
21 if (__builtin_constant_p(bit)) {
22 /*
23 * Don't care returning the correct value: the build
24 * will fail before any bad features access
25 */
26 BUILD_BUG_ON(bit >= VIRTIO_FEATURES_BITS);
27 } else {
28 if (WARN_ON_ONCE(bit >= VIRTIO_FEATURES_BITS))
29 return false;
30 }
31 return true;
32}
33
34static inline bool virtio_features_test_bit(const u64 *features,
35 unsigned int bit)
36{
37 return virtio_features_chk_bit(bit) &&
38 !!(features[VIRTIO_U64(bit)] & VIRTIO_BIT(bit));
39}
40
41static inline void virtio_features_set_bit(u64 *features,
42 unsigned int bit)
43{
44 if (virtio_features_chk_bit(bit))
45 features[VIRTIO_U64(bit)] |= VIRTIO_BIT(bit);
46}
47
48static inline void virtio_features_clear_bit(u64 *features,
49 unsigned int bit)
50{
51 if (virtio_features_chk_bit(bit))
52 features[VIRTIO_U64(bit)] &= ~VIRTIO_BIT(bit);
53}
54
55static inline void virtio_features_zero(u64 *features)
56{
57 memset(features, 0, sizeof(features[0]) * VIRTIO_FEATURES_U64S);
58}
59
60static inline void virtio_features_from_u64(u64 *features, u64 from)
61{
62 virtio_features_zero(features);
63 features[0] = from;
64}
65
66static inline bool virtio_features_equal(const u64 *f1, const u64 *f2)
67{
68 int i;
69
70 for (i = 0; i < VIRTIO_FEATURES_U64S; ++i)
71 if (f1[i] != f2[i])
72 return false;
73 return true;
74}
75
76static inline void virtio_features_copy(u64 *to, const u64 *from)
77{
78 memcpy(to, from, sizeof(to[0]) * VIRTIO_FEATURES_U64S);
79}
80
81static inline void virtio_features_andnot(u64 *to, const u64 *f1, const u64 *f2)
82{
83 int i;
84
85 for (i = 0; i < VIRTIO_FEATURES_U64S; i++)
86 to[i] = f1[i] & ~f2[i];
87}
88
89#endif