1 /* SPDX-License-Identifier: GPL-2.0+ */
6 * VirtIO is a virtualization standard for network and disk device drivers
7 * where just the guest's device driver "knows" it is running in a virtual
8 * environment, and cooperates with the hypervisor. This enables guests to
9 * get high performance network and disk operations, and gives most of the
10 * performance benefits of paravirtualization. In the U-Boot case, the guest
11 * is U-Boot itself, while the virtual environment are normally QEMU targets
12 * like ARM, RISC-V and x86.
14 * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
15 * the VirtIO specification v1.0.
17 * This file is largely based on Linux kernel virtio_*.h files
23 #include <virtio_types.h>
24 #include <dm/device.h>
25 #include <linux/bitops.h>
26 #include <linux/bug.h>
27 #include <linux/typecheck.h>
28 #define VIRTIO_ID_NET 1 /* virtio net */
29 #define VIRTIO_ID_BLOCK 2 /* virtio block */
30 #define VIRTIO_ID_RNG 4 /* virtio rng */
31 #define VIRTIO_ID_MAX_NUM 5
33 #define VIRTIO_NET_DRV_NAME "virtio-net"
34 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
35 #define VIRTIO_RNG_DRV_NAME "virtio-rng"
37 /* Status byte for guest to report progress, and synchronize features */
39 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
40 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
41 /* We have found a driver for the device */
42 #define VIRTIO_CONFIG_S_DRIVER 2
43 /* Driver has used its parts of the config, and is happy */
44 #define VIRTIO_CONFIG_S_DRIVER_OK 4
45 /* Driver has finished configuring features */
46 #define VIRTIO_CONFIG_S_FEATURES_OK 8
47 /* Device entered invalid state, driver must reset it */
48 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
49 /* We've given up on this device */
50 #define VIRTIO_CONFIG_S_FAILED 0x80
53 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
54 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
55 * the rest are per-device feature bits.
57 #define VIRTIO_TRANSPORT_F_START 28
58 #define VIRTIO_TRANSPORT_F_END 38
60 #ifndef VIRTIO_CONFIG_NO_LEGACY
62 * Do we get callbacks when the ring is completely used,
63 * even if we've suppressed them?
65 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
67 /* Can the device handle any descriptor layout? */
68 #define VIRTIO_F_ANY_LAYOUT 27
69 #endif /* VIRTIO_CONFIG_NO_LEGACY */
72 #define VIRTIO_F_VERSION_1 32
75 * If clear - device has the IOMMU bypass quirk feature.
76 * If set - use platform tools to detect the IOMMU.
78 * Note the reverse polarity (compared to most other features),
79 * this is for compatibility with legacy systems.
81 #define VIRTIO_F_IOMMU_PLATFORM 33
83 /* Does the device support Single Root I/O Virtualization? */
84 #define VIRTIO_F_SR_IOV 37
87 * virtio scatter-gather struct
89 * @addr: sg buffer address
90 * @lengh: sg buffer length
99 /* virtio bus operations */
100 struct dm_virtio_ops {
102 * get_config() - read the value of a configuration field
104 * @vdev: the real virtio device
105 * @offset: the offset of the configuration field
106 * @buf: the buffer to write the field value into
107 * @len: the length of the buffer
108 * @return 0 if OK, -ve on error
110 int (*get_config)(struct udevice *vdev, unsigned int offset,
111 void *buf, unsigned int len);
113 * set_config() - write the value of a configuration field
115 * @vdev: the real virtio device
116 * @offset: the offset of the configuration field
117 * @buf: the buffer to read the field value from
118 * @len: the length of the buffer
119 * @return 0 if OK, -ve on error
121 int (*set_config)(struct udevice *vdev, unsigned int offset,
122 const void *buf, unsigned int len);
124 * generation() - config generation counter
126 * @vdev: the real virtio device
127 * @counter: the returned config generation counter
128 * @return 0 if OK, -ve on error
130 int (*generation)(struct udevice *vdev, u32 *counter);
132 * get_status() - read the status byte
134 * @vdev: the real virtio device
135 * @status: the returned status byte
136 * @return 0 if OK, -ve on error
138 int (*get_status)(struct udevice *vdev, u8 *status);
140 * set_status() - write the status byte
142 * @vdev: the real virtio device
143 * @status: the new status byte
144 * @return 0 if OK, -ve on error
146 int (*set_status)(struct udevice *vdev, u8 status);
148 * reset() - reset the device
150 * @vdev: the real virtio device
151 * @return 0 if OK, -ve on error
153 int (*reset)(struct udevice *vdev);
155 * get_features() - get the array of feature bits for this device
157 * @vdev: the real virtio device
158 * @features: the first 32 feature bits (all we currently need)
159 * @return 0 if OK, -ve on error
161 int (*get_features)(struct udevice *vdev, u64 *features);
163 * set_features() - confirm what device features we'll be using
165 * @vdev: the real virtio device
166 * @return 0 if OK, -ve on error
168 int (*set_features)(struct udevice *vdev);
170 * find_vqs() - find virtqueues and instantiate them
172 * @vdev: the real virtio device
173 * @nvqs: the number of virtqueues to find
174 * @vqs: on success, includes new virtqueues
175 * @return 0 if OK, -ve on error
177 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
178 struct virtqueue *vqs[]);
180 * del_vqs() - free virtqueues found by find_vqs()
182 * @vdev: the real virtio device
183 * @return 0 if OK, -ve on error
185 int (*del_vqs)(struct udevice *vdev);
187 * notify() - notify the device to process the queue
189 * @vdev: the real virtio device
190 * @vq: virtqueue to process
191 * @return 0 if OK, -ve on error
193 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
196 /* Get access to a virtio bus' operations */
197 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
200 * virtio uclass per device private data
202 * @vqs: virtualqueue for the virtio device
203 * @vdev: the real virtio device underneath
204 * @legacy: is it a legacy device?
205 * @device: virtio device ID
206 * @vendor: virtio vendor ID
207 * @features: negotiated supported features
208 * @feature_table: an array of feature supported by the driver
209 * @feature_table_size: number of entries in the feature table array
210 * @feature_table_legacy: same as feature_table but working in legacy mode
211 * @feature_table_size_legacy: number of entries in feature table legacy array
213 struct virtio_dev_priv {
214 struct list_head vqs;
215 struct udevice *vdev;
220 const u32 *feature_table;
221 u32 feature_table_size;
222 const u32 *feature_table_legacy;
223 u32 feature_table_size_legacy;
227 * virtio_get_config() - read the value of a configuration field
229 * @vdev: the real virtio device
230 * @offset: the offset of the configuration field
231 * @buf: the buffer to write the field value into
232 * @len: the length of the buffer
233 * Return: 0 if OK, -ve on error
235 int virtio_get_config(struct udevice *vdev, unsigned int offset,
236 void *buf, unsigned int len);
239 * virtio_set_config() - write the value of a configuration field
241 * @vdev: the real virtio device
242 * @offset: the offset of the configuration field
243 * @buf: the buffer to read the field value from
244 * @len: the length of the buffer
245 * Return: 0 if OK, -ve on error
247 int virtio_set_config(struct udevice *vdev, unsigned int offset,
248 void *buf, unsigned int len);
251 * virtio_generation() - config generation counter
253 * @vdev: the real virtio device
254 * @counter: the returned config generation counter
255 * Return: 0 if OK, -ve on error
257 int virtio_generation(struct udevice *vdev, u32 *counter);
260 * virtio_get_status() - read the status byte
262 * @vdev: the real virtio device
263 * @status: the returned status byte
264 * Return: 0 if OK, -ve on error
266 int virtio_get_status(struct udevice *vdev, u8 *status);
269 * virtio_set_status() - write the status byte
271 * @vdev: the real virtio device
272 * @status: the new status byte
273 * Return: 0 if OK, -ve on error
275 int virtio_set_status(struct udevice *vdev, u8 status);
278 * virtio_reset() - reset the device
280 * @vdev: the real virtio device
281 * Return: 0 if OK, -ve on error
283 int virtio_reset(struct udevice *vdev);
286 * virtio_get_features() - get the array of feature bits for this device
288 * @vdev: the real virtio device
289 * @features: the first 32 feature bits (all we currently need)
290 * Return: 0 if OK, -ve on error
292 int virtio_get_features(struct udevice *vdev, u64 *features);
295 * virtio_set_features() - confirm what device features we'll be using
297 * @vdev: the real virtio device
298 * Return: 0 if OK, -ve on error
300 int virtio_set_features(struct udevice *vdev);
303 * virtio_find_vqs() - find virtqueues and instantiate them
305 * @vdev: the real virtio device
306 * @nvqs: the number of virtqueues to find
307 * @vqs: on success, includes new virtqueues
308 * Return: 0 if OK, -ve on error
310 int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
311 struct virtqueue *vqs[]);
314 * virtio_del_vqs() - free virtqueues found by find_vqs()
316 * @vdev: the real virtio device
317 * Return: 0 if OK, -ve on error
319 int virtio_del_vqs(struct udevice *vdev);
322 * virtio_notify() - notify the device to process the queue
324 * @vdev: the real virtio device
325 * @vq: virtqueue to process
326 * Return: 0 if OK, -ve on error
328 int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
331 * virtio_add_status() - helper to set a new status code to the device
333 * @vdev: the real virtio device
334 * @status: new status code to be added
336 void virtio_add_status(struct udevice *vdev, u8 status);
339 * virtio_finalize_features() - helper to finalize features
341 * @vdev: the real virtio device
342 * Return: 0 if OK, -ve on error
344 int virtio_finalize_features(struct udevice *vdev);
347 * virtio_driver_features_init() - initialize driver supported features
349 * This fills in the virtio device parent per child private data with the given
350 * information, which contains driver supported features and legacy features.
352 * This API should be called in the virtio device driver's bind method, so that
353 * later virtio transport uclass driver can utilize the driver supplied features
354 * to negotiate with the device on the final supported features.
356 * @priv: virtio uclass per device private data
357 * @feature: an array of feature supported by the driver
358 * @feature_size: number of entries in the feature table array
359 * @feature_legacy: same as feature_table but working in legacy mode
360 * @feature_legacy_size:number of entries in feature table legacy array
362 void virtio_driver_features_init(struct virtio_dev_priv *priv,
365 const u32 *feature_legacy,
366 u32 feature_legacy_size);
369 * virtio_init() - helper to enumerate all known virtio devices
371 * Return: 0 if OK, -ve on error
373 int virtio_init(void);
375 static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
378 return le16_to_cpu((__force __le16)val);
380 return be16_to_cpu((__force __be16)val);
383 static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
386 return (__force __virtio16)cpu_to_le16(val);
388 return (__force __virtio16)cpu_to_be16(val);
391 static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
394 return le32_to_cpu((__force __le32)val);
396 return be32_to_cpu((__force __be32)val);
399 static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
402 return (__force __virtio32)cpu_to_le32(val);
404 return (__force __virtio32)cpu_to_be32(val);
407 static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
410 return le64_to_cpu((__force __le64)val);
412 return be64_to_cpu((__force __be64)val);
415 static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
418 return (__force __virtio64)cpu_to_le64(val);
420 return (__force __virtio64)cpu_to_be64(val);
424 * __virtio_test_bit - helper to test feature bits
426 * For use by transports. Devices should normally use virtio_has_feature,
427 * which includes more checks.
429 * @udev: the transport device
430 * @fbit: the feature bit
432 static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
434 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
436 /* Did you forget to fix assumptions on max features? */
437 if (__builtin_constant_p(fbit))
438 BUILD_BUG_ON(fbit >= 64);
442 return uc_priv->features & BIT_ULL(fbit);
446 * __virtio_set_bit - helper to set feature bits
448 * For use by transports.
450 * @udev: the transport device
451 * @fbit: the feature bit
453 static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
455 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
457 /* Did you forget to fix assumptions on max features? */
458 if (__builtin_constant_p(fbit))
459 BUILD_BUG_ON(fbit >= 64);
463 uc_priv->features |= BIT_ULL(fbit);
467 * __virtio_clear_bit - helper to clear feature bits
469 * For use by transports.
471 * @vdev: the transport device
472 * @fbit: the feature bit
474 static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
476 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
478 /* Did you forget to fix assumptions on max features? */
479 if (__builtin_constant_p(fbit))
480 BUILD_BUG_ON(fbit >= 64);
484 uc_priv->features &= ~BIT_ULL(fbit);
488 * virtio_has_feature - helper to determine if this device has this feature
490 * Note this API is only usable after the virtio device driver's bind phase,
491 * as the feature has been negotiated between the device and the driver.
493 * @vdev: the virtio device
494 * @fbit: the feature bit
496 static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
498 if (!(dev_get_flags(vdev) & DM_FLAG_BOUND))
501 return __virtio_test_bit(vdev->parent, fbit);
504 static inline bool virtio_legacy_is_little_endian(void)
506 #ifdef __LITTLE_ENDIAN
513 static inline bool virtio_is_little_endian(struct udevice *vdev)
515 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
517 return !uc_priv->legacy || virtio_legacy_is_little_endian();
520 /* Memory accessors */
521 static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
523 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
526 static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
528 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
531 static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
533 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
536 static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
538 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
541 static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
543 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
546 static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
548 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
551 /* Read @count fields, @bytes each */
552 static inline void __virtio_cread_many(struct udevice *vdev,
554 void *buf, size_t count, size_t bytes)
559 /* no need to check return value as generation can be optional */
560 virtio_generation(vdev, &gen);
564 for (i = 0; i < count; i++)
565 virtio_get_config(vdev, offset + bytes * i,
566 buf + i * bytes, bytes);
568 virtio_generation(vdev, &gen);
569 } while (gen != old);
572 static inline void virtio_cread_bytes(struct udevice *vdev,
574 void *buf, size_t len)
576 __virtio_cread_many(vdev, offset, buf, len, 1);
579 static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
583 virtio_get_config(vdev, offset, &ret, sizeof(ret));
587 static inline void virtio_cwrite8(struct udevice *vdev,
588 unsigned int offset, u8 val)
590 virtio_set_config(vdev, offset, &val, sizeof(val));
593 static inline u16 virtio_cread16(struct udevice *vdev,
598 virtio_get_config(vdev, offset, &ret, sizeof(ret));
599 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
602 static inline void virtio_cwrite16(struct udevice *vdev,
603 unsigned int offset, u16 val)
605 val = (__force u16)cpu_to_virtio16(vdev, val);
606 virtio_set_config(vdev, offset, &val, sizeof(val));
609 static inline u32 virtio_cread32(struct udevice *vdev,
614 virtio_get_config(vdev, offset, &ret, sizeof(ret));
615 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
618 static inline void virtio_cwrite32(struct udevice *vdev,
619 unsigned int offset, u32 val)
621 val = (__force u32)cpu_to_virtio32(vdev, val);
622 virtio_set_config(vdev, offset, &val, sizeof(val));
625 static inline u64 virtio_cread64(struct udevice *vdev,
630 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
631 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
634 static inline void virtio_cwrite64(struct udevice *vdev,
635 unsigned int offset, u64 val)
637 val = (__force u64)cpu_to_virtio64(vdev, val);
638 virtio_set_config(vdev, offset, &val, sizeof(val));
641 /* Config space read accessor */
642 #define virtio_cread(vdev, structname, member, ptr) \
644 /* Must match the member's type, and be integer */ \
645 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
648 switch (sizeof(*ptr)) { \
650 *(ptr) = virtio_cread8(vdev, \
651 offsetof(structname, member)); \
654 *(ptr) = virtio_cread16(vdev, \
655 offsetof(structname, member)); \
658 *(ptr) = virtio_cread32(vdev, \
659 offsetof(structname, member)); \
662 *(ptr) = virtio_cread64(vdev, \
663 offsetof(structname, member)); \
670 /* Config space write accessor */
671 #define virtio_cwrite(vdev, structname, member, ptr) \
673 /* Must match the member's type, and be integer */ \
674 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
675 WARN_ON((*ptr) == 1); \
677 switch (sizeof(*ptr)) { \
679 virtio_cwrite8(vdev, \
680 offsetof(structname, member), \
684 virtio_cwrite16(vdev, \
685 offsetof(structname, member), \
689 virtio_cwrite32(vdev, \
690 offsetof(structname, member), \
694 virtio_cwrite64(vdev, \
695 offsetof(structname, member), \
703 /* Conditional config space accessors */
704 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
707 if (!virtio_has_feature(vdev, fbit)) \
710 virtio_cread(vdev, structname, member, ptr); \
714 #endif /* __VIRTIO_H__ */