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/bug.h>
24 #define VIRTIO_ID_NET 1 /* virtio net */
25 #define VIRTIO_ID_BLOCK 2 /* virtio block */
26 #define VIRTIO_ID_RNG 4 /* virtio rng */
27 #define VIRTIO_ID_MAX_NUM 5
29 #define VIRTIO_NET_DRV_NAME "virtio-net"
30 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
31 #define VIRTIO_RNG_DRV_NAME "virtio-rng"
33 /* Status byte for guest to report progress, and synchronize features */
35 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
36 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
37 /* We have found a driver for the device */
38 #define VIRTIO_CONFIG_S_DRIVER 2
39 /* Driver has used its parts of the config, and is happy */
40 #define VIRTIO_CONFIG_S_DRIVER_OK 4
41 /* Driver has finished configuring features */
42 #define VIRTIO_CONFIG_S_FEATURES_OK 8
43 /* Device entered invalid state, driver must reset it */
44 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
45 /* We've given up on this device */
46 #define VIRTIO_CONFIG_S_FAILED 0x80
49 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
50 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
51 * the rest are per-device feature bits.
53 #define VIRTIO_TRANSPORT_F_START 28
54 #define VIRTIO_TRANSPORT_F_END 38
56 #ifndef VIRTIO_CONFIG_NO_LEGACY
58 * Do we get callbacks when the ring is completely used,
59 * even if we've suppressed them?
61 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
63 /* Can the device handle any descriptor layout? */
64 #define VIRTIO_F_ANY_LAYOUT 27
65 #endif /* VIRTIO_CONFIG_NO_LEGACY */
68 #define VIRTIO_F_VERSION_1 32
71 * If clear - device has the IOMMU bypass quirk feature.
72 * If set - use platform tools to detect the IOMMU.
74 * Note the reverse polarity (compared to most other features),
75 * this is for compatibility with legacy systems.
77 #define VIRTIO_F_IOMMU_PLATFORM 33
79 /* Does the device support Single Root I/O Virtualization? */
80 #define VIRTIO_F_SR_IOV 37
83 * virtio scatter-gather struct
85 * @addr: sg buffer address
86 * @lengh: sg buffer length
95 /* virtio bus operations */
96 struct dm_virtio_ops {
98 * get_config() - read the value of a configuration field
100 * @vdev: the real virtio device
101 * @offset: the offset of the configuration field
102 * @buf: the buffer to write the field value into
103 * @len: the length of the buffer
104 * @return 0 if OK, -ve on error
106 int (*get_config)(struct udevice *vdev, unsigned int offset,
107 void *buf, unsigned int len);
109 * set_config() - write the value of a configuration field
111 * @vdev: the real virtio device
112 * @offset: the offset of the configuration field
113 * @buf: the buffer to read the field value from
114 * @len: the length of the buffer
115 * @return 0 if OK, -ve on error
117 int (*set_config)(struct udevice *vdev, unsigned int offset,
118 const void *buf, unsigned int len);
120 * generation() - config generation counter
122 * @vdev: the real virtio device
123 * @counter: the returned config generation counter
124 * @return 0 if OK, -ve on error
126 int (*generation)(struct udevice *vdev, u32 *counter);
128 * get_status() - read the status byte
130 * @vdev: the real virtio device
131 * @status: the returned status byte
132 * @return 0 if OK, -ve on error
134 int (*get_status)(struct udevice *vdev, u8 *status);
136 * set_status() - write the status byte
138 * @vdev: the real virtio device
139 * @status: the new status byte
140 * @return 0 if OK, -ve on error
142 int (*set_status)(struct udevice *vdev, u8 status);
144 * reset() - reset the device
146 * @vdev: the real virtio device
147 * @return 0 if OK, -ve on error
149 int (*reset)(struct udevice *vdev);
151 * get_features() - get the array of feature bits for this device
153 * @vdev: the real virtio device
154 * @features: the first 32 feature bits (all we currently need)
155 * @return 0 if OK, -ve on error
157 int (*get_features)(struct udevice *vdev, u64 *features);
159 * set_features() - confirm what device features we'll be using
161 * @vdev: the real virtio device
162 * @return 0 if OK, -ve on error
164 int (*set_features)(struct udevice *vdev);
166 * find_vqs() - find virtqueues and instantiate them
168 * @vdev: the real virtio device
169 * @nvqs: the number of virtqueues to find
170 * @vqs: on success, includes new virtqueues
171 * @return 0 if OK, -ve on error
173 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
174 struct virtqueue *vqs[]);
176 * del_vqs() - free virtqueues found by find_vqs()
178 * @vdev: the real virtio device
179 * @return 0 if OK, -ve on error
181 int (*del_vqs)(struct udevice *vdev);
183 * notify() - notify the device to process the queue
185 * @vdev: the real virtio device
186 * @vq: virtqueue to process
187 * @return 0 if OK, -ve on error
189 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
192 /* Get access to a virtio bus' operations */
193 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
196 * virtio uclass per device private data
198 * @vqs: virtualqueue for the virtio device
199 * @vdev: the real virtio device underneath
200 * @legacy: is it a legacy device?
201 * @device: virtio device ID
202 * @vendor: virtio vendor ID
203 * @features: negotiated supported features
204 * @feature_table: an array of feature supported by the driver
205 * @feature_table_size: number of entries in the feature table array
206 * @feature_table_legacy: same as feature_table but working in legacy mode
207 * @feature_table_size_legacy: number of entries in feature table legacy array
209 struct virtio_dev_priv {
210 struct list_head vqs;
211 struct udevice *vdev;
216 const u32 *feature_table;
217 u32 feature_table_size;
218 const u32 *feature_table_legacy;
219 u32 feature_table_size_legacy;
223 * virtio_get_config() - read the value of a configuration field
225 * @vdev: the real virtio device
226 * @offset: the offset of the configuration field
227 * @buf: the buffer to write the field value into
228 * @len: the length of the buffer
229 * @return 0 if OK, -ve on error
231 int virtio_get_config(struct udevice *vdev, unsigned int offset,
232 void *buf, unsigned int len);
235 * virtio_set_config() - write the value of a configuration field
237 * @vdev: the real virtio device
238 * @offset: the offset of the configuration field
239 * @buf: the buffer to read the field value from
240 * @len: the length of the buffer
241 * @return 0 if OK, -ve on error
243 int virtio_set_config(struct udevice *vdev, unsigned int offset,
244 void *buf, unsigned int len);
247 * virtio_generation() - config generation counter
249 * @vdev: the real virtio device
250 * @counter: the returned config generation counter
251 * @return 0 if OK, -ve on error
253 int virtio_generation(struct udevice *vdev, u32 *counter);
256 * virtio_get_status() - read the status byte
258 * @vdev: the real virtio device
259 * @status: the returned status byte
260 * @return 0 if OK, -ve on error
262 int virtio_get_status(struct udevice *vdev, u8 *status);
265 * virtio_set_status() - write the status byte
267 * @vdev: the real virtio device
268 * @status: the new status byte
269 * @return 0 if OK, -ve on error
271 int virtio_set_status(struct udevice *vdev, u8 status);
274 * virtio_reset() - reset the device
276 * @vdev: the real virtio device
277 * @return 0 if OK, -ve on error
279 int virtio_reset(struct udevice *vdev);
282 * virtio_get_features() - get the array of feature bits for this device
284 * @vdev: the real virtio device
285 * @features: the first 32 feature bits (all we currently need)
286 * @return 0 if OK, -ve on error
288 int virtio_get_features(struct udevice *vdev, u64 *features);
291 * virtio_set_features() - confirm what device features we'll be using
293 * @vdev: the real virtio device
294 * @return 0 if OK, -ve on error
296 int virtio_set_features(struct udevice *vdev);
299 * virtio_find_vqs() - find virtqueues and instantiate them
301 * @vdev: the real virtio device
302 * @nvqs: the number of virtqueues to find
303 * @vqs: on success, includes new virtqueues
304 * @return 0 if OK, -ve on error
306 int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
307 struct virtqueue *vqs[]);
310 * virtio_del_vqs() - free virtqueues found by find_vqs()
312 * @vdev: the real virtio device
313 * @return 0 if OK, -ve on error
315 int virtio_del_vqs(struct udevice *vdev);
318 * virtio_notify() - notify the device to process the queue
320 * @vdev: the real virtio device
321 * @vq: virtqueue to process
322 * @return 0 if OK, -ve on error
324 int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
327 * virtio_add_status() - helper to set a new status code to the device
329 * @vdev: the real virtio device
330 * @status: new status code to be added
332 void virtio_add_status(struct udevice *vdev, u8 status);
335 * virtio_finalize_features() - helper to finalize features
337 * @vdev: the real virtio device
338 * @return 0 if OK, -ve on error
340 int virtio_finalize_features(struct udevice *vdev);
343 * virtio_driver_features_init() - initialize driver supported features
345 * This fills in the virtio device parent per child private data with the given
346 * information, which contains driver supported features and legacy features.
348 * This API should be called in the virtio device driver's bind method, so that
349 * later virtio transport uclass driver can utilize the driver supplied features
350 * to negotiate with the device on the final supported features.
352 * @priv: virtio uclass per device private data
353 * @feature: an array of feature supported by the driver
354 * @feature_size: number of entries in the feature table array
355 * @feature_legacy: same as feature_table but working in legacy mode
356 * @feature_legacy_size:number of entries in feature table legacy array
358 void virtio_driver_features_init(struct virtio_dev_priv *priv,
361 const u32 *feature_legacy,
362 u32 feature_legacy_size);
365 * virtio_init() - helper to enumerate all known virtio devices
367 * @return 0 if OK, -ve on error
369 int virtio_init(void);
371 static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
374 return le16_to_cpu((__force __le16)val);
376 return be16_to_cpu((__force __be16)val);
379 static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
382 return (__force __virtio16)cpu_to_le16(val);
384 return (__force __virtio16)cpu_to_be16(val);
387 static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
390 return le32_to_cpu((__force __le32)val);
392 return be32_to_cpu((__force __be32)val);
395 static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
398 return (__force __virtio32)cpu_to_le32(val);
400 return (__force __virtio32)cpu_to_be32(val);
403 static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
406 return le64_to_cpu((__force __le64)val);
408 return be64_to_cpu((__force __be64)val);
411 static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
414 return (__force __virtio64)cpu_to_le64(val);
416 return (__force __virtio64)cpu_to_be64(val);
420 * __virtio_test_bit - helper to test feature bits
422 * For use by transports. Devices should normally use virtio_has_feature,
423 * which includes more checks.
425 * @udev: the transport device
426 * @fbit: the feature bit
428 static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
430 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
432 /* Did you forget to fix assumptions on max features? */
433 if (__builtin_constant_p(fbit))
434 BUILD_BUG_ON(fbit >= 64);
438 return uc_priv->features & BIT_ULL(fbit);
442 * __virtio_set_bit - helper to set feature bits
444 * For use by transports.
446 * @udev: the transport device
447 * @fbit: the feature bit
449 static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
451 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
453 /* Did you forget to fix assumptions on max features? */
454 if (__builtin_constant_p(fbit))
455 BUILD_BUG_ON(fbit >= 64);
459 uc_priv->features |= BIT_ULL(fbit);
463 * __virtio_clear_bit - helper to clear feature bits
465 * For use by transports.
467 * @vdev: the transport device
468 * @fbit: the feature bit
470 static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
472 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
474 /* Did you forget to fix assumptions on max features? */
475 if (__builtin_constant_p(fbit))
476 BUILD_BUG_ON(fbit >= 64);
480 uc_priv->features &= ~BIT_ULL(fbit);
484 * virtio_has_feature - helper to determine if this device has this feature
486 * Note this API is only usable after the virtio device driver's bind phase,
487 * as the feature has been negotiated between the device and the driver.
489 * @vdev: the virtio device
490 * @fbit: the feature bit
492 static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
494 if (!(vdev->flags & DM_FLAG_BOUND))
497 return __virtio_test_bit(vdev->parent, fbit);
500 static inline bool virtio_legacy_is_little_endian(void)
502 #ifdef __LITTLE_ENDIAN
509 static inline bool virtio_is_little_endian(struct udevice *vdev)
511 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
513 return !uc_priv->legacy || virtio_legacy_is_little_endian();
516 /* Memory accessors */
517 static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
519 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
522 static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
524 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
527 static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
529 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
532 static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
534 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
537 static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
539 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
542 static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
544 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
547 /* Read @count fields, @bytes each */
548 static inline void __virtio_cread_many(struct udevice *vdev,
550 void *buf, size_t count, size_t bytes)
555 /* no need to check return value as generation can be optional */
556 virtio_generation(vdev, &gen);
560 for (i = 0; i < count; i++)
561 virtio_get_config(vdev, offset + bytes * i,
562 buf + i * bytes, bytes);
564 virtio_generation(vdev, &gen);
565 } while (gen != old);
568 static inline void virtio_cread_bytes(struct udevice *vdev,
570 void *buf, size_t len)
572 __virtio_cread_many(vdev, offset, buf, len, 1);
575 static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
579 virtio_get_config(vdev, offset, &ret, sizeof(ret));
583 static inline void virtio_cwrite8(struct udevice *vdev,
584 unsigned int offset, u8 val)
586 virtio_set_config(vdev, offset, &val, sizeof(val));
589 static inline u16 virtio_cread16(struct udevice *vdev,
594 virtio_get_config(vdev, offset, &ret, sizeof(ret));
595 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
598 static inline void virtio_cwrite16(struct udevice *vdev,
599 unsigned int offset, u16 val)
601 val = (__force u16)cpu_to_virtio16(vdev, val);
602 virtio_set_config(vdev, offset, &val, sizeof(val));
605 static inline u32 virtio_cread32(struct udevice *vdev,
610 virtio_get_config(vdev, offset, &ret, sizeof(ret));
611 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
614 static inline void virtio_cwrite32(struct udevice *vdev,
615 unsigned int offset, u32 val)
617 val = (__force u32)cpu_to_virtio32(vdev, val);
618 virtio_set_config(vdev, offset, &val, sizeof(val));
621 static inline u64 virtio_cread64(struct udevice *vdev,
626 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
627 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
630 static inline void virtio_cwrite64(struct udevice *vdev,
631 unsigned int offset, u64 val)
633 val = (__force u64)cpu_to_virtio64(vdev, val);
634 virtio_set_config(vdev, offset, &val, sizeof(val));
637 /* Config space read accessor */
638 #define virtio_cread(vdev, structname, member, ptr) \
640 /* Must match the member's type, and be integer */ \
641 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
644 switch (sizeof(*ptr)) { \
646 *(ptr) = virtio_cread8(vdev, \
647 offsetof(structname, member)); \
650 *(ptr) = virtio_cread16(vdev, \
651 offsetof(structname, member)); \
654 *(ptr) = virtio_cread32(vdev, \
655 offsetof(structname, member)); \
658 *(ptr) = virtio_cread64(vdev, \
659 offsetof(structname, member)); \
666 /* Config space write accessor */
667 #define virtio_cwrite(vdev, structname, member, ptr) \
669 /* Must match the member's type, and be integer */ \
670 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
671 WARN_ON((*ptr) == 1); \
673 switch (sizeof(*ptr)) { \
675 virtio_cwrite8(vdev, \
676 offsetof(structname, member), \
680 virtio_cwrite16(vdev, \
681 offsetof(structname, member), \
685 virtio_cwrite32(vdev, \
686 offsetof(structname, member), \
690 virtio_cwrite64(vdev, \
691 offsetof(structname, member), \
699 /* Conditional config space accessors */
700 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
703 if (!virtio_has_feature(vdev, fbit)) \
706 virtio_cread(vdev, structname, member, ptr); \
710 #endif /* __VIRTIO_H__ */