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 * @enable_cbs: enable the callbacks
27 * vdev: the virtio_device
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 * @get_features: get the array of feature bits for this device.
63 * vdev: the virtio_device
64 * Returns the first 64 feature bits (all we currently need).
65 * @finalize_features: confirm what device features we'll be using.
66 * vdev: the virtio_device
67 * This gives the final feature bits for the device: it can change
68 * the dev->feature bits if it wants.
69 * Returns 0 on success or error status
70 * @bus_name: return the bus name associated with the device (optional)
71 * vdev: the virtio_device
72 * This returns a pointer to the bus name a la pci_name from which
73 * the caller can then copy.
74 * @set_vq_affinity: set the affinity for a virtqueue (optional).
75 * @get_vq_affinity: get the affinity for a virtqueue (optional).
76 * @get_shm_region: get a shared memory region based on the index.
78 typedef void vq_callback_t(struct virtqueue *);
79 struct virtio_config_ops {
80 void (*enable_cbs)(struct virtio_device *vdev);
81 void (*get)(struct virtio_device *vdev, unsigned offset,
82 void *buf, unsigned len);
83 void (*set)(struct virtio_device *vdev, unsigned offset,
84 const void *buf, unsigned len);
85 u32 (*generation)(struct virtio_device *vdev);
86 u8 (*get_status)(struct virtio_device *vdev);
87 void (*set_status)(struct virtio_device *vdev, u8 status);
88 void (*reset)(struct virtio_device *vdev);
89 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
90 struct virtqueue *vqs[], vq_callback_t *callbacks[],
91 const char * const names[], const bool *ctx,
92 struct irq_affinity *desc);
93 void (*del_vqs)(struct virtio_device *);
94 u64 (*get_features)(struct virtio_device *vdev);
95 int (*finalize_features)(struct virtio_device *vdev);
96 const char *(*bus_name)(struct virtio_device *vdev);
97 int (*set_vq_affinity)(struct virtqueue *vq,
98 const struct cpumask *cpu_mask);
99 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
101 bool (*get_shm_region)(struct virtio_device *vdev,
102 struct virtio_shm_region *region, u8 id);
105 /* If driver didn't advertise the feature, it will never appear. */
106 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
110 * __virtio_test_bit - helper to test feature bits. For use by transports.
111 * Devices should normally use virtio_has_feature,
112 * which includes more checks.
114 * @fbit: the feature bit
116 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
119 /* Did you forget to fix assumptions on max features? */
120 if (__builtin_constant_p(fbit))
121 BUILD_BUG_ON(fbit >= 64);
125 return vdev->features & BIT_ULL(fbit);
129 * __virtio_set_bit - helper to set feature bits. For use by transports.
131 * @fbit: the feature bit
133 static inline void __virtio_set_bit(struct virtio_device *vdev,
136 /* Did you forget to fix assumptions on max features? */
137 if (__builtin_constant_p(fbit))
138 BUILD_BUG_ON(fbit >= 64);
142 vdev->features |= BIT_ULL(fbit);
146 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
148 * @fbit: the feature bit
150 static inline void __virtio_clear_bit(struct virtio_device *vdev,
153 /* Did you forget to fix assumptions on max features? */
154 if (__builtin_constant_p(fbit))
155 BUILD_BUG_ON(fbit >= 64);
159 vdev->features &= ~BIT_ULL(fbit);
163 * virtio_has_feature - helper to determine if this device has this feature.
165 * @fbit: the feature bit
167 static inline bool virtio_has_feature(const struct virtio_device *vdev,
170 if (fbit < VIRTIO_TRANSPORT_F_START)
171 virtio_check_driver_offered_feature(vdev, fbit);
173 return __virtio_test_bit(vdev, fbit);
177 * virtio_has_dma_quirk - determine whether this device has the DMA quirk
180 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
183 * Note the reverse polarity of the quirk feature (compared to most
184 * other features), this is for compatibility with legacy systems.
186 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
190 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
191 vq_callback_t *c, const char *n)
193 vq_callback_t *callbacks[] = { c };
194 const char *names[] = { n };
195 struct virtqueue *vq;
196 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
204 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
205 struct virtqueue *vqs[], vq_callback_t *callbacks[],
206 const char * const names[],
207 struct irq_affinity *desc)
209 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
213 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
214 struct virtqueue *vqs[], vq_callback_t *callbacks[],
215 const char * const names[], const bool *ctx,
216 struct irq_affinity *desc)
218 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
223 * virtio_device_ready - enable vq use in probe function
226 * Driver must call this to use vqs in the probe function.
228 * Note: vqs are enabled automatically after probe returns.
231 void virtio_device_ready(struct virtio_device *dev)
233 unsigned status = dev->config->get_status(dev);
235 if (dev->config->enable_cbs)
236 dev->config->enable_cbs(dev);
238 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
239 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
243 const char *virtio_bus_name(struct virtio_device *vdev)
245 if (!vdev->config->bus_name)
247 return vdev->config->bus_name(vdev);
251 * virtqueue_set_affinity - setting affinity for a virtqueue
255 * Pay attention the function are best-effort: the affinity hint may not be set
256 * due to config support, irq type and sharing.
260 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
262 struct virtio_device *vdev = vq->vdev;
263 if (vdev->config->set_vq_affinity)
264 return vdev->config->set_vq_affinity(vq, cpu_mask);
269 bool virtio_get_shm_region(struct virtio_device *vdev,
270 struct virtio_shm_region *region, u8 id)
272 if (!vdev->config->get_shm_region)
274 return vdev->config->get_shm_region(vdev, region, id);
277 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
279 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
280 virtio_legacy_is_little_endian();
283 /* Memory accessors */
284 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
286 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
289 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
291 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
294 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
296 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
299 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
301 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
304 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
306 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
309 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
311 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
314 #define virtio_to_cpu(vdev, x) \
317 __virtio16: virtio16_to_cpu((vdev), (x)), \
318 __virtio32: virtio32_to_cpu((vdev), (x)), \
319 __virtio64: virtio64_to_cpu((vdev), (x)) \
322 #define cpu_to_virtio(vdev, x, m) \
325 __virtio16: cpu_to_virtio16((vdev), (x)), \
326 __virtio32: cpu_to_virtio32((vdev), (x)), \
327 __virtio64: cpu_to_virtio64((vdev), (x)) \
330 #define __virtio_native_type(structname, member) \
331 typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
333 /* Config space accessors. */
334 #define virtio_cread(vdev, structname, member, ptr) \
336 typeof(((structname*)0)->member) virtio_cread_v; \
339 /* Sanity check: must match the member's type */ \
340 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
342 switch (sizeof(virtio_cread_v)) { \
346 vdev->config->get((vdev), \
347 offsetof(structname, member), \
349 sizeof(virtio_cread_v)); \
352 __virtio_cread_many((vdev), \
353 offsetof(structname, member), \
356 sizeof(virtio_cread_v)); \
359 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \
362 /* Config space accessors. */
363 #define virtio_cwrite(vdev, structname, member, ptr) \
365 typeof(((structname*)0)->member) virtio_cwrite_v = \
366 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
369 /* Sanity check: must match the member's type */ \
370 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
372 vdev->config->set((vdev), offsetof(structname, member), \
374 sizeof(virtio_cwrite_v)); \
378 * Nothing virtio-specific about these, but let's worry about generalizing
381 #define virtio_le_to_cpu(x) \
384 __le16: (u16)le16_to_cpu(x), \
385 __le32: (u32)le32_to_cpu(x), \
386 __le64: (u64)le64_to_cpu(x) \
389 #define virtio_cpu_to_le(x, m) \
392 __le16: cpu_to_le16(x), \
393 __le32: cpu_to_le32(x), \
394 __le64: cpu_to_le64(x) \
397 /* LE (e.g. modern) Config space accessors. */
398 #define virtio_cread_le(vdev, structname, member, ptr) \
400 typeof(((structname*)0)->member) virtio_cread_v; \
403 /* Sanity check: must match the member's type */ \
404 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
406 switch (sizeof(virtio_cread_v)) { \
410 vdev->config->get((vdev), \
411 offsetof(structname, member), \
413 sizeof(virtio_cread_v)); \
416 __virtio_cread_many((vdev), \
417 offsetof(structname, member), \
420 sizeof(virtio_cread_v)); \
423 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \
426 #define virtio_cwrite_le(vdev, structname, member, ptr) \
428 typeof(((structname*)0)->member) virtio_cwrite_v = \
429 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
432 /* Sanity check: must match the member's type */ \
433 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
435 vdev->config->set((vdev), offsetof(structname, member), \
437 sizeof(virtio_cwrite_v)); \
441 /* Read @count fields, @bytes each. */
442 static inline void __virtio_cread_many(struct virtio_device *vdev,
444 void *buf, size_t count, size_t bytes)
446 u32 old, gen = vdev->config->generation ?
447 vdev->config->generation(vdev) : 0;
454 for (i = 0; i < count; i++)
455 vdev->config->get(vdev, offset + bytes * i,
456 buf + i * bytes, bytes);
458 gen = vdev->config->generation ?
459 vdev->config->generation(vdev) : 0;
460 } while (gen != old);
463 static inline void virtio_cread_bytes(struct virtio_device *vdev,
465 void *buf, size_t len)
467 __virtio_cread_many(vdev, offset, buf, len, 1);
470 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
475 vdev->config->get(vdev, offset, &ret, sizeof(ret));
479 static inline void virtio_cwrite8(struct virtio_device *vdev,
480 unsigned int offset, u8 val)
483 vdev->config->set(vdev, offset, &val, sizeof(val));
486 static inline u16 virtio_cread16(struct virtio_device *vdev,
492 vdev->config->get(vdev, offset, &ret, sizeof(ret));
493 return virtio16_to_cpu(vdev, ret);
496 static inline void virtio_cwrite16(struct virtio_device *vdev,
497 unsigned int offset, u16 val)
502 v = cpu_to_virtio16(vdev, val);
503 vdev->config->set(vdev, offset, &v, sizeof(v));
506 static inline u32 virtio_cread32(struct virtio_device *vdev,
512 vdev->config->get(vdev, offset, &ret, sizeof(ret));
513 return virtio32_to_cpu(vdev, ret);
516 static inline void virtio_cwrite32(struct virtio_device *vdev,
517 unsigned int offset, u32 val)
522 v = cpu_to_virtio32(vdev, val);
523 vdev->config->set(vdev, offset, &v, sizeof(v));
526 static inline u64 virtio_cread64(struct virtio_device *vdev,
531 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
532 return virtio64_to_cpu(vdev, ret);
535 static inline void virtio_cwrite64(struct virtio_device *vdev,
536 unsigned int offset, u64 val)
541 v = cpu_to_virtio64(vdev, val);
542 vdev->config->set(vdev, offset, &v, sizeof(v));
545 /* Conditional config space accessors. */
546 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
549 if (!virtio_has_feature(vdev, fbit)) \
552 virtio_cread((vdev), structname, member, ptr); \
556 /* Conditional config space accessors. */
557 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \
560 if (!virtio_has_feature(vdev, fbit)) \
563 virtio_cread_le((vdev), structname, member, ptr); \
567 #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
568 int arch_has_restricted_virtio_memory_access(void);
570 static inline int arch_has_restricted_virtio_memory_access(void)
574 #endif /* CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS */
576 #endif /* _LINUX_VIRTIO_CONFIG_H */