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 <virtio_types.h>
24 #include <linux/bitops.h>
25 #include <linux/bug.h>
26 #define VIRTIO_ID_NET 1 /* virtio net */
27 #define VIRTIO_ID_BLOCK 2 /* virtio block */
28 #define VIRTIO_ID_RNG 4 /* virtio rng */
29 #define VIRTIO_ID_MAX_NUM 5
31 #define VIRTIO_NET_DRV_NAME "virtio-net"
32 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
33 #define VIRTIO_RNG_DRV_NAME "virtio-rng"
35 /* Status byte for guest to report progress, and synchronize features */
37 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
38 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
39 /* We have found a driver for the device */
40 #define VIRTIO_CONFIG_S_DRIVER 2
41 /* Driver has used its parts of the config, and is happy */
42 #define VIRTIO_CONFIG_S_DRIVER_OK 4
43 /* Driver has finished configuring features */
44 #define VIRTIO_CONFIG_S_FEATURES_OK 8
45 /* Device entered invalid state, driver must reset it */
46 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
47 /* We've given up on this device */
48 #define VIRTIO_CONFIG_S_FAILED 0x80
51 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
52 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
53 * the rest are per-device feature bits.
55 #define VIRTIO_TRANSPORT_F_START 28
56 #define VIRTIO_TRANSPORT_F_END 38
58 #ifndef VIRTIO_CONFIG_NO_LEGACY
60 * Do we get callbacks when the ring is completely used,
61 * even if we've suppressed them?
63 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
65 /* Can the device handle any descriptor layout? */
66 #define VIRTIO_F_ANY_LAYOUT 27
67 #endif /* VIRTIO_CONFIG_NO_LEGACY */
70 #define VIRTIO_F_VERSION_1 32
73 * If clear - device has the IOMMU bypass quirk feature.
74 * If set - use platform tools to detect the IOMMU.
76 * Note the reverse polarity (compared to most other features),
77 * this is for compatibility with legacy systems.
79 #define VIRTIO_F_IOMMU_PLATFORM 33
81 /* Does the device support Single Root I/O Virtualization? */
82 #define VIRTIO_F_SR_IOV 37
85 * virtio scatter-gather struct
87 * @addr: sg buffer address
88 * @lengh: sg buffer length
97 /* virtio bus operations */
98 struct dm_virtio_ops
{
100 * get_config() - read the value of a configuration field
102 * @vdev: the real virtio device
103 * @offset: the offset of the configuration field
104 * @buf: the buffer to write the field value into
105 * @len: the length of the buffer
106 * @return 0 if OK, -ve on error
108 int (*get_config
)(struct udevice
*vdev
, unsigned int offset
,
109 void *buf
, unsigned int len
);
111 * set_config() - write the value of a configuration field
113 * @vdev: the real virtio device
114 * @offset: the offset of the configuration field
115 * @buf: the buffer to read the field value from
116 * @len: the length of the buffer
117 * @return 0 if OK, -ve on error
119 int (*set_config
)(struct udevice
*vdev
, unsigned int offset
,
120 const void *buf
, unsigned int len
);
122 * generation() - config generation counter
124 * @vdev: the real virtio device
125 * @counter: the returned config generation counter
126 * @return 0 if OK, -ve on error
128 int (*generation
)(struct udevice
*vdev
, u32
*counter
);
130 * get_status() - read the status byte
132 * @vdev: the real virtio device
133 * @status: the returned status byte
134 * @return 0 if OK, -ve on error
136 int (*get_status
)(struct udevice
*vdev
, u8
*status
);
138 * set_status() - write the status byte
140 * @vdev: the real virtio device
141 * @status: the new status byte
142 * @return 0 if OK, -ve on error
144 int (*set_status
)(struct udevice
*vdev
, u8 status
);
146 * reset() - reset the device
148 * @vdev: the real virtio device
149 * @return 0 if OK, -ve on error
151 int (*reset
)(struct udevice
*vdev
);
153 * get_features() - get the array of feature bits for this device
155 * @vdev: the real virtio device
156 * @features: the first 32 feature bits (all we currently need)
157 * @return 0 if OK, -ve on error
159 int (*get_features
)(struct udevice
*vdev
, u64
*features
);
161 * set_features() - confirm what device features we'll be using
163 * @vdev: the real virtio device
164 * @return 0 if OK, -ve on error
166 int (*set_features
)(struct udevice
*vdev
);
168 * find_vqs() - find virtqueues and instantiate them
170 * @vdev: the real virtio device
171 * @nvqs: the number of virtqueues to find
172 * @vqs: on success, includes new virtqueues
173 * @return 0 if OK, -ve on error
175 int (*find_vqs
)(struct udevice
*vdev
, unsigned int nvqs
,
176 struct virtqueue
*vqs
[]);
178 * del_vqs() - free virtqueues found by find_vqs()
180 * @vdev: the real virtio device
181 * @return 0 if OK, -ve on error
183 int (*del_vqs
)(struct udevice
*vdev
);
185 * notify() - notify the device to process the queue
187 * @vdev: the real virtio device
188 * @vq: virtqueue to process
189 * @return 0 if OK, -ve on error
191 int (*notify
)(struct udevice
*vdev
, struct virtqueue
*vq
);
194 /* Get access to a virtio bus' operations */
195 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
198 * virtio uclass per device private data
200 * @vqs: virtualqueue for the virtio device
201 * @vdev: the real virtio device underneath
202 * @legacy: is it a legacy device?
203 * @device: virtio device ID
204 * @vendor: virtio vendor ID
205 * @features: negotiated supported features
206 * @feature_table: an array of feature supported by the driver
207 * @feature_table_size: number of entries in the feature table array
208 * @feature_table_legacy: same as feature_table but working in legacy mode
209 * @feature_table_size_legacy: number of entries in feature table legacy array
211 struct virtio_dev_priv
{
212 struct list_head vqs
;
213 struct udevice
*vdev
;
218 const u32
*feature_table
;
219 u32 feature_table_size
;
220 const u32
*feature_table_legacy
;
221 u32 feature_table_size_legacy
;
225 * virtio_get_config() - read the value of a configuration field
227 * @vdev: the real virtio device
228 * @offset: the offset of the configuration field
229 * @buf: the buffer to write the field value into
230 * @len: the length of the buffer
231 * Return: 0 if OK, -ve on error
233 int virtio_get_config(struct udevice
*vdev
, unsigned int offset
,
234 void *buf
, unsigned int len
);
237 * virtio_set_config() - write the value of a configuration field
239 * @vdev: the real virtio device
240 * @offset: the offset of the configuration field
241 * @buf: the buffer to read the field value from
242 * @len: the length of the buffer
243 * Return: 0 if OK, -ve on error
245 int virtio_set_config(struct udevice
*vdev
, unsigned int offset
,
246 void *buf
, unsigned int len
);
249 * virtio_generation() - config generation counter
251 * @vdev: the real virtio device
252 * @counter: the returned config generation counter
253 * Return: 0 if OK, -ve on error
255 int virtio_generation(struct udevice
*vdev
, u32
*counter
);
258 * virtio_get_status() - read the status byte
260 * @vdev: the real virtio device
261 * @status: the returned status byte
262 * Return: 0 if OK, -ve on error
264 int virtio_get_status(struct udevice
*vdev
, u8
*status
);
267 * virtio_set_status() - write the status byte
269 * @vdev: the real virtio device
270 * @status: the new status byte
271 * Return: 0 if OK, -ve on error
273 int virtio_set_status(struct udevice
*vdev
, u8 status
);
276 * virtio_reset() - reset the device
278 * @vdev: the real virtio device
279 * Return: 0 if OK, -ve on error
281 int virtio_reset(struct udevice
*vdev
);
284 * virtio_get_features() - get the array of feature bits for this device
286 * @vdev: the real virtio device
287 * @features: the first 32 feature bits (all we currently need)
288 * Return: 0 if OK, -ve on error
290 int virtio_get_features(struct udevice
*vdev
, u64
*features
);
293 * virtio_set_features() - confirm what device features we'll be using
295 * @vdev: the real virtio device
296 * Return: 0 if OK, -ve on error
298 int virtio_set_features(struct udevice
*vdev
);
301 * virtio_find_vqs() - find virtqueues and instantiate them
303 * @vdev: the real virtio device
304 * @nvqs: the number of virtqueues to find
305 * @vqs: on success, includes new virtqueues
306 * Return: 0 if OK, -ve on error
308 int virtio_find_vqs(struct udevice
*vdev
, unsigned int nvqs
,
309 struct virtqueue
*vqs
[]);
312 * virtio_del_vqs() - free virtqueues found by find_vqs()
314 * @vdev: the real virtio device
315 * Return: 0 if OK, -ve on error
317 int virtio_del_vqs(struct udevice
*vdev
);
320 * virtio_notify() - notify the device to process the queue
322 * @vdev: the real virtio device
323 * @vq: virtqueue to process
324 * Return: 0 if OK, -ve on error
326 int virtio_notify(struct udevice
*vdev
, struct virtqueue
*vq
);
329 * virtio_add_status() - helper to set a new status code to the device
331 * @vdev: the real virtio device
332 * @status: new status code to be added
334 void virtio_add_status(struct udevice
*vdev
, u8 status
);
337 * virtio_finalize_features() - helper to finalize features
339 * @vdev: the real virtio device
340 * Return: 0 if OK, -ve on error
342 int virtio_finalize_features(struct udevice
*vdev
);
345 * virtio_driver_features_init() - initialize driver supported features
347 * This fills in the virtio device parent per child private data with the given
348 * information, which contains driver supported features and legacy features.
350 * This API should be called in the virtio device driver's bind method, so that
351 * later virtio transport uclass driver can utilize the driver supplied features
352 * to negotiate with the device on the final supported features.
354 * @priv: virtio uclass per device private data
355 * @feature: an array of feature supported by the driver
356 * @feature_size: number of entries in the feature table array
357 * @feature_legacy: same as feature_table but working in legacy mode
358 * @feature_legacy_size:number of entries in feature table legacy array
360 void virtio_driver_features_init(struct virtio_dev_priv
*priv
,
363 const u32
*feature_legacy
,
364 u32 feature_legacy_size
);
367 * virtio_init() - helper to enumerate all known virtio devices
369 * Return: 0 if OK, -ve on error
371 int virtio_init(void);
373 static inline u16
__virtio16_to_cpu(bool little_endian
, __virtio16 val
)
376 return le16_to_cpu((__force __le16
)val
);
378 return be16_to_cpu((__force __be16
)val
);
381 static inline __virtio16
__cpu_to_virtio16(bool little_endian
, u16 val
)
384 return (__force __virtio16
)cpu_to_le16(val
);
386 return (__force __virtio16
)cpu_to_be16(val
);
389 static inline u32
__virtio32_to_cpu(bool little_endian
, __virtio32 val
)
392 return le32_to_cpu((__force __le32
)val
);
394 return be32_to_cpu((__force __be32
)val
);
397 static inline __virtio32
__cpu_to_virtio32(bool little_endian
, u32 val
)
400 return (__force __virtio32
)cpu_to_le32(val
);
402 return (__force __virtio32
)cpu_to_be32(val
);
405 static inline u64
__virtio64_to_cpu(bool little_endian
, __virtio64 val
)
408 return le64_to_cpu((__force __le64
)val
);
410 return be64_to_cpu((__force __be64
)val
);
413 static inline __virtio64
__cpu_to_virtio64(bool little_endian
, u64 val
)
416 return (__force __virtio64
)cpu_to_le64(val
);
418 return (__force __virtio64
)cpu_to_be64(val
);
422 * __virtio_test_bit - helper to test feature bits
424 * For use by transports. Devices should normally use virtio_has_feature,
425 * which includes more checks.
427 * @udev: the transport device
428 * @fbit: the feature bit
430 static inline bool __virtio_test_bit(struct udevice
*udev
, unsigned int fbit
)
432 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
434 /* Did you forget to fix assumptions on max features? */
435 if (__builtin_constant_p(fbit
))
436 BUILD_BUG_ON(fbit
>= 64);
440 return uc_priv
->features
& BIT_ULL(fbit
);
444 * __virtio_set_bit - helper to set feature bits
446 * For use by transports.
448 * @udev: the transport device
449 * @fbit: the feature bit
451 static inline void __virtio_set_bit(struct udevice
*udev
, unsigned int fbit
)
453 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
455 /* Did you forget to fix assumptions on max features? */
456 if (__builtin_constant_p(fbit
))
457 BUILD_BUG_ON(fbit
>= 64);
461 uc_priv
->features
|= BIT_ULL(fbit
);
465 * __virtio_clear_bit - helper to clear feature bits
467 * For use by transports.
469 * @vdev: the transport device
470 * @fbit: the feature bit
472 static inline void __virtio_clear_bit(struct udevice
*udev
, unsigned int fbit
)
474 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
476 /* Did you forget to fix assumptions on max features? */
477 if (__builtin_constant_p(fbit
))
478 BUILD_BUG_ON(fbit
>= 64);
482 uc_priv
->features
&= ~BIT_ULL(fbit
);
486 * virtio_has_feature - helper to determine if this device has this feature
488 * Note this API is only usable after the virtio device driver's bind phase,
489 * as the feature has been negotiated between the device and the driver.
491 * @vdev: the virtio device
492 * @fbit: the feature bit
494 static inline bool virtio_has_feature(struct udevice
*vdev
, unsigned int fbit
)
496 if (!(dev_get_flags(vdev
) & DM_FLAG_BOUND
))
499 return __virtio_test_bit(vdev
->parent
, fbit
);
502 static inline bool virtio_legacy_is_little_endian(void)
504 #ifdef __LITTLE_ENDIAN
511 static inline bool virtio_is_little_endian(struct udevice
*vdev
)
513 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(vdev
->parent
);
515 return !uc_priv
->legacy
|| virtio_legacy_is_little_endian();
518 /* Memory accessors */
519 static inline u16
virtio16_to_cpu(struct udevice
*vdev
, __virtio16 val
)
521 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
524 static inline __virtio16
cpu_to_virtio16(struct udevice
*vdev
, u16 val
)
526 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
529 static inline u32
virtio32_to_cpu(struct udevice
*vdev
, __virtio32 val
)
531 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
534 static inline __virtio32
cpu_to_virtio32(struct udevice
*vdev
, u32 val
)
536 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
539 static inline u64
virtio64_to_cpu(struct udevice
*vdev
, __virtio64 val
)
541 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
544 static inline __virtio64
cpu_to_virtio64(struct udevice
*vdev
, u64 val
)
546 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
549 /* Read @count fields, @bytes each */
550 static inline void __virtio_cread_many(struct udevice
*vdev
,
552 void *buf
, size_t count
, size_t bytes
)
557 /* no need to check return value as generation can be optional */
558 virtio_generation(vdev
, &gen
);
562 for (i
= 0; i
< count
; i
++)
563 virtio_get_config(vdev
, offset
+ bytes
* i
,
564 buf
+ i
* bytes
, bytes
);
566 virtio_generation(vdev
, &gen
);
567 } while (gen
!= old
);
570 static inline void virtio_cread_bytes(struct udevice
*vdev
,
572 void *buf
, size_t len
)
574 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
577 static inline u8
virtio_cread8(struct udevice
*vdev
, unsigned int offset
)
581 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
585 static inline void virtio_cwrite8(struct udevice
*vdev
,
586 unsigned int offset
, u8 val
)
588 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
591 static inline u16
virtio_cread16(struct udevice
*vdev
,
596 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
597 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
600 static inline void virtio_cwrite16(struct udevice
*vdev
,
601 unsigned int offset
, u16 val
)
603 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
604 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
607 static inline u32
virtio_cread32(struct udevice
*vdev
,
612 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
613 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
616 static inline void virtio_cwrite32(struct udevice
*vdev
,
617 unsigned int offset
, u32 val
)
619 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
620 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
623 static inline u64
virtio_cread64(struct udevice
*vdev
,
628 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
629 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
632 static inline void virtio_cwrite64(struct udevice
*vdev
,
633 unsigned int offset
, u64 val
)
635 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
636 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
639 /* Config space read accessor */
640 #define virtio_cread(vdev, structname, member, ptr) \
642 /* Must match the member's type, and be integer */ \
643 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
646 switch (sizeof(*ptr)) { \
648 *(ptr) = virtio_cread8(vdev, \
649 offsetof(structname, member)); \
652 *(ptr) = virtio_cread16(vdev, \
653 offsetof(structname, member)); \
656 *(ptr) = virtio_cread32(vdev, \
657 offsetof(structname, member)); \
660 *(ptr) = virtio_cread64(vdev, \
661 offsetof(structname, member)); \
668 /* Config space write accessor */
669 #define virtio_cwrite(vdev, structname, member, ptr) \
671 /* Must match the member's type, and be integer */ \
672 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
673 WARN_ON((*ptr) == 1); \
675 switch (sizeof(*ptr)) { \
677 virtio_cwrite8(vdev, \
678 offsetof(structname, member), \
682 virtio_cwrite16(vdev, \
683 offsetof(structname, member), \
687 virtio_cwrite32(vdev, \
688 offsetof(structname, member), \
692 virtio_cwrite64(vdev, \
693 offsetof(structname, member), \
701 /* Conditional config space accessors */
702 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
705 if (!virtio_has_feature(vdev, fbit)) \
708 virtio_cread(vdev, structname, member, ptr); \
712 #endif /* __VIRTIO_H__ */