1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_CONFIG_H
3 #define _LINUX_VIRTIO_CONFIG_H
7 #include <linux/virtio.h>
8 #include <linux/virtio_byteorder.h>
9 #include <uapi/linux/virtio_config.h>
14 * virtio_config_ops - operations for configuring a virtio device
15 * @get: read the value of a configuration field
16 * vdev: the virtio_device
17 * offset: the offset of the configuration field
18 * buf: the buffer to write the field value into.
19 * len: the length of the buffer
20 * @set: write the value of a configuration field
21 * vdev: the virtio_device
22 * offset: the offset of the configuration field
23 * buf: the buffer to read the field value from.
24 * len: the length of the buffer
25 * @generation: config generation counter
26 * vdev: the virtio_device
27 * Returns the config generation counter
28 * @get_status: read the status byte
29 * vdev: the virtio_device
30 * Returns the status byte
31 * @set_status: write the status byte
32 * vdev: the virtio_device
33 * status: the new status byte
34 * @reset: reset the device
35 * vdev: the virtio device
36 * After this, status and feature negotiation must be done again
37 * Device must not be reset from its vq/config callbacks, or in
38 * parallel with being added/removed.
39 * @find_vqs: find virtqueues and instantiate them.
40 * vdev: the virtio_device
41 * nvqs: the number of virtqueues to find
42 * vqs: on success, includes new virtqueues
43 * callbacks: array of callbacks, for each virtqueue
44 * include a NULL entry for vqs that do not need a callback
45 * names: array of virtqueue names (mainly for debugging)
46 * include a NULL entry for vqs unused by driver
47 * Returns 0 on success or error status
48 * @del_vqs: free virtqueues found by find_vqs().
49 * @get_features: get the array of feature bits for this device.
50 * vdev: the virtio_device
51 * Returns the first 32 feature bits (all we currently need).
52 * @finalize_features: confirm what device features we'll be using.
53 * vdev: the virtio_device
54 * This gives the final feature bits for the device: it can change
55 * the dev->feature bits if it wants.
56 * Returns 0 on success or error status
57 * @bus_name: return the bus name associated with the device
58 * vdev: the virtio_device
59 * This returns a pointer to the bus name a la pci_name from which
60 * the caller can then copy.
61 * @set_vq_affinity: set the affinity for a virtqueue.
62 * @get_vq_affinity: get the affinity for a virtqueue (optional).
64 typedef void vq_callback_t(struct virtqueue
*);
65 struct virtio_config_ops
{
66 void (*get
)(struct virtio_device
*vdev
, unsigned offset
,
67 void *buf
, unsigned len
);
68 void (*set
)(struct virtio_device
*vdev
, unsigned offset
,
69 const void *buf
, unsigned len
);
70 u32 (*generation
)(struct virtio_device
*vdev
);
71 u8 (*get_status
)(struct virtio_device
*vdev
);
72 void (*set_status
)(struct virtio_device
*vdev
, u8 status
);
73 void (*reset
)(struct virtio_device
*vdev
);
74 int (*find_vqs
)(struct virtio_device
*, unsigned nvqs
,
75 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
76 const char * const names
[], const bool *ctx
,
77 struct irq_affinity
*desc
);
78 void (*del_vqs
)(struct virtio_device
*);
79 u64 (*get_features
)(struct virtio_device
*vdev
);
80 int (*finalize_features
)(struct virtio_device
*vdev
);
81 const char *(*bus_name
)(struct virtio_device
*vdev
);
82 int (*set_vq_affinity
)(struct virtqueue
*vq
, int cpu
);
83 const struct cpumask
*(*get_vq_affinity
)(struct virtio_device
*vdev
,
87 /* If driver didn't advertise the feature, it will never appear. */
88 void virtio_check_driver_offered_feature(const struct virtio_device
*vdev
,
92 * __virtio_test_bit - helper to test feature bits. For use by transports.
93 * Devices should normally use virtio_has_feature,
94 * which includes more checks.
96 * @fbit: the feature bit
98 static inline bool __virtio_test_bit(const struct virtio_device
*vdev
,
101 /* Did you forget to fix assumptions on max features? */
102 if (__builtin_constant_p(fbit
))
103 BUILD_BUG_ON(fbit
>= 64);
107 return vdev
->features
& BIT_ULL(fbit
);
111 * __virtio_set_bit - helper to set feature bits. For use by transports.
113 * @fbit: the feature bit
115 static inline void __virtio_set_bit(struct virtio_device
*vdev
,
118 /* Did you forget to fix assumptions on max features? */
119 if (__builtin_constant_p(fbit
))
120 BUILD_BUG_ON(fbit
>= 64);
124 vdev
->features
|= BIT_ULL(fbit
);
128 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
130 * @fbit: the feature bit
132 static inline void __virtio_clear_bit(struct virtio_device
*vdev
,
135 /* Did you forget to fix assumptions on max features? */
136 if (__builtin_constant_p(fbit
))
137 BUILD_BUG_ON(fbit
>= 64);
141 vdev
->features
&= ~BIT_ULL(fbit
);
145 * virtio_has_feature - helper to determine if this device has this feature.
147 * @fbit: the feature bit
149 static inline bool virtio_has_feature(const struct virtio_device
*vdev
,
152 if (fbit
< VIRTIO_TRANSPORT_F_START
)
153 virtio_check_driver_offered_feature(vdev
, fbit
);
155 return __virtio_test_bit(vdev
, fbit
);
159 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
162 static inline bool virtio_has_iommu_quirk(const struct virtio_device
*vdev
)
165 * Note the reverse polarity of the quirk feature (compared to most
166 * other features), this is for compatibility with legacy systems.
168 return !virtio_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
);
172 struct virtqueue
*virtio_find_single_vq(struct virtio_device
*vdev
,
173 vq_callback_t
*c
, const char *n
)
175 vq_callback_t
*callbacks
[] = { c
};
176 const char *names
[] = { n
};
177 struct virtqueue
*vq
;
178 int err
= vdev
->config
->find_vqs(vdev
, 1, &vq
, callbacks
, names
, NULL
,
186 int virtio_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
187 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
188 const char * const names
[],
189 struct irq_affinity
*desc
)
191 return vdev
->config
->find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
, NULL
, desc
);
195 int virtio_find_vqs_ctx(struct virtio_device
*vdev
, unsigned nvqs
,
196 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
197 const char * const names
[], const bool *ctx
,
198 struct irq_affinity
*desc
)
200 return vdev
->config
->find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
, ctx
,
205 * virtio_device_ready - enable vq use in probe function
208 * Driver must call this to use vqs in the probe function.
210 * Note: vqs are enabled automatically after probe returns.
213 void virtio_device_ready(struct virtio_device
*dev
)
215 unsigned status
= dev
->config
->get_status(dev
);
217 BUG_ON(status
& VIRTIO_CONFIG_S_DRIVER_OK
);
218 dev
->config
->set_status(dev
, status
| VIRTIO_CONFIG_S_DRIVER_OK
);
222 const char *virtio_bus_name(struct virtio_device
*vdev
)
224 if (!vdev
->config
->bus_name
)
226 return vdev
->config
->bus_name(vdev
);
230 * virtqueue_set_affinity - setting affinity for a virtqueue
234 * Pay attention the function are best-effort: the affinity hint may not be set
235 * due to config support, irq type and sharing.
239 int virtqueue_set_affinity(struct virtqueue
*vq
, int cpu
)
241 struct virtio_device
*vdev
= vq
->vdev
;
242 if (vdev
->config
->set_vq_affinity
)
243 return vdev
->config
->set_vq_affinity(vq
, cpu
);
247 static inline bool virtio_is_little_endian(struct virtio_device
*vdev
)
249 return virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
) ||
250 virtio_legacy_is_little_endian();
253 /* Memory accessors */
254 static inline u16
virtio16_to_cpu(struct virtio_device
*vdev
, __virtio16 val
)
256 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
259 static inline __virtio16
cpu_to_virtio16(struct virtio_device
*vdev
, u16 val
)
261 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
264 static inline u32
virtio32_to_cpu(struct virtio_device
*vdev
, __virtio32 val
)
266 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
269 static inline __virtio32
cpu_to_virtio32(struct virtio_device
*vdev
, u32 val
)
271 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
274 static inline u64
virtio64_to_cpu(struct virtio_device
*vdev
, __virtio64 val
)
276 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
279 static inline __virtio64
cpu_to_virtio64(struct virtio_device
*vdev
, u64 val
)
281 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
284 /* Config space accessors. */
285 #define virtio_cread(vdev, structname, member, ptr) \
287 /* Must match the member's type, and be integer */ \
288 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
291 switch (sizeof(*ptr)) { \
293 *(ptr) = virtio_cread8(vdev, \
294 offsetof(structname, member)); \
297 *(ptr) = virtio_cread16(vdev, \
298 offsetof(structname, member)); \
301 *(ptr) = virtio_cread32(vdev, \
302 offsetof(structname, member)); \
305 *(ptr) = virtio_cread64(vdev, \
306 offsetof(structname, member)); \
313 /* Config space accessors. */
314 #define virtio_cwrite(vdev, structname, member, ptr) \
316 /* Must match the member's type, and be integer */ \
317 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
318 BUG_ON((*ptr) == 1); \
320 switch (sizeof(*ptr)) { \
322 virtio_cwrite8(vdev, \
323 offsetof(structname, member), \
327 virtio_cwrite16(vdev, \
328 offsetof(structname, member), \
332 virtio_cwrite32(vdev, \
333 offsetof(structname, member), \
337 virtio_cwrite64(vdev, \
338 offsetof(structname, member), \
346 /* Read @count fields, @bytes each. */
347 static inline void __virtio_cread_many(struct virtio_device
*vdev
,
349 void *buf
, size_t count
, size_t bytes
)
351 u32 old
, gen
= vdev
->config
->generation
?
352 vdev
->config
->generation(vdev
) : 0;
358 for (i
= 0; i
< count
; i
++)
359 vdev
->config
->get(vdev
, offset
+ bytes
* i
,
360 buf
+ i
* bytes
, bytes
);
362 gen
= vdev
->config
->generation
?
363 vdev
->config
->generation(vdev
) : 0;
364 } while (gen
!= old
);
367 static inline void virtio_cread_bytes(struct virtio_device
*vdev
,
369 void *buf
, size_t len
)
371 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
374 static inline u8
virtio_cread8(struct virtio_device
*vdev
, unsigned int offset
)
377 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
381 static inline void virtio_cwrite8(struct virtio_device
*vdev
,
382 unsigned int offset
, u8 val
)
384 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
387 static inline u16
virtio_cread16(struct virtio_device
*vdev
,
391 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
392 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
395 static inline void virtio_cwrite16(struct virtio_device
*vdev
,
396 unsigned int offset
, u16 val
)
398 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
399 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
402 static inline u32
virtio_cread32(struct virtio_device
*vdev
,
406 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
407 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
410 static inline void virtio_cwrite32(struct virtio_device
*vdev
,
411 unsigned int offset
, u32 val
)
413 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
414 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
417 static inline u64
virtio_cread64(struct virtio_device
*vdev
,
421 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
422 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
425 static inline void virtio_cwrite64(struct virtio_device
*vdev
,
426 unsigned int offset
, u64 val
)
428 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
429 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
432 /* Conditional config space accessors. */
433 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
436 if (!virtio_has_feature(vdev, fbit)) \
439 virtio_cread((vdev), structname, member, ptr); \
443 #endif /* _LINUX_VIRTIO_CONFIG_H */