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 * @get_features: get the array of feature bits for this device.
61 * vdev: the virtio_device
62 * Returns the first 64 feature bits (all we currently need).
63 * @finalize_features: confirm what device features we'll be using.
64 * vdev: the virtio_device
65 * This sends the driver feature bits to the device: it can change
66 * the dev->feature bits if it wants.
67 * Note: despite the name this can be called any number of times.
68 * Returns 0 on success or error status
69 * @bus_name: return the bus name associated with the device (optional)
70 * vdev: the virtio_device
71 * This returns a pointer to the bus name a la pci_name from which
72 * the caller can then copy.
73 * @set_vq_affinity: set the affinity for a virtqueue (optional).
74 * @get_vq_affinity: get the affinity for a virtqueue (optional).
75 * @get_shm_region: get a shared memory region based on the index.
77 typedef void vq_callback_t(struct virtqueue *);
78 struct virtio_config_ops {
79 void (*get)(struct virtio_device *vdev, unsigned offset,
80 void *buf, unsigned len);
81 void (*set)(struct virtio_device *vdev, unsigned offset,
82 const void *buf, unsigned len);
83 u32 (*generation)(struct virtio_device *vdev);
84 u8 (*get_status)(struct virtio_device *vdev);
85 void (*set_status)(struct virtio_device *vdev, u8 status);
86 void (*reset)(struct virtio_device *vdev);
87 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
88 struct virtqueue *vqs[], vq_callback_t *callbacks[],
89 const char * const names[], const bool *ctx,
90 struct irq_affinity *desc);
91 void (*del_vqs)(struct virtio_device *);
92 u64 (*get_features)(struct virtio_device *vdev);
93 int (*finalize_features)(struct virtio_device *vdev);
94 const char *(*bus_name)(struct virtio_device *vdev);
95 int (*set_vq_affinity)(struct virtqueue *vq,
96 const struct cpumask *cpu_mask);
97 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
99 bool (*get_shm_region)(struct virtio_device *vdev,
100 struct virtio_shm_region *region, u8 id);
103 /* If driver didn't advertise the feature, it will never appear. */
104 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
108 * __virtio_test_bit - helper to test feature bits. For use by transports.
109 * Devices should normally use virtio_has_feature,
110 * which includes more checks.
112 * @fbit: the feature bit
114 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
117 /* Did you forget to fix assumptions on max features? */
118 if (__builtin_constant_p(fbit))
119 BUILD_BUG_ON(fbit >= 64);
123 return vdev->features & BIT_ULL(fbit);
127 * __virtio_set_bit - helper to set feature bits. For use by transports.
129 * @fbit: the feature bit
131 static inline void __virtio_set_bit(struct virtio_device *vdev,
134 /* Did you forget to fix assumptions on max features? */
135 if (__builtin_constant_p(fbit))
136 BUILD_BUG_ON(fbit >= 64);
140 vdev->features |= BIT_ULL(fbit);
144 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
146 * @fbit: the feature bit
148 static inline void __virtio_clear_bit(struct virtio_device *vdev,
151 /* Did you forget to fix assumptions on max features? */
152 if (__builtin_constant_p(fbit))
153 BUILD_BUG_ON(fbit >= 64);
157 vdev->features &= ~BIT_ULL(fbit);
161 * virtio_has_feature - helper to determine if this device has this feature.
163 * @fbit: the feature bit
165 static inline bool virtio_has_feature(const struct virtio_device *vdev,
168 if (fbit < VIRTIO_TRANSPORT_F_START)
169 virtio_check_driver_offered_feature(vdev, fbit);
171 return __virtio_test_bit(vdev, fbit);
175 * virtio_has_dma_quirk - determine whether this device has the DMA quirk
178 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
181 * Note the reverse polarity of the quirk feature (compared to most
182 * other features), this is for compatibility with legacy systems.
184 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
188 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
189 vq_callback_t *c, const char *n)
191 vq_callback_t *callbacks[] = { c };
192 const char *names[] = { n };
193 struct virtqueue *vq;
194 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
202 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
203 struct virtqueue *vqs[], vq_callback_t *callbacks[],
204 const char * const names[],
205 struct irq_affinity *desc)
207 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
211 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
212 struct virtqueue *vqs[], vq_callback_t *callbacks[],
213 const char * const names[], const bool *ctx,
214 struct irq_affinity *desc)
216 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
221 * virtio_device_ready - enable vq use in probe function
224 * Driver must call this to use vqs in the probe function.
226 * Note: vqs are enabled automatically after probe returns.
229 void virtio_device_ready(struct virtio_device *dev)
231 unsigned status = dev->config->get_status(dev);
233 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
234 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
238 const char *virtio_bus_name(struct virtio_device *vdev)
240 if (!vdev->config->bus_name)
242 return vdev->config->bus_name(vdev);
246 * virtqueue_set_affinity - setting affinity for a virtqueue
250 * Pay attention the function are best-effort: the affinity hint may not be set
251 * due to config support, irq type and sharing.
255 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
257 struct virtio_device *vdev = vq->vdev;
258 if (vdev->config->set_vq_affinity)
259 return vdev->config->set_vq_affinity(vq, cpu_mask);
264 bool virtio_get_shm_region(struct virtio_device *vdev,
265 struct virtio_shm_region *region, u8 id)
267 if (!vdev->config->get_shm_region)
269 return vdev->config->get_shm_region(vdev, region, id);
272 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
274 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
275 virtio_legacy_is_little_endian();
278 /* Memory accessors */
279 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
281 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
284 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
286 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
289 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
291 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
294 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
296 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
299 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
301 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
304 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
306 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
309 #define virtio_to_cpu(vdev, x) \
312 __virtio16: virtio16_to_cpu((vdev), (x)), \
313 __virtio32: virtio32_to_cpu((vdev), (x)), \
314 __virtio64: virtio64_to_cpu((vdev), (x)) \
317 #define cpu_to_virtio(vdev, x, m) \
320 __virtio16: cpu_to_virtio16((vdev), (x)), \
321 __virtio32: cpu_to_virtio32((vdev), (x)), \
322 __virtio64: cpu_to_virtio64((vdev), (x)) \
325 #define __virtio_native_type(structname, member) \
326 typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
328 /* Config space accessors. */
329 #define virtio_cread(vdev, structname, member, ptr) \
331 typeof(((structname*)0)->member) virtio_cread_v; \
334 /* Sanity check: must match the member's type */ \
335 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
337 switch (sizeof(virtio_cread_v)) { \
341 vdev->config->get((vdev), \
342 offsetof(structname, member), \
344 sizeof(virtio_cread_v)); \
347 __virtio_cread_many((vdev), \
348 offsetof(structname, member), \
351 sizeof(virtio_cread_v)); \
354 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \
357 /* Config space accessors. */
358 #define virtio_cwrite(vdev, structname, member, ptr) \
360 typeof(((structname*)0)->member) virtio_cwrite_v = \
361 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
364 /* Sanity check: must match the member's type */ \
365 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
367 vdev->config->set((vdev), offsetof(structname, member), \
369 sizeof(virtio_cwrite_v)); \
373 * Nothing virtio-specific about these, but let's worry about generalizing
376 #define virtio_le_to_cpu(x) \
379 __le16: (u16)le16_to_cpu(x), \
380 __le32: (u32)le32_to_cpu(x), \
381 __le64: (u64)le64_to_cpu(x) \
384 #define virtio_cpu_to_le(x, m) \
387 __le16: cpu_to_le16(x), \
388 __le32: cpu_to_le32(x), \
389 __le64: cpu_to_le64(x) \
392 /* LE (e.g. modern) Config space accessors. */
393 #define virtio_cread_le(vdev, structname, member, ptr) \
395 typeof(((structname*)0)->member) virtio_cread_v; \
398 /* Sanity check: must match the member's type */ \
399 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
401 switch (sizeof(virtio_cread_v)) { \
405 vdev->config->get((vdev), \
406 offsetof(structname, member), \
408 sizeof(virtio_cread_v)); \
411 __virtio_cread_many((vdev), \
412 offsetof(structname, member), \
415 sizeof(virtio_cread_v)); \
418 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \
421 #define virtio_cwrite_le(vdev, structname, member, ptr) \
423 typeof(((structname*)0)->member) virtio_cwrite_v = \
424 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
427 /* Sanity check: must match the member's type */ \
428 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
430 vdev->config->set((vdev), offsetof(structname, member), \
432 sizeof(virtio_cwrite_v)); \
436 /* Read @count fields, @bytes each. */
437 static inline void __virtio_cread_many(struct virtio_device *vdev,
439 void *buf, size_t count, size_t bytes)
441 u32 old, gen = vdev->config->generation ?
442 vdev->config->generation(vdev) : 0;
449 for (i = 0; i < count; i++)
450 vdev->config->get(vdev, offset + bytes * i,
451 buf + i * bytes, bytes);
453 gen = vdev->config->generation ?
454 vdev->config->generation(vdev) : 0;
455 } while (gen != old);
458 static inline void virtio_cread_bytes(struct virtio_device *vdev,
460 void *buf, size_t len)
462 __virtio_cread_many(vdev, offset, buf, len, 1);
465 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
470 vdev->config->get(vdev, offset, &ret, sizeof(ret));
474 static inline void virtio_cwrite8(struct virtio_device *vdev,
475 unsigned int offset, u8 val)
478 vdev->config->set(vdev, offset, &val, sizeof(val));
481 static inline u16 virtio_cread16(struct virtio_device *vdev,
487 vdev->config->get(vdev, offset, &ret, sizeof(ret));
488 return virtio16_to_cpu(vdev, ret);
491 static inline void virtio_cwrite16(struct virtio_device *vdev,
492 unsigned int offset, u16 val)
497 v = cpu_to_virtio16(vdev, val);
498 vdev->config->set(vdev, offset, &v, sizeof(v));
501 static inline u32 virtio_cread32(struct virtio_device *vdev,
507 vdev->config->get(vdev, offset, &ret, sizeof(ret));
508 return virtio32_to_cpu(vdev, ret);
511 static inline void virtio_cwrite32(struct virtio_device *vdev,
512 unsigned int offset, u32 val)
517 v = cpu_to_virtio32(vdev, val);
518 vdev->config->set(vdev, offset, &v, sizeof(v));
521 static inline u64 virtio_cread64(struct virtio_device *vdev,
526 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
527 return virtio64_to_cpu(vdev, ret);
530 static inline void virtio_cwrite64(struct virtio_device *vdev,
531 unsigned int offset, u64 val)
536 v = cpu_to_virtio64(vdev, val);
537 vdev->config->set(vdev, offset, &v, sizeof(v));
540 /* Conditional config space accessors. */
541 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
544 if (!virtio_has_feature(vdev, fbit)) \
547 virtio_cread((vdev), structname, member, ptr); \
551 /* Conditional config space accessors. */
552 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \
555 if (!virtio_has_feature(vdev, fbit)) \
558 virtio_cread_le((vdev), structname, member, ptr); \
562 #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
563 int arch_has_restricted_virtio_memory_access(void);
565 static inline int arch_has_restricted_virtio_memory_access(void)
569 #endif /* CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS */
571 #endif /* _LINUX_VIRTIO_CONFIG_H */