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_MAX_NUM 3
27 #define VIRTIO_NET_DRV_NAME "virtio-net"
28 #define VIRTIO_BLK_DRV_NAME "virtio-blk"
30 /* Status byte for guest to report progress, and synchronize features */
32 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
33 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
34 /* We have found a driver for the device */
35 #define VIRTIO_CONFIG_S_DRIVER 2
36 /* Driver has used its parts of the config, and is happy */
37 #define VIRTIO_CONFIG_S_DRIVER_OK 4
38 /* Driver has finished configuring features */
39 #define VIRTIO_CONFIG_S_FEATURES_OK 8
40 /* Device entered invalid state, driver must reset it */
41 #define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
42 /* We've given up on this device */
43 #define VIRTIO_CONFIG_S_FAILED 0x80
46 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
47 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
48 * the rest are per-device feature bits.
50 #define VIRTIO_TRANSPORT_F_START 28
51 #define VIRTIO_TRANSPORT_F_END 38
53 #ifndef VIRTIO_CONFIG_NO_LEGACY
55 * Do we get callbacks when the ring is completely used,
56 * even if we've suppressed them?
58 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
60 /* Can the device handle any descriptor layout? */
61 #define VIRTIO_F_ANY_LAYOUT 27
62 #endif /* VIRTIO_CONFIG_NO_LEGACY */
65 #define VIRTIO_F_VERSION_1 32
68 * If clear - device has the IOMMU bypass quirk feature.
69 * If set - use platform tools to detect the IOMMU.
71 * Note the reverse polarity (compared to most other features),
72 * this is for compatibility with legacy systems.
74 #define VIRTIO_F_IOMMU_PLATFORM 33
76 /* Does the device support Single Root I/O Virtualization? */
77 #define VIRTIO_F_SR_IOV 37
80 * virtio scatter-gather struct
82 * @addr: sg buffer address
83 * @lengh: sg buffer length
92 /* virtio bus operations */
93 struct dm_virtio_ops
{
95 * get_config() - read the value of a configuration field
97 * @vdev: the real virtio device
98 * @offset: the offset of the configuration field
99 * @buf: the buffer to write the field value into
100 * @len: the length of the buffer
101 * @return 0 if OK, -ve on error
103 int (*get_config
)(struct udevice
*vdev
, unsigned int offset
,
104 void *buf
, unsigned int len
);
106 * set_config() - write the value of a configuration field
108 * @vdev: the real virtio device
109 * @offset: the offset of the configuration field
110 * @buf: the buffer to read the field value from
111 * @len: the length of the buffer
112 * @return 0 if OK, -ve on error
114 int (*set_config
)(struct udevice
*vdev
, unsigned int offset
,
115 const void *buf
, unsigned int len
);
117 * generation() - config generation counter
119 * @vdev: the real virtio device
120 * @counter: the returned config generation counter
121 * @return 0 if OK, -ve on error
123 int (*generation
)(struct udevice
*vdev
, u32
*counter
);
125 * get_status() - read the status byte
127 * @vdev: the real virtio device
128 * @status: the returned status byte
129 * @return 0 if OK, -ve on error
131 int (*get_status
)(struct udevice
*vdev
, u8
*status
);
133 * set_status() - write the status byte
135 * @vdev: the real virtio device
136 * @status: the new status byte
137 * @return 0 if OK, -ve on error
139 int (*set_status
)(struct udevice
*vdev
, u8 status
);
141 * reset() - reset the device
143 * @vdev: the real virtio device
144 * @return 0 if OK, -ve on error
146 int (*reset
)(struct udevice
*vdev
);
148 * get_features() - get the array of feature bits for this device
150 * @vdev: the real virtio device
151 * @features: the first 32 feature bits (all we currently need)
152 * @return 0 if OK, -ve on error
154 int (*get_features
)(struct udevice
*vdev
, u64
*features
);
156 * set_features() - confirm what device features we'll be using
158 * @vdev: the real virtio device
159 * @return 0 if OK, -ve on error
161 int (*set_features
)(struct udevice
*vdev
);
163 * find_vqs() - find virtqueues and instantiate them
165 * @vdev: the real virtio device
166 * @nvqs: the number of virtqueues to find
167 * @vqs: on success, includes new virtqueues
168 * @return 0 if OK, -ve on error
170 int (*find_vqs
)(struct udevice
*vdev
, unsigned int nvqs
,
171 struct virtqueue
*vqs
[]);
173 * del_vqs() - free virtqueues found by find_vqs()
175 * @vdev: the real virtio device
176 * @return 0 if OK, -ve on error
178 int (*del_vqs
)(struct udevice
*vdev
);
180 * notify() - notify the device to process the queue
182 * @vdev: the real virtio device
183 * @vq: virtqueue to process
184 * @return 0 if OK, -ve on error
186 int (*notify
)(struct udevice
*vdev
, struct virtqueue
*vq
);
189 /* Get access to a virtio bus' operations */
190 #define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
193 * virtio uclass per device private data
195 * @vqs: virtualqueue for the virtio device
196 * @vdev: the real virtio device underneath
197 * @legacy: is it a legacy device?
198 * @device: virtio device ID
199 * @vendor: virtio vendor ID
200 * @features: negotiated supported features
201 * @feature_table: an array of feature supported by the driver
202 * @feature_table_size: number of entries in the feature table array
203 * @feature_table_legacy: same as feature_table but working in legacy mode
204 * @feature_table_size_legacy: number of entries in feature table legacy array
206 struct virtio_dev_priv
{
207 struct list_head vqs
;
208 struct udevice
*vdev
;
213 const u32
*feature_table
;
214 u32 feature_table_size
;
215 const u32
*feature_table_legacy
;
216 u32 feature_table_size_legacy
;
220 * virtio_get_config() - read the value of a configuration field
222 * @vdev: the real virtio device
223 * @offset: the offset of the configuration field
224 * @buf: the buffer to write the field value into
225 * @len: the length of the buffer
226 * @return 0 if OK, -ve on error
228 int virtio_get_config(struct udevice
*vdev
, unsigned int offset
,
229 void *buf
, unsigned int len
);
232 * virtio_set_config() - write the value of a configuration field
234 * @vdev: the real virtio device
235 * @offset: the offset of the configuration field
236 * @buf: the buffer to read the field value from
237 * @len: the length of the buffer
238 * @return 0 if OK, -ve on error
240 int virtio_set_config(struct udevice
*vdev
, unsigned int offset
,
241 void *buf
, unsigned int len
);
244 * virtio_generation() - config generation counter
246 * @vdev: the real virtio device
247 * @counter: the returned config generation counter
248 * @return 0 if OK, -ve on error
250 int virtio_generation(struct udevice
*vdev
, u32
*counter
);
253 * virtio_get_status() - read the status byte
255 * @vdev: the real virtio device
256 * @status: the returned status byte
257 * @return 0 if OK, -ve on error
259 int virtio_get_status(struct udevice
*vdev
, u8
*status
);
262 * virtio_set_status() - write the status byte
264 * @vdev: the real virtio device
265 * @status: the new status byte
266 * @return 0 if OK, -ve on error
268 int virtio_set_status(struct udevice
*vdev
, u8 status
);
271 * virtio_reset() - reset the device
273 * @vdev: the real virtio device
274 * @return 0 if OK, -ve on error
276 int virtio_reset(struct udevice
*vdev
);
279 * virtio_get_features() - get the array of feature bits for this device
281 * @vdev: the real virtio device
282 * @features: the first 32 feature bits (all we currently need)
283 * @return 0 if OK, -ve on error
285 int virtio_get_features(struct udevice
*vdev
, u64
*features
);
288 * virtio_set_features() - confirm what device features we'll be using
290 * @vdev: the real virtio device
291 * @return 0 if OK, -ve on error
293 int virtio_set_features(struct udevice
*vdev
);
296 * virtio_find_vqs() - find virtqueues and instantiate them
298 * @vdev: the real virtio device
299 * @nvqs: the number of virtqueues to find
300 * @vqs: on success, includes new virtqueues
301 * @return 0 if OK, -ve on error
303 int virtio_find_vqs(struct udevice
*vdev
, unsigned int nvqs
,
304 struct virtqueue
*vqs
[]);
307 * virtio_del_vqs() - free virtqueues found by find_vqs()
309 * @vdev: the real virtio device
310 * @return 0 if OK, -ve on error
312 int virtio_del_vqs(struct udevice
*vdev
);
315 * virtio_notify() - notify the device to process the queue
317 * @vdev: the real virtio device
318 * @vq: virtqueue to process
319 * @return 0 if OK, -ve on error
321 int virtio_notify(struct udevice
*vdev
, struct virtqueue
*vq
);
324 * virtio_add_status() - helper to set a new status code to the device
326 * @vdev: the real virtio device
327 * @status: new status code to be added
329 void virtio_add_status(struct udevice
*vdev
, u8 status
);
332 * virtio_finalize_features() - helper to finalize features
334 * @vdev: the real virtio device
335 * @return 0 if OK, -ve on error
337 int virtio_finalize_features(struct udevice
*vdev
);
340 * virtio_driver_features_init() - initialize driver supported features
342 * This fills in the virtio device parent per child private data with the given
343 * information, which contains driver supported features and legacy features.
345 * This API should be called in the virtio device driver's bind method, so that
346 * later virtio transport uclass driver can utilize the driver supplied features
347 * to negotiate with the device on the final supported features.
349 * @priv: virtio uclass per device private data
350 * @feature: an array of feature supported by the driver
351 * @feature_size: number of entries in the feature table array
352 * @feature_legacy: same as feature_table but working in legacy mode
353 * @feature_legacy_size:number of entries in feature table legacy array
355 void virtio_driver_features_init(struct virtio_dev_priv
*priv
,
358 const u32
*feature_legacy
,
359 u32 feature_legacy_size
);
362 * virtio_init() - helper to enumerate all known virtio devices
364 * @return 0 if OK, -ve on error
366 int virtio_init(void);
368 static inline u16
__virtio16_to_cpu(bool little_endian
, __virtio16 val
)
371 return le16_to_cpu((__force __le16
)val
);
373 return be16_to_cpu((__force __be16
)val
);
376 static inline __virtio16
__cpu_to_virtio16(bool little_endian
, u16 val
)
379 return (__force __virtio16
)cpu_to_le16(val
);
381 return (__force __virtio16
)cpu_to_be16(val
);
384 static inline u32
__virtio32_to_cpu(bool little_endian
, __virtio32 val
)
387 return le32_to_cpu((__force __le32
)val
);
389 return be32_to_cpu((__force __be32
)val
);
392 static inline __virtio32
__cpu_to_virtio32(bool little_endian
, u32 val
)
395 return (__force __virtio32
)cpu_to_le32(val
);
397 return (__force __virtio32
)cpu_to_be32(val
);
400 static inline u64
__virtio64_to_cpu(bool little_endian
, __virtio64 val
)
403 return le64_to_cpu((__force __le64
)val
);
405 return be64_to_cpu((__force __be64
)val
);
408 static inline __virtio64
__cpu_to_virtio64(bool little_endian
, u64 val
)
411 return (__force __virtio64
)cpu_to_le64(val
);
413 return (__force __virtio64
)cpu_to_be64(val
);
417 * __virtio_test_bit - helper to test feature bits
419 * For use by transports. Devices should normally use virtio_has_feature,
420 * which includes more checks.
422 * @udev: the transport device
423 * @fbit: the feature bit
425 static inline bool __virtio_test_bit(struct udevice
*udev
, unsigned int fbit
)
427 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
429 /* Did you forget to fix assumptions on max features? */
430 if (__builtin_constant_p(fbit
))
431 BUILD_BUG_ON(fbit
>= 64);
435 return uc_priv
->features
& BIT_ULL(fbit
);
439 * __virtio_set_bit - helper to set feature bits
441 * For use by transports.
443 * @udev: the transport device
444 * @fbit: the feature bit
446 static inline void __virtio_set_bit(struct udevice
*udev
, unsigned int fbit
)
448 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
450 /* Did you forget to fix assumptions on max features? */
451 if (__builtin_constant_p(fbit
))
452 BUILD_BUG_ON(fbit
>= 64);
456 uc_priv
->features
|= BIT_ULL(fbit
);
460 * __virtio_clear_bit - helper to clear feature bits
462 * For use by transports.
464 * @vdev: the transport device
465 * @fbit: the feature bit
467 static inline void __virtio_clear_bit(struct udevice
*udev
, unsigned int fbit
)
469 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(udev
);
471 /* Did you forget to fix assumptions on max features? */
472 if (__builtin_constant_p(fbit
))
473 BUILD_BUG_ON(fbit
>= 64);
477 uc_priv
->features
&= ~BIT_ULL(fbit
);
481 * virtio_has_feature - helper to determine if this device has this feature
483 * Note this API is only usable after the virtio device driver's bind phase,
484 * as the feature has been negotiated between the device and the driver.
486 * @vdev: the virtio device
487 * @fbit: the feature bit
489 static inline bool virtio_has_feature(struct udevice
*vdev
, unsigned int fbit
)
491 if (!(vdev
->flags
& DM_FLAG_BOUND
))
494 return __virtio_test_bit(vdev
->parent
, fbit
);
497 static inline bool virtio_legacy_is_little_endian(void)
499 #ifdef __LITTLE_ENDIAN
506 static inline bool virtio_is_little_endian(struct udevice
*vdev
)
508 struct virtio_dev_priv
*uc_priv
= dev_get_uclass_priv(vdev
->parent
);
510 return !uc_priv
->legacy
|| virtio_legacy_is_little_endian();
513 /* Memory accessors */
514 static inline u16
virtio16_to_cpu(struct udevice
*vdev
, __virtio16 val
)
516 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
519 static inline __virtio16
cpu_to_virtio16(struct udevice
*vdev
, u16 val
)
521 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
524 static inline u32
virtio32_to_cpu(struct udevice
*vdev
, __virtio32 val
)
526 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
529 static inline __virtio32
cpu_to_virtio32(struct udevice
*vdev
, u32 val
)
531 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
534 static inline u64
virtio64_to_cpu(struct udevice
*vdev
, __virtio64 val
)
536 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
539 static inline __virtio64
cpu_to_virtio64(struct udevice
*vdev
, u64 val
)
541 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
544 /* Read @count fields, @bytes each */
545 static inline void __virtio_cread_many(struct udevice
*vdev
,
547 void *buf
, size_t count
, size_t bytes
)
552 /* no need to check return value as generation can be optional */
553 virtio_generation(vdev
, &gen
);
557 for (i
= 0; i
< count
; i
++)
558 virtio_get_config(vdev
, offset
+ bytes
* i
,
559 buf
+ i
* bytes
, bytes
);
561 virtio_generation(vdev
, &gen
);
562 } while (gen
!= old
);
565 static inline void virtio_cread_bytes(struct udevice
*vdev
,
567 void *buf
, size_t len
)
569 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
572 static inline u8
virtio_cread8(struct udevice
*vdev
, unsigned int offset
)
576 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
580 static inline void virtio_cwrite8(struct udevice
*vdev
,
581 unsigned int offset
, u8 val
)
583 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
586 static inline u16
virtio_cread16(struct udevice
*vdev
,
591 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
592 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
595 static inline void virtio_cwrite16(struct udevice
*vdev
,
596 unsigned int offset
, u16 val
)
598 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
599 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
602 static inline u32
virtio_cread32(struct udevice
*vdev
,
607 virtio_get_config(vdev
, offset
, &ret
, sizeof(ret
));
608 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
611 static inline void virtio_cwrite32(struct udevice
*vdev
,
612 unsigned int offset
, u32 val
)
614 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
615 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
618 static inline u64
virtio_cread64(struct udevice
*vdev
,
623 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
624 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
627 static inline void virtio_cwrite64(struct udevice
*vdev
,
628 unsigned int offset
, u64 val
)
630 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
631 virtio_set_config(vdev
, offset
, &val
, sizeof(val
));
634 /* Config space read accessor */
635 #define virtio_cread(vdev, structname, member, ptr) \
637 /* Must match the member's type, and be integer */ \
638 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
641 switch (sizeof(*ptr)) { \
643 *(ptr) = virtio_cread8(vdev, \
644 offsetof(structname, member)); \
647 *(ptr) = virtio_cread16(vdev, \
648 offsetof(structname, member)); \
651 *(ptr) = virtio_cread32(vdev, \
652 offsetof(structname, member)); \
655 *(ptr) = virtio_cread64(vdev, \
656 offsetof(structname, member)); \
663 /* Config space write accessor */
664 #define virtio_cwrite(vdev, structname, member, ptr) \
666 /* Must match the member's type, and be integer */ \
667 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
668 WARN_ON((*ptr) == 1); \
670 switch (sizeof(*ptr)) { \
672 virtio_cwrite8(vdev, \
673 offsetof(structname, member), \
677 virtio_cwrite16(vdev, \
678 offsetof(structname, member), \
682 virtio_cwrite32(vdev, \
683 offsetof(structname, member), \
687 virtio_cwrite64(vdev, \
688 offsetof(structname, member), \
696 /* Conditional config space accessors */
697 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
700 if (!virtio_has_feature(vdev, fbit)) \
703 virtio_cread(vdev, structname, member, ptr); \
707 #endif /* __VIRTIO_H__ */