1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
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 <linux/bitops.h>
24 #include <linux/bug.h>
25 #define VIRTIO_ID_NET 1 /* virtio net */
26 #define VIRTIO_ID_BLOCK 2 /* virtio block */
27 #define VIRTIO_ID_RNG 4 /* virtio rng */
28 #define VIRTIO_ID_MAX_NUM 5
30 #define VIRTIO_NET_DRV_NAME "virtio-net"
31 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
32 #define VIRTIO_RNG_DRV_NAME "virtio-rng"
34 /* Status byte for guest to report progress, and synchronize features */
36 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
37 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
38 /* We have found a driver for the device */
39 #define VIRTIO_CONFIG_S_DRIVER 2
40 /* Driver has used its parts of the config, and is happy */
41 #define VIRTIO_CONFIG_S_DRIVER_OK 4
42 /* Driver has finished configuring features */
43 #define VIRTIO_CONFIG_S_FEATURES_OK 8
44 /* Device entered invalid state, driver must reset it */
45 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
46 /* We've given up on this device */
47 #define VIRTIO_CONFIG_S_FAILED 0x80
50 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
51 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
52 * the rest are per-device feature bits.
54 #define VIRTIO_TRANSPORT_F_START 28
55 #define VIRTIO_TRANSPORT_F_END 38
57 #ifndef VIRTIO_CONFIG_NO_LEGACY
59 * Do we get callbacks when the ring is completely used,
60 * even if we've suppressed them?
62 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
64 /* Can the device handle any descriptor layout? */
65 #define VIRTIO_F_ANY_LAYOUT 27
66 #endif /* VIRTIO_CONFIG_NO_LEGACY */
69 #define VIRTIO_F_VERSION_1 32
72 * If clear - device has the IOMMU bypass quirk feature.
73 * If set - use platform tools to detect the IOMMU.
75 * Note the reverse polarity (compared to most other features),
76 * this is for compatibility with legacy systems.
78 #define VIRTIO_F_IOMMU_PLATFORM 33
80 /* Does the device support Single Root I/O Virtualization? */
81 #define VIRTIO_F_SR_IOV 37
84 * virtio scatter-gather struct
86 * @addr: sg buffer address
87 * @lengh: sg buffer length
96 /* virtio bus operations */
97 struct dm_virtio_ops {
99 * get_config() - read the value of a configuration field
101 * @vdev: the real virtio device
102 * @offset: the offset of the configuration field
103 * @buf: the buffer to write the field value into
104 * @len: the length of the buffer
105 * @return 0 if OK, -ve on error
107 int (*get_config)(struct udevice *vdev, unsigned int offset,
108 void *buf, unsigned int len);
110 * set_config() - write the value of a configuration field
112 * @vdev: the real virtio device
113 * @offset: the offset of the configuration field
114 * @buf: the buffer to read the field value from
115 * @len: the length of the buffer
116 * @return 0 if OK, -ve on error
118 int (*set_config)(struct udevice *vdev, unsigned int offset,
119 const void *buf, unsigned int len);
121 * generation() - config generation counter
123 * @vdev: the real virtio device
124 * @counter: the returned config generation counter
125 * @return 0 if OK, -ve on error
127 int (*generation)(struct udevice *vdev, u32 *counter);
129 * get_status() - read the status byte
131 * @vdev: the real virtio device
132 * @status: the returned status byte
133 * @return 0 if OK, -ve on error
135 int (*get_status)(struct udevice *vdev, u8 *status);
137 * set_status() - write the status byte
139 * @vdev: the real virtio device
140 * @status: the new status byte
141 * @return 0 if OK, -ve on error
143 int (*set_status)(struct udevice *vdev, u8 status);
145 * reset() - reset the device
147 * @vdev: the real virtio device
148 * @return 0 if OK, -ve on error
150 int (*reset)(struct udevice *vdev);
152 * get_features() - get the array of feature bits for this device
154 * @vdev: the real virtio device
155 * @features: the first 32 feature bits (all we currently need)
156 * @return 0 if OK, -ve on error
158 int (*get_features)(struct udevice *vdev, u64 *features);
160 * set_features() - confirm what device features we'll be using
162 * @vdev: the real virtio device
163 * @return 0 if OK, -ve on error
165 int (*set_features)(struct udevice *vdev);
167 * find_vqs() - find virtqueues and instantiate them
169 * @vdev: the real virtio device
170 * @nvqs: the number of virtqueues to find
171 * @vqs: on success, includes new virtqueues
172 * @return 0 if OK, -ve on error
174 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
175 struct virtqueue *vqs[]);
177 * del_vqs() - free virtqueues found by find_vqs()
179 * @vdev: the real virtio device
180 * @return 0 if OK, -ve on error
182 int (*del_vqs)(struct udevice *vdev);
184 * notify() - notify the device to process the queue
186 * @vdev: the real virtio device
187 * @vq: virtqueue to process
188 * @return 0 if OK, -ve on error
190 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
193 /* Get access to a virtio bus' operations */
194 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
197 * virtio uclass per device private data
199 * @vqs: virtualqueue for the virtio device
200 * @vdev: the real virtio device underneath
201 * @legacy: is it a legacy device?
202 * @device: virtio device ID
203 * @vendor: virtio vendor ID
204 * @features: negotiated supported features
205 * @feature_table: an array of feature supported by the driver
206 * @feature_table_size: number of entries in the feature table array
207 * @feature_table_legacy: same as feature_table but working in legacy mode
208 * @feature_table_size_legacy: number of entries in feature table legacy array
210 struct virtio_dev_priv {
211 struct list_head vqs;
212 struct udevice *vdev;
217 const u32 *feature_table;
218 u32 feature_table_size;
219 const u32 *feature_table_legacy;
220 u32 feature_table_size_legacy;
224 * virtio_get_config() - read the value of a configuration field
226 * @vdev: the real virtio device
227 * @offset: the offset of the configuration field
228 * @buf: the buffer to write the field value into
229 * @len: the length of the buffer
230 * @return 0 if OK, -ve on error
232 int virtio_get_config(struct udevice *vdev, unsigned int offset,
233 void *buf, unsigned int len);
236 * virtio_set_config() - write the value of a configuration field
238 * @vdev: the real virtio device
239 * @offset: the offset of the configuration field
240 * @buf: the buffer to read the field value from
241 * @len: the length of the buffer
242 * @return 0 if OK, -ve on error
244 int virtio_set_config(struct udevice *vdev, unsigned int offset,
245 void *buf, unsigned int len);
248 * virtio_generation() - config generation counter
250 * @vdev: the real virtio device
251 * @counter: the returned config generation counter
252 * @return 0 if OK, -ve on error
254 int virtio_generation(struct udevice *vdev, u32 *counter);
257 * virtio_get_status() - read the status byte
259 * @vdev: the real virtio device
260 * @status: the returned status byte
261 * @return 0 if OK, -ve on error
263 int virtio_get_status(struct udevice *vdev, u8 *status);
266 * virtio_set_status() - write the status byte
268 * @vdev: the real virtio device
269 * @status: the new status byte
270 * @return 0 if OK, -ve on error
272 int virtio_set_status(struct udevice *vdev, u8 status);
275 * virtio_reset() - reset the device
277 * @vdev: the real virtio device
278 * @return 0 if OK, -ve on error
280 int virtio_reset(struct udevice *vdev);
283 * virtio_get_features() - get the array of feature bits for this device
285 * @vdev: the real virtio device
286 * @features: the first 32 feature bits (all we currently need)
287 * @return 0 if OK, -ve on error
289 int virtio_get_features(struct udevice *vdev, u64 *features);
292 * virtio_set_features() - confirm what device features we'll be using
294 * @vdev: the real virtio device
295 * @return 0 if OK, -ve on error
297 int virtio_set_features(struct udevice *vdev);
300 * virtio_find_vqs() - find virtqueues and instantiate them
302 * @vdev: the real virtio device
303 * @nvqs: the number of virtqueues to find
304 * @vqs: on success, includes new virtqueues
305 * @return 0 if OK, -ve on error
307 int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
308 struct virtqueue *vqs[]);
311 * virtio_del_vqs() - free virtqueues found by find_vqs()
313 * @vdev: the real virtio device
314 * @return 0 if OK, -ve on error
316 int virtio_del_vqs(struct udevice *vdev);
319 * virtio_notify() - notify the device to process the queue
321 * @vdev: the real virtio device
322 * @vq: virtqueue to process
323 * @return 0 if OK, -ve on error
325 int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
328 * virtio_add_status() - helper to set a new status code to the device
330 * @vdev: the real virtio device
331 * @status: new status code to be added
333 void virtio_add_status(struct udevice *vdev, u8 status);
336 * virtio_finalize_features() - helper to finalize features
338 * @vdev: the real virtio device
339 * @return 0 if OK, -ve on error
341 int virtio_finalize_features(struct udevice *vdev);
344 * virtio_driver_features_init() - initialize driver supported features
346 * This fills in the virtio device parent per child private data with the given
347 * information, which contains driver supported features and legacy features.
349 * This API should be called in the virtio device driver's bind method, so that
350 * later virtio transport uclass driver can utilize the driver supplied features
351 * to negotiate with the device on the final supported features.
353 * @priv: virtio uclass per device private data
354 * @feature: an array of feature supported by the driver
355 * @feature_size: number of entries in the feature table array
356 * @feature_legacy: same as feature_table but working in legacy mode
357 * @feature_legacy_size:number of entries in feature table legacy array
359 void virtio_driver_features_init(struct virtio_dev_priv *priv,
362 const u32 *feature_legacy,
363 u32 feature_legacy_size);
366 * virtio_init() - helper to enumerate all known virtio devices
368 * @return 0 if OK, -ve on error
370 int virtio_init(void);
372 static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
375 return le16_to_cpu((__force __le16)val);
377 return be16_to_cpu((__force __be16)val);
380 static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
383 return (__force __virtio16)cpu_to_le16(val);
385 return (__force __virtio16)cpu_to_be16(val);
388 static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
391 return le32_to_cpu((__force __le32)val);
393 return be32_to_cpu((__force __be32)val);
396 static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
399 return (__force __virtio32)cpu_to_le32(val);
401 return (__force __virtio32)cpu_to_be32(val);
404 static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
407 return le64_to_cpu((__force __le64)val);
409 return be64_to_cpu((__force __be64)val);
412 static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
415 return (__force __virtio64)cpu_to_le64(val);
417 return (__force __virtio64)cpu_to_be64(val);
421 * __virtio_test_bit - helper to test feature bits
423 * For use by transports. Devices should normally use virtio_has_feature,
424 * which includes more checks.
426 * @udev: the transport device
427 * @fbit: the feature bit
429 static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
431 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
433 /* Did you forget to fix assumptions on max features? */
434 if (__builtin_constant_p(fbit))
435 BUILD_BUG_ON(fbit >= 64);
439 return uc_priv->features & BIT_ULL(fbit);
443 * __virtio_set_bit - helper to set feature bits
445 * For use by transports.
447 * @udev: the transport device
448 * @fbit: the feature bit
450 static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
452 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
454 /* Did you forget to fix assumptions on max features? */
455 if (__builtin_constant_p(fbit))
456 BUILD_BUG_ON(fbit >= 64);
460 uc_priv->features |= BIT_ULL(fbit);
464 * __virtio_clear_bit - helper to clear feature bits
466 * For use by transports.
468 * @vdev: the transport device
469 * @fbit: the feature bit
471 static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
473 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
475 /* Did you forget to fix assumptions on max features? */
476 if (__builtin_constant_p(fbit))
477 BUILD_BUG_ON(fbit >= 64);
481 uc_priv->features &= ~BIT_ULL(fbit);
485 * virtio_has_feature - helper to determine if this device has this feature
487 * Note this API is only usable after the virtio device driver's bind phase,
488 * as the feature has been negotiated between the device and the driver.
490 * @vdev: the virtio device
491 * @fbit: the feature bit
493 static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
495 if (!(dev_get_flags(vdev) & DM_FLAG_BOUND))
498 return __virtio_test_bit(vdev->parent, fbit);
501 static inline bool virtio_legacy_is_little_endian(void)
503 #ifdef __LITTLE_ENDIAN
510 static inline bool virtio_is_little_endian(struct udevice *vdev)
512 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
514 return !uc_priv->legacy || virtio_legacy_is_little_endian();
517 /* Memory accessors */
518 static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
520 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
523 static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
525 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
528 static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
530 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
533 static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
535 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
538 static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
540 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
543 static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
545 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
548 /* Read @count fields, @bytes each */
549 static inline void __virtio_cread_many(struct udevice *vdev,
551 void *buf, size_t count, size_t bytes)
556 /* no need to check return value as generation can be optional */
557 virtio_generation(vdev, &gen);
561 for (i = 0; i < count; i++)
562 virtio_get_config(vdev, offset + bytes * i,
563 buf + i * bytes, bytes);
565 virtio_generation(vdev, &gen);
566 } while (gen != old);
569 static inline void virtio_cread_bytes(struct udevice *vdev,
571 void *buf, size_t len)
573 __virtio_cread_many(vdev, offset, buf, len, 1);
576 static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
580 virtio_get_config(vdev, offset, &ret, sizeof(ret));
584 static inline void virtio_cwrite8(struct udevice *vdev,
585 unsigned int offset, u8 val)
587 virtio_set_config(vdev, offset, &val, sizeof(val));
590 static inline u16 virtio_cread16(struct udevice *vdev,
595 virtio_get_config(vdev, offset, &ret, sizeof(ret));
596 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
599 static inline void virtio_cwrite16(struct udevice *vdev,
600 unsigned int offset, u16 val)
602 val = (__force u16)cpu_to_virtio16(vdev, val);
603 virtio_set_config(vdev, offset, &val, sizeof(val));
606 static inline u32 virtio_cread32(struct udevice *vdev,
611 virtio_get_config(vdev, offset, &ret, sizeof(ret));
612 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
615 static inline void virtio_cwrite32(struct udevice *vdev,
616 unsigned int offset, u32 val)
618 val = (__force u32)cpu_to_virtio32(vdev, val);
619 virtio_set_config(vdev, offset, &val, sizeof(val));
622 static inline u64 virtio_cread64(struct udevice *vdev,
627 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
628 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
631 static inline void virtio_cwrite64(struct udevice *vdev,
632 unsigned int offset, u64 val)
634 val = (__force u64)cpu_to_virtio64(vdev, val);
635 virtio_set_config(vdev, offset, &val, sizeof(val));
638 /* Config space read accessor */
639 #define virtio_cread(vdev, structname, member, ptr) \
641 /* Must match the member's type, and be integer */ \
642 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
645 switch (sizeof(*ptr)) { \
647 *(ptr) = virtio_cread8(vdev, \
648 offsetof(structname, member)); \
651 *(ptr) = virtio_cread16(vdev, \
652 offsetof(structname, member)); \
655 *(ptr) = virtio_cread32(vdev, \
656 offsetof(structname, member)); \
659 *(ptr) = virtio_cread64(vdev, \
660 offsetof(structname, member)); \
667 /* Config space write accessor */
668 #define virtio_cwrite(vdev, structname, member, ptr) \
670 /* Must match the member's type, and be integer */ \
671 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
672 WARN_ON((*ptr) == 1); \
674 switch (sizeof(*ptr)) { \
676 virtio_cwrite8(vdev, \
677 offsetof(structname, member), \
681 virtio_cwrite16(vdev, \
682 offsetof(structname, member), \
686 virtio_cwrite32(vdev, \
687 offsetof(structname, member), \
691 virtio_cwrite64(vdev, \
692 offsetof(structname, member), \
700 /* Conditional config space accessors */
701 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
704 if (!virtio_has_feature(vdev, fbit)) \
707 virtio_cread(vdev, structname, member, ptr); \
711 #endif /* __VIRTIO_H__ */