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 #define VIRTIO_ID_NET 1 /* virtio net */
24 #define VIRTIO_ID_BLOCK 2 /* virtio block */
25 #define VIRTIO_ID_RNG 4 /* virtio rng */
26 #define VIRTIO_ID_MAX_NUM 5
28 #define VIRTIO_NET_DRV_NAME "virtio-net"
29 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
30 #define VIRTIO_RNG_DRV_NAME "virtio-rng"
32 /* Status byte for guest to report progress, and synchronize features */
34 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
35 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
36 /* We have found a driver for the device */
37 #define VIRTIO_CONFIG_S_DRIVER 2
38 /* Driver has used its parts of the config, and is happy */
39 #define VIRTIO_CONFIG_S_DRIVER_OK 4
40 /* Driver has finished configuring features */
41 #define VIRTIO_CONFIG_S_FEATURES_OK 8
42 /* Device entered invalid state, driver must reset it */
43 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
44 /* We've given up on this device */
45 #define VIRTIO_CONFIG_S_FAILED 0x80
48 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
49 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
50 * the rest are per-device feature bits.
52 #define VIRTIO_TRANSPORT_F_START 28
53 #define VIRTIO_TRANSPORT_F_END 38
55 #ifndef VIRTIO_CONFIG_NO_LEGACY
57 * Do we get callbacks when the ring is completely used,
58 * even if we've suppressed them?
60 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
62 /* Can the device handle any descriptor layout? */
63 #define VIRTIO_F_ANY_LAYOUT 27
64 #endif /* VIRTIO_CONFIG_NO_LEGACY */
67 #define VIRTIO_F_VERSION_1 32
70 * If clear - device has the IOMMU bypass quirk feature.
71 * If set - use platform tools to detect the IOMMU.
73 * Note the reverse polarity (compared to most other features),
74 * this is for compatibility with legacy systems.
76 #define VIRTIO_F_IOMMU_PLATFORM 33
78 /* Does the device support Single Root I/O Virtualization? */
79 #define VIRTIO_F_SR_IOV 37
82 * virtio scatter-gather struct
84 * @addr: sg buffer address
85 * @lengh: sg buffer length
94 /* virtio bus operations */
95 struct dm_virtio_ops
{
97 * get_config() - read the value of a configuration field
99 * @vdev: the real virtio device
100 * @offset: the offset of the configuration field
101 * @buf: the buffer to write the field value into
102 * @len: the length of the buffer
103 * @return 0 if OK, -ve on error
105 int (*get_config
)(struct udevice
*vdev
, unsigned int offset
,
106 void *buf
, unsigned int len
);
108 * set_config() - write the value of a configuration field
110 * @vdev: the real virtio device
111 * @offset: the offset of the configuration field
112 * @buf: the buffer to read the field value from
113 * @len: the length of the buffer
114 * @return 0 if OK, -ve on error
116 int (*set_config
)(struct udevice
*vdev
, unsigned int offset
,
117 const void *buf
, unsigned int len
);
119 * generation() - config generation counter
121 * @vdev: the real virtio device
122 * @counter: the returned config generation counter
123 * @return 0 if OK, -ve on error
125 int (*generation
)(struct udevice
*vdev
, u32
*counter
);
127 * get_status() - read the status byte
129 * @vdev: the real virtio device
130 * @status: the returned status byte
131 * @return 0 if OK, -ve on error
133 int (*get_status
)(struct udevice
*vdev
, u8
*status
);
135 * set_status() - write the status byte
137 * @vdev: the real virtio device
138 * @status: the new status byte
139 * @return 0 if OK, -ve on error
141 int (*set_status
)(struct udevice
*vdev
, u8 status
);
143 * reset() - reset the device
145 * @vdev: the real virtio device
146 * @return 0 if OK, -ve on error
148 int (*reset
)(struct udevice
*vdev
);
150 * get_features() - get the array of feature bits for this device
152 * @vdev: the real virtio device
153 * @features: the first 32 feature bits (all we currently need)
154 * @return 0 if OK, -ve on error
156 int (*get_features
)(struct udevice
*vdev
, u64
*features
);
158 * set_features() - confirm what device features we'll be using
160 * @vdev: the real virtio device
161 * @return 0 if OK, -ve on error
163 int (*set_features
)(struct udevice
*vdev
);
165 * find_vqs() - find virtqueues and instantiate them
167 * @vdev: the real virtio device
168 * @nvqs: the number of virtqueues to find
169 * @vqs: on success, includes new virtqueues
170 * @return 0 if OK, -ve on error
172 int (*find_vqs
)(struct udevice
*vdev
, unsigned int nvqs
,
173 struct virtqueue
*vqs
[]);
175 * del_vqs() - free virtqueues found by find_vqs()
177 * @vdev: the real virtio device
178 * @return 0 if OK, -ve on error
180 int (*del_vqs
)(struct udevice
*vdev
);
182 * notify() - notify the device to process the queue
184 * @vdev: the real virtio device
185 * @vq: virtqueue to process
186 * @return 0 if OK, -ve on error
188 int (*notify
)(struct udevice
*vdev
, struct virtqueue
*vq
);
191 /* Get access to a virtio bus' operations */
192 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
195 * virtio uclass per device private data
197 * @vqs: virtualqueue for the virtio device
198 * @vdev: the real virtio device underneath
199 * @legacy: is it a legacy device?
200 * @device: virtio device ID
201 * @vendor: virtio vendor ID
202 * @features: negotiated supported features
203 * @feature_table: an array of feature supported by the driver
204 * @feature_table_size: number of entries in the feature table array
205 * @feature_table_legacy: same as feature_table but working in legacy mode
206 * @feature_table_size_legacy: number of entries in feature table legacy array
208 struct virtio_dev_priv
{
209 struct list_head vqs
;
210 struct udevice
*vdev
;
215 const u32
*feature_table
;
216 u32 feature_table_size
;
217 const u32
*feature_table_legacy
;
218 u32 feature_table_size_legacy
;
222 * virtio_get_config() - read the value of a configuration field
224 * @vdev: the real virtio device
225 * @offset: the offset of the configuration field
226 * @buf: the buffer to write the field value into
227 * @len: the length of the buffer
228 * @return 0 if OK, -ve on error
230 int virtio_get_config(struct udevice
*vdev
, unsigned int offset
,
231 void *buf
, unsigned int len
);
234 * virtio_set_config() - write the value of a configuration field
236 * @vdev: the real virtio device
237 * @offset: the offset of the configuration field
238 * @buf: the buffer to read the field value from
239 * @len: the length of the buffer
240 * @return 0 if OK, -ve on error
242 int virtio_set_config(struct udevice
*vdev
, unsigned int offset
,
243 void *buf
, unsigned int len
);
246 * virtio_generation() - config generation counter
248 * @vdev: the real virtio device
249 * @counter: the returned config generation counter
250 * @return 0 if OK, -ve on error
252 int virtio_generation(struct udevice
*vdev
, u32
*counter
);
255 * virtio_get_status() - read the status byte
257 * @vdev: the real virtio device
258 * @status: the returned status byte
259 * @return 0 if OK, -ve on error
261 int virtio_get_status(struct udevice
*vdev
, u8
*status
);
264 * virtio_set_status() - write the status byte
266 * @vdev: the real virtio device
267 * @status: the new status byte
268 * @return 0 if OK, -ve on error
270 int virtio_set_status(struct udevice
*vdev
, u8 status
);
273 * virtio_reset() - reset the device
275 * @vdev: the real virtio device
276 * @return 0 if OK, -ve on error
278 int virtio_reset(struct udevice
*vdev
);
281 * virtio_get_features() - get the array of feature bits for this device
283 * @vdev: the real virtio device
284 * @features: the first 32 feature bits (all we currently need)
285 * @return 0 if OK, -ve on error
287 int virtio_get_features(struct udevice
*vdev
, u64
*features
);
290 * virtio_set_features() - confirm what device features we'll be using
292 * @vdev: the real virtio device
293 * @return 0 if OK, -ve on error
295 int virtio_set_features(struct udevice
*vdev
);
298 * virtio_find_vqs() - find virtqueues and instantiate them
300 * @vdev: the real virtio device
301 * @nvqs: the number of virtqueues to find
302 * @vqs: on success, includes new virtqueues
303 * @return 0 if OK, -ve on error
305 int virtio_find_vqs(struct udevice
*vdev
, unsigned int nvqs
,
306 struct virtqueue
*vqs
[]);
309 * virtio_del_vqs() - free virtqueues found by find_vqs()
311 * @vdev: the real virtio device
312 * @return 0 if OK, -ve on error
314 int virtio_del_vqs(struct udevice
*vdev
);
317 * virtio_notify() - notify the device to process the queue
319 * @vdev: the real virtio device
320 * @vq: virtqueue to process
321 * @return 0 if OK, -ve on error
323 int virtio_notify(struct udevice
*vdev
, struct virtqueue
*vq
);
326 * virtio_add_status() - helper to set a new status code to the device
328 * @vdev: the real virtio device
329 * @status: new status code to be added
331 void virtio_add_status(struct udevice
*vdev
, u8 status
);
334 * virtio_finalize_features() - helper to finalize features
336 * @vdev: the real virtio device
337 * @return 0 if OK, -ve on error
339 int virtio_finalize_features(struct udevice
*vdev
);
342 * virtio_driver_features_init() - initialize driver supported features
344 * This fills in the virtio device parent per child private data with the given
345 * information, which contains driver supported features and legacy features.
347 * This API should be called in the virtio device driver's bind method, so that
348 * later virtio transport uclass driver can utilize the driver supplied features
349 * to negotiate with the device on the final supported features.
351 * @priv: virtio uclass per device private data
352 * @feature: an array of feature supported by the driver
353 * @feature_size: number of entries in the feature table array
354 * @feature_legacy: same as feature_table but working in legacy mode
355 * @feature_legacy_size:number of entries in feature table legacy array
357 void virtio_driver_features_init(struct virtio_dev_priv
*priv
,
360 const u32
*feature_legacy
,
361 u32 feature_legacy_size
);
364 * virtio_init() - helper to enumerate all known virtio devices
366 * @return 0 if OK, -ve on error
368 int virtio_init(void);
370 static inline u16
__virtio16_to_cpu(bool little_endian
, __virtio16 val
)
373 return le16_to_cpu((__force __le16
)val
);
375 return be16_to_cpu((__force __be16
)val
);
378 static inline __virtio16
__cpu_to_virtio16(bool little_endian
, u16 val
)
381 return (__force __virtio16
)cpu_to_le16(val
);
383 return (__force __virtio16
)cpu_to_be16(val
);
386 static inline u32
__virtio32_to_cpu(bool little_endian
, __virtio32 val
)
389 return le32_to_cpu((__force __le32
)val
);
391 return be32_to_cpu((__force __be32
)val
);
394 static inline __virtio32
__cpu_to_virtio32(bool little_endian
, u32 val
)
397 return (__force __virtio32
)cpu_to_le32(val
);
399 return (__force __virtio32
)cpu_to_be32(val
);
402 static inline u64
__virtio64_to_cpu(bool little_endian
, __virtio64 val
)
405 return le64_to_cpu((__force __le64
)val
);
407 return be64_to_cpu((__force __be64
)val
);
410 static inline __virtio64
__cpu_to_virtio64(bool little_endian
, u64 val
)
413 return (__force __virtio64
)cpu_to_le64(val
);
415 return (__force __virtio64
)cpu_to_be64(val
);
419 * __virtio_test_bit - helper to test feature bits
421 * For use by transports. Devices should normally use virtio_has_feature,
422 * which includes more checks.
424 * @udev: the transport device
425 * @fbit: the feature bit
427 static inline bool __virtio_test_bit(struct udevice
*udev
, unsigned int fbit
)
429 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
431 /* Did you forget to fix assumptions on max features? */
432 if (__builtin_constant_p(fbit
))
433 BUILD_BUG_ON(fbit
>= 64);
437 return uc_priv
->features
& BIT_ULL(fbit
);
441 * __virtio_set_bit - helper to set feature bits
443 * For use by transports.
445 * @udev: the transport device
446 * @fbit: the feature bit
448 static inline void __virtio_set_bit(struct udevice
*udev
, unsigned int fbit
)
450 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
452 /* Did you forget to fix assumptions on max features? */
453 if (__builtin_constant_p(fbit
))
454 BUILD_BUG_ON(fbit
>= 64);
458 uc_priv
->features
|= BIT_ULL(fbit
);
462 * __virtio_clear_bit - helper to clear feature bits
464 * For use by transports.
466 * @vdev: the transport device
467 * @fbit: the feature bit
469 static inline void __virtio_clear_bit(struct udevice
*udev
, unsigned int fbit
)
471 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
473 /* Did you forget to fix assumptions on max features? */
474 if (__builtin_constant_p(fbit
))
475 BUILD_BUG_ON(fbit
>= 64);
479 uc_priv
->features
&= ~BIT_ULL(fbit
);
483 * virtio_has_feature - helper to determine if this device has this feature
485 * Note this API is only usable after the virtio device driver's bind phase,
486 * as the feature has been negotiated between the device and the driver.
488 * @vdev: the virtio device
489 * @fbit: the feature bit
491 static inline bool virtio_has_feature(struct udevice
*vdev
, unsigned int fbit
)
493 if (!(vdev
->flags
& DM_FLAG_BOUND
))
496 return __virtio_test_bit(vdev
->parent
, fbit
);
499 static inline bool virtio_legacy_is_little_endian(void)
501 #ifdef __LITTLE_ENDIAN
508 static inline bool virtio_is_little_endian(struct udevice
*vdev
)
510 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(vdev
->parent
);
512 return !uc_priv
->legacy
|| virtio_legacy_is_little_endian();
515 /* Memory accessors */
516 static inline u16
virtio16_to_cpu(struct udevice
*vdev
, __virtio16 val
)
518 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
521 static inline __virtio16
cpu_to_virtio16(struct udevice
*vdev
, u16 val
)
523 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
526 static inline u32
virtio32_to_cpu(struct udevice
*vdev
, __virtio32 val
)
528 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
531 static inline __virtio32
cpu_to_virtio32(struct udevice
*vdev
, u32 val
)
533 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
536 static inline u64
virtio64_to_cpu(struct udevice
*vdev
, __virtio64 val
)
538 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
541 static inline __virtio64
cpu_to_virtio64(struct udevice
*vdev
, u64 val
)
543 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
546 /* Read @count fields, @bytes each */
547 static inline void __virtio_cread_many(struct udevice
*vdev
,
549 void *buf
, size_t count
, size_t bytes
)
554 /* no need to check return value as generation can be optional */
555 virtio_generation(vdev
, &gen
);
559 for (i
= 0; i
< count
; i
++)
560 virtio_get_config(vdev
, offset
+ bytes
* i
,
561 buf
+ i
* bytes
, bytes
);
563 virtio_generation(vdev
, &gen
);
564 } while (gen
!= old
);
567 static inline void virtio_cread_bytes(struct udevice
*vdev
,
569 void *buf
, size_t len
)
571 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
574 static inline u8
virtio_cread8(struct udevice
*vdev
, unsigned int offset
)
578 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
582 static inline void virtio_cwrite8(struct udevice
*vdev
,
583 unsigned int offset
, u8 val
)
585 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
588 static inline u16
virtio_cread16(struct udevice
*vdev
,
593 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
594 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
597 static inline void virtio_cwrite16(struct udevice
*vdev
,
598 unsigned int offset
, u16 val
)
600 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
601 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
604 static inline u32
virtio_cread32(struct udevice
*vdev
,
609 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
610 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
613 static inline void virtio_cwrite32(struct udevice
*vdev
,
614 unsigned int offset
, u32 val
)
616 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
617 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
620 static inline u64
virtio_cread64(struct udevice
*vdev
,
625 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
626 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
629 static inline void virtio_cwrite64(struct udevice
*vdev
,
630 unsigned int offset
, u64 val
)
632 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
633 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
636 /* Config space read accessor */
637 #define virtio_cread(vdev, structname, member, ptr) \
639 /* Must match the member's type, and be integer */ \
640 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
643 switch (sizeof(*ptr)) { \
645 *(ptr) = virtio_cread8(vdev, \
646 offsetof(structname, member)); \
649 *(ptr) = virtio_cread16(vdev, \
650 offsetof(structname, member)); \
653 *(ptr) = virtio_cread32(vdev, \
654 offsetof(structname, member)); \
657 *(ptr) = virtio_cread64(vdev, \
658 offsetof(structname, member)); \
665 /* Config space write accessor */
666 #define virtio_cwrite(vdev, structname, member, ptr) \
668 /* Must match the member's type, and be integer */ \
669 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
670 WARN_ON((*ptr) == 1); \
672 switch (sizeof(*ptr)) { \
674 virtio_cwrite8(vdev, \
675 offsetof(structname, member), \
679 virtio_cwrite16(vdev, \
680 offsetof(structname, member), \
684 virtio_cwrite32(vdev, \
685 offsetof(structname, member), \
689 virtio_cwrite64(vdev, \
690 offsetof(structname, member), \
698 /* Conditional config space accessors */
699 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
702 if (!virtio_has_feature(vdev, fbit)) \
705 virtio_cread(vdev, structname, member, ptr); \
709 #endif /* __VIRTIO_H__ */