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 <linux/compiler_types.h>
10 #include <uapi/linux/virtio_config.h>
14 struct virtio_shm_region {
20 * virtio_config_ops - operations for configuring a virtio device
21 * Note: Do not assume that a transport implements all of the operations
22 * getting/setting a value as a simple read/write! Generally speaking,
23 * any of @get/@set, @get_status/@set_status, or @get_features/
24 * @finalize_features are NOT safe to be called from an atomic
26 * @get: read the value of a configuration field
27 * vdev: the virtio_device
28 * offset: the offset of the configuration field
29 * buf: the buffer to write the field value into.
30 * len: the length of the buffer
31 * @set: write the value of a configuration field
32 * vdev: the virtio_device
33 * offset: the offset of the configuration field
34 * buf: the buffer to read the field value from.
35 * len: the length of the buffer
36 * @generation: config generation counter (optional)
37 * vdev: the virtio_device
38 * Returns the config generation counter
39 * @get_status: read the status byte
40 * vdev: the virtio_device
41 * Returns the status byte
42 * @set_status: write the status byte
43 * vdev: the virtio_device
44 * status: the new status byte
45 * @reset: reset the device
46 * vdev: the virtio device
47 * After this, status and feature negotiation must be done again
48 * Device must not be reset from its vq/config callbacks, or in
49 * parallel with being added/removed.
50 * @find_vqs: find virtqueues and instantiate them.
51 * vdev: the virtio_device
52 * nvqs: the number of virtqueues to find
53 * vqs: on success, includes new virtqueues
54 * callbacks: array of callbacks, for each virtqueue
55 * include a NULL entry for vqs that do not need a callback
56 * names: array of virtqueue names (mainly for debugging)
57 * include a NULL entry for vqs unused by driver
58 * Returns 0 on success or error status
59 * @del_vqs: free virtqueues found by find_vqs().
60 * @synchronize_cbs: synchronize with the virtqueue callbacks (optional)
61 * The function guarantees that all memory operations on the
62 * queue before it are visible to the vring_interrupt() that is
64 * vdev: the virtio_device
65 * @get_features: get the array of feature bits for this device.
66 * vdev: the virtio_device
67 * Returns the first 64 feature bits (all we currently need).
68 * @finalize_features: confirm what device features we'll be using.
69 * vdev: the virtio_device
70 * This sends the driver feature bits to the device: it can change
71 * the dev->feature bits if it wants.
72 * Note: despite the name this can be called any number of times.
73 * Returns 0 on success or error status
74 * @bus_name: return the bus name associated with the device (optional)
75 * vdev: the virtio_device
76 * This returns a pointer to the bus name a la pci_name from which
77 * the caller can then copy.
78 * @set_vq_affinity: set the affinity for a virtqueue (optional).
79 * @get_vq_affinity: get the affinity for a virtqueue (optional).
80 * @get_shm_region: get a shared memory region based on the index.
81 * @disable_vq_and_reset: reset a queue individually (optional).
83 * Returns 0 on success or error status
84 * disable_vq_and_reset will guarantee that the callbacks are disabled and
86 * Except for the callback, the caller should guarantee that the vring is
87 * not accessed by any functions of virtqueue.
88 * @enable_vq_after_reset: enable a reset queue
90 * Returns 0 on success or error status
91 * If disable_vq_and_reset is set, then enable_vq_after_reset must also be
94 typedef void vq_callback_t(struct virtqueue *);
95 struct virtio_config_ops {
96 void (*get)(struct virtio_device *vdev, unsigned offset,
97 void *buf, unsigned len);
98 void (*set)(struct virtio_device *vdev, unsigned offset,
99 const void *buf, unsigned len);
100 u32 (*generation)(struct virtio_device *vdev);
101 u8 (*get_status)(struct virtio_device *vdev);
102 void (*set_status)(struct virtio_device *vdev, u8 status);
103 void (*reset)(struct virtio_device *vdev);
104 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
105 struct virtqueue *vqs[], vq_callback_t *callbacks[],
106 const char * const names[], const bool *ctx,
107 struct irq_affinity *desc);
108 void (*del_vqs)(struct virtio_device *);
109 void (*synchronize_cbs)(struct virtio_device *);
110 u64 (*get_features)(struct virtio_device *vdev);
111 int (*finalize_features)(struct virtio_device *vdev);
112 const char *(*bus_name)(struct virtio_device *vdev);
113 int (*set_vq_affinity)(struct virtqueue *vq,
114 const struct cpumask *cpu_mask);
115 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
117 bool (*get_shm_region)(struct virtio_device *vdev,
118 struct virtio_shm_region *region, u8 id);
119 int (*disable_vq_and_reset)(struct virtqueue *vq);
120 int (*enable_vq_after_reset)(struct virtqueue *vq);
123 /* If driver didn't advertise the feature, it will never appear. */
124 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
128 * __virtio_test_bit - helper to test feature bits. For use by transports.
129 * Devices should normally use virtio_has_feature,
130 * which includes more checks.
132 * @fbit: the feature bit
134 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
137 /* Did you forget to fix assumptions on max features? */
138 if (__builtin_constant_p(fbit))
139 BUILD_BUG_ON(fbit >= 64);
143 return vdev->features & BIT_ULL(fbit);
147 * __virtio_set_bit - helper to set feature bits. For use by transports.
149 * @fbit: the feature bit
151 static inline void __virtio_set_bit(struct virtio_device *vdev,
154 /* Did you forget to fix assumptions on max features? */
155 if (__builtin_constant_p(fbit))
156 BUILD_BUG_ON(fbit >= 64);
160 vdev->features |= BIT_ULL(fbit);
164 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
166 * @fbit: the feature bit
168 static inline void __virtio_clear_bit(struct virtio_device *vdev,
171 /* Did you forget to fix assumptions on max features? */
172 if (__builtin_constant_p(fbit))
173 BUILD_BUG_ON(fbit >= 64);
177 vdev->features &= ~BIT_ULL(fbit);
181 * virtio_has_feature - helper to determine if this device has this feature.
183 * @fbit: the feature bit
185 static inline bool virtio_has_feature(const struct virtio_device *vdev,
188 if (fbit < VIRTIO_TRANSPORT_F_START)
189 virtio_check_driver_offered_feature(vdev, fbit);
191 return __virtio_test_bit(vdev, fbit);
195 * virtio_has_dma_quirk - determine whether this device has the DMA quirk
198 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
201 * Note the reverse polarity of the quirk feature (compared to most
202 * other features), this is for compatibility with legacy systems.
204 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
208 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
209 vq_callback_t *c, const char *n)
211 vq_callback_t *callbacks[] = { c };
212 const char *names[] = { n };
213 struct virtqueue *vq;
214 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
222 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
223 struct virtqueue *vqs[], vq_callback_t *callbacks[],
224 const char * const names[],
225 struct irq_affinity *desc)
227 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
231 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
232 struct virtqueue *vqs[], vq_callback_t *callbacks[],
233 const char * const names[], const bool *ctx,
234 struct irq_affinity *desc)
236 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
241 * virtio_synchronize_cbs - synchronize with virtqueue callbacks
242 * @dev: the virtio device
245 void virtio_synchronize_cbs(struct virtio_device *dev)
247 if (dev->config->synchronize_cbs) {
248 dev->config->synchronize_cbs(dev);
251 * A best effort fallback to synchronize with
252 * interrupts, preemption and softirq disabled
253 * regions. See comment above synchronize_rcu().
260 * virtio_device_ready - enable vq use in probe function
261 * @dev: the virtio device
263 * Driver must call this to use vqs in the probe function.
265 * Note: vqs are enabled automatically after probe returns.
268 void virtio_device_ready(struct virtio_device *dev)
270 unsigned status = dev->config->get_status(dev);
272 WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
274 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
276 * The virtio_synchronize_cbs() makes sure vring_interrupt()
277 * will see the driver specific setup if it sees vq->broken
278 * as false (even if the notifications come before DRIVER_OK).
280 virtio_synchronize_cbs(dev);
281 __virtio_unbreak_device(dev);
284 * The transport should ensure the visibility of vq->broken
285 * before setting DRIVER_OK. See the comments for the transport
286 * specific set_status() method.
288 * A well behaved device will only notify a virtqueue after
289 * DRIVER_OK, this means the device should "see" the coherenct
290 * memory write that set vq->broken as false which is done by
291 * the driver when it sees DRIVER_OK, then the following
292 * driver's vring_interrupt() will see vq->broken as false so
293 * we won't lose any notification.
295 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
299 const char *virtio_bus_name(struct virtio_device *vdev)
301 if (!vdev->config->bus_name)
303 return vdev->config->bus_name(vdev);
307 * virtqueue_set_affinity - setting affinity for a virtqueue
309 * @cpu_mask: the cpu mask
311 * Pay attention the function are best-effort: the affinity hint may not be set
312 * due to config support, irq type and sharing.
316 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
318 struct virtio_device *vdev = vq->vdev;
319 if (vdev->config->set_vq_affinity)
320 return vdev->config->set_vq_affinity(vq, cpu_mask);
325 bool virtio_get_shm_region(struct virtio_device *vdev,
326 struct virtio_shm_region *region, u8 id)
328 if (!vdev->config->get_shm_region)
330 return vdev->config->get_shm_region(vdev, region, id);
333 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
335 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
336 virtio_legacy_is_little_endian();
339 /* Memory accessors */
340 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
342 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
345 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
347 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
350 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
352 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
355 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
357 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
360 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
362 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
365 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
367 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
370 #define virtio_to_cpu(vdev, x) \
373 __virtio16: virtio16_to_cpu((vdev), (x)), \
374 __virtio32: virtio32_to_cpu((vdev), (x)), \
375 __virtio64: virtio64_to_cpu((vdev), (x)) \
378 #define cpu_to_virtio(vdev, x, m) \
381 __virtio16: cpu_to_virtio16((vdev), (x)), \
382 __virtio32: cpu_to_virtio32((vdev), (x)), \
383 __virtio64: cpu_to_virtio64((vdev), (x)) \
386 #define __virtio_native_type(structname, member) \
387 typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
389 /* Config space accessors. */
390 #define virtio_cread(vdev, structname, member, ptr) \
392 typeof(((structname*)0)->member) virtio_cread_v; \
395 /* Sanity check: must match the member's type */ \
396 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
398 switch (sizeof(virtio_cread_v)) { \
402 vdev->config->get((vdev), \
403 offsetof(structname, member), \
405 sizeof(virtio_cread_v)); \
408 __virtio_cread_many((vdev), \
409 offsetof(structname, member), \
412 sizeof(virtio_cread_v)); \
415 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \
418 /* Config space accessors. */
419 #define virtio_cwrite(vdev, structname, member, ptr) \
421 typeof(((structname*)0)->member) virtio_cwrite_v = \
422 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
425 /* Sanity check: must match the member's type */ \
426 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
428 vdev->config->set((vdev), offsetof(structname, member), \
430 sizeof(virtio_cwrite_v)); \
434 * Nothing virtio-specific about these, but let's worry about generalizing
437 #define virtio_le_to_cpu(x) \
440 __le16: (u16)le16_to_cpu(x), \
441 __le32: (u32)le32_to_cpu(x), \
442 __le64: (u64)le64_to_cpu(x) \
445 #define virtio_cpu_to_le(x, m) \
448 __le16: cpu_to_le16(x), \
449 __le32: cpu_to_le32(x), \
450 __le64: cpu_to_le64(x) \
453 /* LE (e.g. modern) Config space accessors. */
454 #define virtio_cread_le(vdev, structname, member, ptr) \
456 typeof(((structname*)0)->member) virtio_cread_v; \
459 /* Sanity check: must match the member's type */ \
460 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
462 switch (sizeof(virtio_cread_v)) { \
466 vdev->config->get((vdev), \
467 offsetof(structname, member), \
469 sizeof(virtio_cread_v)); \
472 __virtio_cread_many((vdev), \
473 offsetof(structname, member), \
476 sizeof(virtio_cread_v)); \
479 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \
482 #define virtio_cwrite_le(vdev, structname, member, ptr) \
484 typeof(((structname*)0)->member) virtio_cwrite_v = \
485 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
488 /* Sanity check: must match the member's type */ \
489 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
491 vdev->config->set((vdev), offsetof(structname, member), \
493 sizeof(virtio_cwrite_v)); \
497 /* Read @count fields, @bytes each. */
498 static inline void __virtio_cread_many(struct virtio_device *vdev,
500 void *buf, size_t count, size_t bytes)
502 u32 old, gen = vdev->config->generation ?
503 vdev->config->generation(vdev) : 0;
510 for (i = 0; i < count; i++)
511 vdev->config->get(vdev, offset + bytes * i,
512 buf + i * bytes, bytes);
514 gen = vdev->config->generation ?
515 vdev->config->generation(vdev) : 0;
516 } while (gen != old);
519 static inline void virtio_cread_bytes(struct virtio_device *vdev,
521 void *buf, size_t len)
523 __virtio_cread_many(vdev, offset, buf, len, 1);
526 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
531 vdev->config->get(vdev, offset, &ret, sizeof(ret));
535 static inline void virtio_cwrite8(struct virtio_device *vdev,
536 unsigned int offset, u8 val)
539 vdev->config->set(vdev, offset, &val, sizeof(val));
542 static inline u16 virtio_cread16(struct virtio_device *vdev,
548 vdev->config->get(vdev, offset, &ret, sizeof(ret));
549 return virtio16_to_cpu(vdev, ret);
552 static inline void virtio_cwrite16(struct virtio_device *vdev,
553 unsigned int offset, u16 val)
558 v = cpu_to_virtio16(vdev, val);
559 vdev->config->set(vdev, offset, &v, sizeof(v));
562 static inline u32 virtio_cread32(struct virtio_device *vdev,
568 vdev->config->get(vdev, offset, &ret, sizeof(ret));
569 return virtio32_to_cpu(vdev, ret);
572 static inline void virtio_cwrite32(struct virtio_device *vdev,
573 unsigned int offset, u32 val)
578 v = cpu_to_virtio32(vdev, val);
579 vdev->config->set(vdev, offset, &v, sizeof(v));
582 static inline u64 virtio_cread64(struct virtio_device *vdev,
587 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
588 return virtio64_to_cpu(vdev, ret);
591 static inline void virtio_cwrite64(struct virtio_device *vdev,
592 unsigned int offset, u64 val)
597 v = cpu_to_virtio64(vdev, val);
598 vdev->config->set(vdev, offset, &v, sizeof(v));
601 /* Conditional config space accessors. */
602 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
605 if (!virtio_has_feature(vdev, fbit)) \
608 virtio_cread((vdev), structname, member, ptr); \
612 /* Conditional config space accessors. */
613 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \
616 if (!virtio_has_feature(vdev, fbit)) \
619 virtio_cread_le((vdev), structname, member, ptr); \
623 #endif /* _LINUX_VIRTIO_CONFIG_H */