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 {
19 typedef void vq_callback_t(struct virtqueue *);
22 * struct virtio_config_ops - operations for configuring a virtio device
23 * Note: Do not assume that a transport implements all of the operations
24 * getting/setting a value as a simple read/write! Generally speaking,
25 * any of @get/@set, @get_status/@set_status, or @get_features/
26 * @finalize_features are NOT safe to be called from an atomic
28 * @get: read the value of a configuration field
29 * vdev: the virtio_device
30 * offset: the offset of the configuration field
31 * buf: the buffer to write the field value into.
32 * len: the length of the buffer
33 * @set: write the value of a configuration field
34 * vdev: the virtio_device
35 * offset: the offset of the configuration field
36 * buf: the buffer to read the field value from.
37 * len: the length of the buffer
38 * @generation: config generation counter (optional)
39 * vdev: the virtio_device
40 * Returns the config generation counter
41 * @get_status: read the status byte
42 * vdev: the virtio_device
43 * Returns the status byte
44 * @set_status: write the status byte
45 * vdev: the virtio_device
46 * status: the new status byte
47 * @reset: reset the device
48 * vdev: the virtio device
49 * After this, status and feature negotiation must be done again
50 * Device must not be reset from its vq/config callbacks, or in
51 * parallel with being added/removed.
52 * @find_vqs: find virtqueues and instantiate them.
53 * vdev: the virtio_device
54 * nvqs: the number of virtqueues to find
55 * vqs: on success, includes new virtqueues
56 * callbacks: array of callbacks, for each virtqueue
57 * include a NULL entry for vqs that do not need a callback
58 * names: array of virtqueue names (mainly for debugging)
59 * include a NULL entry for vqs unused by driver
60 * Returns 0 on success or error status
61 * @del_vqs: free virtqueues found by find_vqs().
62 * @synchronize_cbs: synchronize with the virtqueue callbacks (optional)
63 * The function guarantees that all memory operations on the
64 * queue before it are visible to the vring_interrupt() that is
66 * vdev: the virtio_device
67 * @get_features: get the array of feature bits for this device.
68 * vdev: the virtio_device
69 * Returns the first 64 feature bits (all we currently need).
70 * @finalize_features: confirm what device features we'll be using.
71 * vdev: the virtio_device
72 * This sends the driver feature bits to the device: it can change
73 * the dev->feature bits if it wants.
74 * Note that despite the name this can be called any number of
76 * Returns 0 on success or error status
77 * @bus_name: return the bus name associated with the device (optional)
78 * vdev: the virtio_device
79 * This returns a pointer to the bus name a la pci_name from which
80 * the caller can then copy.
81 * @set_vq_affinity: set the affinity for a virtqueue (optional).
82 * @get_vq_affinity: get the affinity for a virtqueue (optional).
83 * @get_shm_region: get a shared memory region based on the index.
84 * @disable_vq_and_reset: reset a queue individually (optional).
86 * Returns 0 on success or error status
87 * disable_vq_and_reset will guarantee that the callbacks are disabled and
89 * Except for the callback, the caller should guarantee that the vring is
90 * not accessed by any functions of virtqueue.
91 * @enable_vq_after_reset: enable a reset queue
93 * Returns 0 on success or error status
94 * If disable_vq_and_reset is set, then enable_vq_after_reset must also be
96 * @create_avq: create admin virtqueue resource.
97 * @destroy_avq: destroy admin virtqueue resource.
99 struct virtio_config_ops {
100 void (*get)(struct virtio_device *vdev, unsigned offset,
101 void *buf, unsigned len);
102 void (*set)(struct virtio_device *vdev, unsigned offset,
103 const void *buf, unsigned len);
104 u32 (*generation)(struct virtio_device *vdev);
105 u8 (*get_status)(struct virtio_device *vdev);
106 void (*set_status)(struct virtio_device *vdev, u8 status);
107 void (*reset)(struct virtio_device *vdev);
108 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
109 struct virtqueue *vqs[], vq_callback_t *callbacks[],
110 const char * const names[], const bool *ctx,
111 struct irq_affinity *desc);
112 void (*del_vqs)(struct virtio_device *);
113 void (*synchronize_cbs)(struct virtio_device *);
114 u64 (*get_features)(struct virtio_device *vdev);
115 int (*finalize_features)(struct virtio_device *vdev);
116 const char *(*bus_name)(struct virtio_device *vdev);
117 int (*set_vq_affinity)(struct virtqueue *vq,
118 const struct cpumask *cpu_mask);
119 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
121 bool (*get_shm_region)(struct virtio_device *vdev,
122 struct virtio_shm_region *region, u8 id);
123 int (*disable_vq_and_reset)(struct virtqueue *vq);
124 int (*enable_vq_after_reset)(struct virtqueue *vq);
125 int (*create_avq)(struct virtio_device *vdev);
126 void (*destroy_avq)(struct virtio_device *vdev);
129 /* If driver didn't advertise the feature, it will never appear. */
130 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
134 * __virtio_test_bit - helper to test feature bits. For use by transports.
135 * Devices should normally use virtio_has_feature,
136 * which includes more checks.
138 * @fbit: the feature bit
140 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
143 /* Did you forget to fix assumptions on max features? */
144 if (__builtin_constant_p(fbit))
145 BUILD_BUG_ON(fbit >= 64);
149 return vdev->features & BIT_ULL(fbit);
153 * __virtio_set_bit - helper to set feature bits. For use by transports.
155 * @fbit: the feature bit
157 static inline void __virtio_set_bit(struct virtio_device *vdev,
160 /* Did you forget to fix assumptions on max features? */
161 if (__builtin_constant_p(fbit))
162 BUILD_BUG_ON(fbit >= 64);
166 vdev->features |= BIT_ULL(fbit);
170 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
172 * @fbit: the feature bit
174 static inline void __virtio_clear_bit(struct virtio_device *vdev,
177 /* Did you forget to fix assumptions on max features? */
178 if (__builtin_constant_p(fbit))
179 BUILD_BUG_ON(fbit >= 64);
183 vdev->features &= ~BIT_ULL(fbit);
187 * virtio_has_feature - helper to determine if this device has this feature.
189 * @fbit: the feature bit
191 static inline bool virtio_has_feature(const struct virtio_device *vdev,
194 if (fbit < VIRTIO_TRANSPORT_F_START)
195 virtio_check_driver_offered_feature(vdev, fbit);
197 return __virtio_test_bit(vdev, fbit);
201 * virtio_has_dma_quirk - determine whether this device has the DMA quirk
204 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
207 * Note the reverse polarity of the quirk feature (compared to most
208 * other features), this is for compatibility with legacy systems.
210 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
214 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
215 vq_callback_t *c, const char *n)
217 vq_callback_t *callbacks[] = { c };
218 const char *names[] = { n };
219 struct virtqueue *vq;
220 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
228 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
229 struct virtqueue *vqs[], vq_callback_t *callbacks[],
230 const char * const names[],
231 struct irq_affinity *desc)
233 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
237 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
238 struct virtqueue *vqs[], vq_callback_t *callbacks[],
239 const char * const names[], const bool *ctx,
240 struct irq_affinity *desc)
242 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
247 * virtio_synchronize_cbs - synchronize with virtqueue callbacks
248 * @dev: the virtio device
251 void virtio_synchronize_cbs(struct virtio_device *dev)
253 if (dev->config->synchronize_cbs) {
254 dev->config->synchronize_cbs(dev);
257 * A best effort fallback to synchronize with
258 * interrupts, preemption and softirq disabled
259 * regions. See comment above synchronize_rcu().
266 * virtio_device_ready - enable vq use in probe function
267 * @dev: the virtio device
269 * Driver must call this to use vqs in the probe function.
271 * Note: vqs are enabled automatically after probe returns.
274 void virtio_device_ready(struct virtio_device *dev)
276 unsigned status = dev->config->get_status(dev);
278 WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
280 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
282 * The virtio_synchronize_cbs() makes sure vring_interrupt()
283 * will see the driver specific setup if it sees vq->broken
284 * as false (even if the notifications come before DRIVER_OK).
286 virtio_synchronize_cbs(dev);
287 __virtio_unbreak_device(dev);
290 * The transport should ensure the visibility of vq->broken
291 * before setting DRIVER_OK. See the comments for the transport
292 * specific set_status() method.
294 * A well behaved device will only notify a virtqueue after
295 * DRIVER_OK, this means the device should "see" the coherenct
296 * memory write that set vq->broken as false which is done by
297 * the driver when it sees DRIVER_OK, then the following
298 * driver's vring_interrupt() will see vq->broken as false so
299 * we won't lose any notification.
301 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
305 const char *virtio_bus_name(struct virtio_device *vdev)
307 if (!vdev->config->bus_name)
309 return vdev->config->bus_name(vdev);
313 * virtqueue_set_affinity - setting affinity for a virtqueue
315 * @cpu_mask: the cpu mask
317 * Pay attention the function are best-effort: the affinity hint may not be set
318 * due to config support, irq type and sharing.
322 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
324 struct virtio_device *vdev = vq->vdev;
325 if (vdev->config->set_vq_affinity)
326 return vdev->config->set_vq_affinity(vq, cpu_mask);
331 bool virtio_get_shm_region(struct virtio_device *vdev,
332 struct virtio_shm_region *region, u8 id)
334 if (!vdev->config->get_shm_region)
336 return vdev->config->get_shm_region(vdev, region, id);
339 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
341 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
342 virtio_legacy_is_little_endian();
345 /* Memory accessors */
346 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
348 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
351 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
353 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
356 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
358 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
361 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
363 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
366 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
368 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
371 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
373 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
376 #define virtio_to_cpu(vdev, x) \
379 __virtio16: virtio16_to_cpu((vdev), (x)), \
380 __virtio32: virtio32_to_cpu((vdev), (x)), \
381 __virtio64: virtio64_to_cpu((vdev), (x)) \
384 #define cpu_to_virtio(vdev, x, m) \
387 __virtio16: cpu_to_virtio16((vdev), (x)), \
388 __virtio32: cpu_to_virtio32((vdev), (x)), \
389 __virtio64: cpu_to_virtio64((vdev), (x)) \
392 #define __virtio_native_type(structname, member) \
393 typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
395 /* Config space accessors. */
396 #define virtio_cread(vdev, structname, member, ptr) \
398 typeof(((structname*)0)->member) virtio_cread_v; \
401 /* Sanity check: must match the member's type */ \
402 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
404 switch (sizeof(virtio_cread_v)) { \
408 vdev->config->get((vdev), \
409 offsetof(structname, member), \
411 sizeof(virtio_cread_v)); \
414 __virtio_cread_many((vdev), \
415 offsetof(structname, member), \
418 sizeof(virtio_cread_v)); \
421 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \
424 /* Config space accessors. */
425 #define virtio_cwrite(vdev, structname, member, ptr) \
427 typeof(((structname*)0)->member) virtio_cwrite_v = \
428 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
431 /* Sanity check: must match the member's type */ \
432 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
434 vdev->config->set((vdev), offsetof(structname, member), \
436 sizeof(virtio_cwrite_v)); \
440 * Nothing virtio-specific about these, but let's worry about generalizing
443 #define virtio_le_to_cpu(x) \
446 __le16: (u16)le16_to_cpu(x), \
447 __le32: (u32)le32_to_cpu(x), \
448 __le64: (u64)le64_to_cpu(x) \
451 #define virtio_cpu_to_le(x, m) \
454 __le16: cpu_to_le16(x), \
455 __le32: cpu_to_le32(x), \
456 __le64: cpu_to_le64(x) \
459 /* LE (e.g. modern) Config space accessors. */
460 #define virtio_cread_le(vdev, structname, member, ptr) \
462 typeof(((structname*)0)->member) virtio_cread_v; \
465 /* Sanity check: must match the member's type */ \
466 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
468 switch (sizeof(virtio_cread_v)) { \
472 vdev->config->get((vdev), \
473 offsetof(structname, member), \
475 sizeof(virtio_cread_v)); \
478 __virtio_cread_many((vdev), \
479 offsetof(structname, member), \
482 sizeof(virtio_cread_v)); \
485 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \
488 #define virtio_cwrite_le(vdev, structname, member, ptr) \
490 typeof(((structname*)0)->member) virtio_cwrite_v = \
491 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
494 /* Sanity check: must match the member's type */ \
495 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
497 vdev->config->set((vdev), offsetof(structname, member), \
499 sizeof(virtio_cwrite_v)); \
503 /* Read @count fields, @bytes each. */
504 static inline void __virtio_cread_many(struct virtio_device *vdev,
506 void *buf, size_t count, size_t bytes)
508 u32 old, gen = vdev->config->generation ?
509 vdev->config->generation(vdev) : 0;
516 for (i = 0; i < count; i++)
517 vdev->config->get(vdev, offset + bytes * i,
518 buf + i * bytes, bytes);
520 gen = vdev->config->generation ?
521 vdev->config->generation(vdev) : 0;
522 } while (gen != old);
525 static inline void virtio_cread_bytes(struct virtio_device *vdev,
527 void *buf, size_t len)
529 __virtio_cread_many(vdev, offset, buf, len, 1);
532 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
537 vdev->config->get(vdev, offset, &ret, sizeof(ret));
541 static inline void virtio_cwrite8(struct virtio_device *vdev,
542 unsigned int offset, u8 val)
545 vdev->config->set(vdev, offset, &val, sizeof(val));
548 static inline u16 virtio_cread16(struct virtio_device *vdev,
554 vdev->config->get(vdev, offset, &ret, sizeof(ret));
555 return virtio16_to_cpu(vdev, ret);
558 static inline void virtio_cwrite16(struct virtio_device *vdev,
559 unsigned int offset, u16 val)
564 v = cpu_to_virtio16(vdev, val);
565 vdev->config->set(vdev, offset, &v, sizeof(v));
568 static inline u32 virtio_cread32(struct virtio_device *vdev,
574 vdev->config->get(vdev, offset, &ret, sizeof(ret));
575 return virtio32_to_cpu(vdev, ret);
578 static inline void virtio_cwrite32(struct virtio_device *vdev,
579 unsigned int offset, u32 val)
584 v = cpu_to_virtio32(vdev, val);
585 vdev->config->set(vdev, offset, &v, sizeof(v));
588 static inline u64 virtio_cread64(struct virtio_device *vdev,
593 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
594 return virtio64_to_cpu(vdev, ret);
597 static inline void virtio_cwrite64(struct virtio_device *vdev,
598 unsigned int offset, u64 val)
603 v = cpu_to_virtio64(vdev, val);
604 vdev->config->set(vdev, offset, &v, sizeof(v));
607 /* Conditional config space accessors. */
608 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
611 if (!virtio_has_feature(vdev, fbit)) \
614 virtio_cread((vdev), structname, member, ptr); \
618 /* Conditional config space accessors. */
619 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \
622 if (!virtio_has_feature(vdev, fbit)) \
625 virtio_cread_le((vdev), structname, member, ptr); \
629 #endif /* _LINUX_VIRTIO_CONFIG_H */