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 sends the driver feature bits to the device: it can change
68 * the dev->feature bits if it wants.
69 * Note: despite the name this can be called any number of times.
70 * Returns 0 on success or error status
71 * @bus_name: return the bus name associated with the device (optional)
72 * vdev: the virtio_device
73 * This returns a pointer to the bus name a la pci_name from which
74 * the caller can then copy.
75 * @set_vq_affinity: set the affinity for a virtqueue (optional).
76 * @get_vq_affinity: get the affinity for a virtqueue (optional).
77 * @get_shm_region: get a shared memory region based on the index.
79 typedef void vq_callback_t(struct virtqueue *);
80 struct virtio_config_ops {
81 void (*enable_cbs)(struct virtio_device *vdev);
82 void (*get)(struct virtio_device *vdev, unsigned offset,
83 void *buf, unsigned len);
84 void (*set)(struct virtio_device *vdev, unsigned offset,
85 const void *buf, unsigned len);
86 u32 (*generation)(struct virtio_device *vdev);
87 u8 (*get_status)(struct virtio_device *vdev);
88 void (*set_status)(struct virtio_device *vdev, u8 status);
89 void (*reset)(struct virtio_device *vdev);
90 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
91 struct virtqueue *vqs[], vq_callback_t *callbacks[],
92 const char * const names[], const bool *ctx,
93 struct irq_affinity *desc);
94 void (*del_vqs)(struct virtio_device *);
95 u64 (*get_features)(struct virtio_device *vdev);
96 int (*finalize_features)(struct virtio_device *vdev);
97 const char *(*bus_name)(struct virtio_device *vdev);
98 int (*set_vq_affinity)(struct virtqueue *vq,
99 const struct cpumask *cpu_mask);
100 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
102 bool (*get_shm_region)(struct virtio_device *vdev,
103 struct virtio_shm_region *region, u8 id);
106 /* If driver didn't advertise the feature, it will never appear. */
107 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
111 * __virtio_test_bit - helper to test feature bits. For use by transports.
112 * Devices should normally use virtio_has_feature,
113 * which includes more checks.
115 * @fbit: the feature bit
117 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
120 /* Did you forget to fix assumptions on max features? */
121 if (__builtin_constant_p(fbit))
122 BUILD_BUG_ON(fbit >= 64);
126 return vdev->features & BIT_ULL(fbit);
130 * __virtio_set_bit - helper to set feature bits. For use by transports.
132 * @fbit: the feature bit
134 static inline void __virtio_set_bit(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 vdev->features |= BIT_ULL(fbit);
147 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
149 * @fbit: the feature bit
151 static inline void __virtio_clear_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_has_feature - helper to determine if this device has this feature.
166 * @fbit: the feature bit
168 static inline bool virtio_has_feature(const struct virtio_device *vdev,
171 if (fbit < VIRTIO_TRANSPORT_F_START)
172 virtio_check_driver_offered_feature(vdev, fbit);
174 return __virtio_test_bit(vdev, fbit);
178 * virtio_has_dma_quirk - determine whether this device has the DMA quirk
181 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
184 * Note the reverse polarity of the quirk feature (compared to most
185 * other features), this is for compatibility with legacy systems.
187 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
191 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
192 vq_callback_t *c, const char *n)
194 vq_callback_t *callbacks[] = { c };
195 const char *names[] = { n };
196 struct virtqueue *vq;
197 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
205 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
206 struct virtqueue *vqs[], vq_callback_t *callbacks[],
207 const char * const names[],
208 struct irq_affinity *desc)
210 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
214 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
215 struct virtqueue *vqs[], vq_callback_t *callbacks[],
216 const char * const names[], const bool *ctx,
217 struct irq_affinity *desc)
219 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
224 * virtio_device_ready - enable vq use in probe function
227 * Driver must call this to use vqs in the probe function.
229 * Note: vqs are enabled automatically after probe returns.
232 void virtio_device_ready(struct virtio_device *dev)
234 unsigned status = dev->config->get_status(dev);
236 if (dev->config->enable_cbs)
237 dev->config->enable_cbs(dev);
239 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
240 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
244 const char *virtio_bus_name(struct virtio_device *vdev)
246 if (!vdev->config->bus_name)
248 return vdev->config->bus_name(vdev);
252 * virtqueue_set_affinity - setting affinity for a virtqueue
256 * Pay attention the function are best-effort: the affinity hint may not be set
257 * due to config support, irq type and sharing.
261 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
263 struct virtio_device *vdev = vq->vdev;
264 if (vdev->config->set_vq_affinity)
265 return vdev->config->set_vq_affinity(vq, cpu_mask);
270 bool virtio_get_shm_region(struct virtio_device *vdev,
271 struct virtio_shm_region *region, u8 id)
273 if (!vdev->config->get_shm_region)
275 return vdev->config->get_shm_region(vdev, region, id);
278 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
280 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
281 virtio_legacy_is_little_endian();
284 /* Memory accessors */
285 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
287 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
290 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
292 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
295 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
297 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
300 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
302 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
305 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
307 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
310 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
312 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
315 #define virtio_to_cpu(vdev, x) \
318 __virtio16: virtio16_to_cpu((vdev), (x)), \
319 __virtio32: virtio32_to_cpu((vdev), (x)), \
320 __virtio64: virtio64_to_cpu((vdev), (x)) \
323 #define cpu_to_virtio(vdev, x, m) \
326 __virtio16: cpu_to_virtio16((vdev), (x)), \
327 __virtio32: cpu_to_virtio32((vdev), (x)), \
328 __virtio64: cpu_to_virtio64((vdev), (x)) \
331 #define __virtio_native_type(structname, member) \
332 typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
334 /* Config space accessors. */
335 #define virtio_cread(vdev, structname, member, ptr) \
337 typeof(((structname*)0)->member) virtio_cread_v; \
340 /* Sanity check: must match the member's type */ \
341 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
343 switch (sizeof(virtio_cread_v)) { \
347 vdev->config->get((vdev), \
348 offsetof(structname, member), \
350 sizeof(virtio_cread_v)); \
353 __virtio_cread_many((vdev), \
354 offsetof(structname, member), \
357 sizeof(virtio_cread_v)); \
360 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \
363 /* Config space accessors. */
364 #define virtio_cwrite(vdev, structname, member, ptr) \
366 typeof(((structname*)0)->member) virtio_cwrite_v = \
367 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
370 /* Sanity check: must match the member's type */ \
371 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
373 vdev->config->set((vdev), offsetof(structname, member), \
375 sizeof(virtio_cwrite_v)); \
379 * Nothing virtio-specific about these, but let's worry about generalizing
382 #define virtio_le_to_cpu(x) \
385 __le16: (u16)le16_to_cpu(x), \
386 __le32: (u32)le32_to_cpu(x), \
387 __le64: (u64)le64_to_cpu(x) \
390 #define virtio_cpu_to_le(x, m) \
393 __le16: cpu_to_le16(x), \
394 __le32: cpu_to_le32(x), \
395 __le64: cpu_to_le64(x) \
398 /* LE (e.g. modern) Config space accessors. */
399 #define virtio_cread_le(vdev, structname, member, ptr) \
401 typeof(((structname*)0)->member) virtio_cread_v; \
404 /* Sanity check: must match the member's type */ \
405 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
407 switch (sizeof(virtio_cread_v)) { \
411 vdev->config->get((vdev), \
412 offsetof(structname, member), \
414 sizeof(virtio_cread_v)); \
417 __virtio_cread_many((vdev), \
418 offsetof(structname, member), \
421 sizeof(virtio_cread_v)); \
424 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \
427 #define virtio_cwrite_le(vdev, structname, member, ptr) \
429 typeof(((structname*)0)->member) virtio_cwrite_v = \
430 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
433 /* Sanity check: must match the member's type */ \
434 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
436 vdev->config->set((vdev), offsetof(structname, member), \
438 sizeof(virtio_cwrite_v)); \
442 /* Read @count fields, @bytes each. */
443 static inline void __virtio_cread_many(struct virtio_device *vdev,
445 void *buf, size_t count, size_t bytes)
447 u32 old, gen = vdev->config->generation ?
448 vdev->config->generation(vdev) : 0;
455 for (i = 0; i < count; i++)
456 vdev->config->get(vdev, offset + bytes * i,
457 buf + i * bytes, bytes);
459 gen = vdev->config->generation ?
460 vdev->config->generation(vdev) : 0;
461 } while (gen != old);
464 static inline void virtio_cread_bytes(struct virtio_device *vdev,
466 void *buf, size_t len)
468 __virtio_cread_many(vdev, offset, buf, len, 1);
471 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
476 vdev->config->get(vdev, offset, &ret, sizeof(ret));
480 static inline void virtio_cwrite8(struct virtio_device *vdev,
481 unsigned int offset, u8 val)
484 vdev->config->set(vdev, offset, &val, sizeof(val));
487 static inline u16 virtio_cread16(struct virtio_device *vdev,
493 vdev->config->get(vdev, offset, &ret, sizeof(ret));
494 return virtio16_to_cpu(vdev, ret);
497 static inline void virtio_cwrite16(struct virtio_device *vdev,
498 unsigned int offset, u16 val)
503 v = cpu_to_virtio16(vdev, val);
504 vdev->config->set(vdev, offset, &v, sizeof(v));
507 static inline u32 virtio_cread32(struct virtio_device *vdev,
513 vdev->config->get(vdev, offset, &ret, sizeof(ret));
514 return virtio32_to_cpu(vdev, ret);
517 static inline void virtio_cwrite32(struct virtio_device *vdev,
518 unsigned int offset, u32 val)
523 v = cpu_to_virtio32(vdev, val);
524 vdev->config->set(vdev, offset, &v, sizeof(v));
527 static inline u64 virtio_cread64(struct virtio_device *vdev,
532 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
533 return virtio64_to_cpu(vdev, ret);
536 static inline void virtio_cwrite64(struct virtio_device *vdev,
537 unsigned int offset, u64 val)
542 v = cpu_to_virtio64(vdev, val);
543 vdev->config->set(vdev, offset, &v, sizeof(v));
546 /* Conditional config space accessors. */
547 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
550 if (!virtio_has_feature(vdev, fbit)) \
553 virtio_cread((vdev), structname, member, ptr); \
557 /* Conditional config space accessors. */
558 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \
561 if (!virtio_has_feature(vdev, fbit)) \
564 virtio_cread_le((vdev), structname, member, ptr); \
568 #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
569 int arch_has_restricted_virtio_memory_access(void);
571 static inline int arch_has_restricted_virtio_memory_access(void)
575 #endif /* CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS */
577 #endif /* _LINUX_VIRTIO_CONFIG_H */