blob: 46f9efb7321074d81302f55fdc88ea418d73b14e [file] [log] [blame]
Bin Meng8fb49b42018-10-15 02:21:00 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 *
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.
13 *
14 * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
15 * the VirtIO specification v1.0.
16 *
17 * This file is largely based on Linux kernel virtio_*.h files
18 */
19
20#ifndef __VIRTIO_H__
21#define __VIRTIO_H__
22
Simon Glasseb41d8a2020-05-10 11:40:08 -060023#include <linux/bug.h>
Bin Meng8fb49b42018-10-15 02:21:00 -070024#define VIRTIO_ID_NET 1 /* virtio net */
25#define VIRTIO_ID_BLOCK 2 /* virtio block */
Sughosh Ganu03018ea2019-12-29 15:30:14 +053026#define VIRTIO_ID_RNG 4 /* virtio rng */
27#define VIRTIO_ID_MAX_NUM 5
Bin Meng8fb49b42018-10-15 02:21:00 -070028
29#define VIRTIO_NET_DRV_NAME "virtio-net"
30#define VIRTIO_BLK_DRV_NAME "virtio-blk"
Sughosh Ganu03018ea2019-12-29 15:30:14 +053031#define VIRTIO_RNG_DRV_NAME "virtio-rng"
Bin Meng8fb49b42018-10-15 02:21:00 -070032
33/* Status byte for guest to report progress, and synchronize features */
34
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
47
48/*
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.
52 */
53#define VIRTIO_TRANSPORT_F_START 28
54#define VIRTIO_TRANSPORT_F_END 38
55
56#ifndef VIRTIO_CONFIG_NO_LEGACY
57/*
58 * Do we get callbacks when the ring is completely used,
59 * even if we've suppressed them?
60 */
61#define VIRTIO_F_NOTIFY_ON_EMPTY 24
62
63/* Can the device handle any descriptor layout? */
64#define VIRTIO_F_ANY_LAYOUT 27
65#endif /* VIRTIO_CONFIG_NO_LEGACY */
66
67/* v1.0 compliant */
68#define VIRTIO_F_VERSION_1 32
69
70/*
71 * If clear - device has the IOMMU bypass quirk feature.
72 * If set - use platform tools to detect the IOMMU.
73 *
74 * Note the reverse polarity (compared to most other features),
75 * this is for compatibility with legacy systems.
76 */
77#define VIRTIO_F_IOMMU_PLATFORM 33
78
79/* Does the device support Single Root I/O Virtualization? */
80#define VIRTIO_F_SR_IOV 37
81
82/**
83 * virtio scatter-gather struct
84 *
85 * @addr: sg buffer address
86 * @lengh: sg buffer length
87 */
88struct virtio_sg {
89 void *addr;
90 size_t length;
91};
92
93struct virtqueue;
94
95/* virtio bus operations */
96struct dm_virtio_ops {
97 /**
98 * get_config() - read the value of a configuration field
99 *
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
105 */
106 int (*get_config)(struct udevice *vdev, unsigned int offset,
107 void *buf, unsigned int len);
108 /**
109 * set_config() - write the value of a configuration field
110 *
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
116 */
117 int (*set_config)(struct udevice *vdev, unsigned int offset,
118 const void *buf, unsigned int len);
119 /**
120 * generation() - config generation counter
121 *
122 * @vdev: the real virtio device
123 * @counter: the returned config generation counter
124 * @return 0 if OK, -ve on error
125 */
126 int (*generation)(struct udevice *vdev, u32 *counter);
127 /**
128 * get_status() - read the status byte
129 *
130 * @vdev: the real virtio device
131 * @status: the returned status byte
132 * @return 0 if OK, -ve on error
133 */
134 int (*get_status)(struct udevice *vdev, u8 *status);
135 /**
136 * set_status() - write the status byte
137 *
138 * @vdev: the real virtio device
139 * @status: the new status byte
140 * @return 0 if OK, -ve on error
141 */
142 int (*set_status)(struct udevice *vdev, u8 status);
143 /**
144 * reset() - reset the device
145 *
146 * @vdev: the real virtio device
147 * @return 0 if OK, -ve on error
148 */
149 int (*reset)(struct udevice *vdev);
150 /**
151 * get_features() - get the array of feature bits for this device
152 *
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
156 */
157 int (*get_features)(struct udevice *vdev, u64 *features);
158 /**
159 * set_features() - confirm what device features we'll be using
160 *
161 * @vdev: the real virtio device
162 * @return 0 if OK, -ve on error
163 */
164 int (*set_features)(struct udevice *vdev);
165 /**
166 * find_vqs() - find virtqueues and instantiate them
167 *
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
172 */
173 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
174 struct virtqueue *vqs[]);
175 /**
176 * del_vqs() - free virtqueues found by find_vqs()
177 *
178 * @vdev: the real virtio device
179 * @return 0 if OK, -ve on error
180 */
181 int (*del_vqs)(struct udevice *vdev);
182 /**
183 * notify() - notify the device to process the queue
184 *
185 * @vdev: the real virtio device
186 * @vq: virtqueue to process
187 * @return 0 if OK, -ve on error
188 */
189 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
190};
191
192/* Get access to a virtio bus' operations */
193#define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
194
195/**
196 * virtio uclass per device private data
197 *
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
208 */
209struct virtio_dev_priv {
210 struct list_head vqs;
211 struct udevice *vdev;
212 bool legacy;
213 u32 device;
214 u32 vendor;
215 u64 features;
216 const u32 *feature_table;
217 u32 feature_table_size;
218 const u32 *feature_table_legacy;
219 u32 feature_table_size_legacy;
220};
221
222/**
223 * virtio_get_config() - read the value of a configuration field
224 *
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
230 */
231int virtio_get_config(struct udevice *vdev, unsigned int offset,
232 void *buf, unsigned int len);
233
234/**
235 * virtio_set_config() - write the value of a configuration field
236 *
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
242 */
243int virtio_set_config(struct udevice *vdev, unsigned int offset,
244 void *buf, unsigned int len);
245
246/**
247 * virtio_generation() - config generation counter
248 *
249 * @vdev: the real virtio device
250 * @counter: the returned config generation counter
251 * @return 0 if OK, -ve on error
252 */
253int virtio_generation(struct udevice *vdev, u32 *counter);
254
255/**
256 * virtio_get_status() - read the status byte
257 *
258 * @vdev: the real virtio device
259 * @status: the returned status byte
260 * @return 0 if OK, -ve on error
261 */
262int virtio_get_status(struct udevice *vdev, u8 *status);
263
264/**
265 * virtio_set_status() - write the status byte
266 *
267 * @vdev: the real virtio device
268 * @status: the new status byte
269 * @return 0 if OK, -ve on error
270 */
271int virtio_set_status(struct udevice *vdev, u8 status);
272
273/**
274 * virtio_reset() - reset the device
275 *
276 * @vdev: the real virtio device
277 * @return 0 if OK, -ve on error
278 */
279int virtio_reset(struct udevice *vdev);
280
281/**
282 * virtio_get_features() - get the array of feature bits for this device
283 *
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
287 */
288int virtio_get_features(struct udevice *vdev, u64 *features);
289
290/**
291 * virtio_set_features() - confirm what device features we'll be using
292 *
293 * @vdev: the real virtio device
294 * @return 0 if OK, -ve on error
295 */
296int virtio_set_features(struct udevice *vdev);
297
298/**
299 * virtio_find_vqs() - find virtqueues and instantiate them
300 *
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
305 */
306int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
307 struct virtqueue *vqs[]);
308
309/**
310 * virtio_del_vqs() - free virtqueues found by find_vqs()
311 *
312 * @vdev: the real virtio device
313 * @return 0 if OK, -ve on error
314 */
315int virtio_del_vqs(struct udevice *vdev);
316
317/**
318 * virtio_notify() - notify the device to process the queue
319 *
320 * @vdev: the real virtio device
321 * @vq: virtqueue to process
322 * @return 0 if OK, -ve on error
323 */
324int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
325
326/**
327 * virtio_add_status() - helper to set a new status code to the device
328 *
329 * @vdev: the real virtio device
330 * @status: new status code to be added
331 */
332void virtio_add_status(struct udevice *vdev, u8 status);
333
334/**
335 * virtio_finalize_features() - helper to finalize features
336 *
337 * @vdev: the real virtio device
338 * @return 0 if OK, -ve on error
339 */
340int virtio_finalize_features(struct udevice *vdev);
341
342/**
343 * virtio_driver_features_init() - initialize driver supported features
344 *
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.
347 *
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.
351 *
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
357 */
358void virtio_driver_features_init(struct virtio_dev_priv *priv,
359 const u32 *feature,
360 u32 feature_size,
361 const u32 *feature_legacy,
362 u32 feature_legacy_size);
363
364/**
365 * virtio_init() - helper to enumerate all known virtio devices
366 *
367 * @return 0 if OK, -ve on error
368 */
369int virtio_init(void);
370
371static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
372{
373 if (little_endian)
374 return le16_to_cpu((__force __le16)val);
375 else
376 return be16_to_cpu((__force __be16)val);
377}
378
379static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
380{
381 if (little_endian)
382 return (__force __virtio16)cpu_to_le16(val);
383 else
384 return (__force __virtio16)cpu_to_be16(val);
385}
386
387static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
388{
389 if (little_endian)
390 return le32_to_cpu((__force __le32)val);
391 else
392 return be32_to_cpu((__force __be32)val);
393}
394
395static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
396{
397 if (little_endian)
398 return (__force __virtio32)cpu_to_le32(val);
399 else
400 return (__force __virtio32)cpu_to_be32(val);
401}
402
403static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
404{
405 if (little_endian)
406 return le64_to_cpu((__force __le64)val);
407 else
408 return be64_to_cpu((__force __be64)val);
409}
410
411static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
412{
413 if (little_endian)
414 return (__force __virtio64)cpu_to_le64(val);
415 else
416 return (__force __virtio64)cpu_to_be64(val);
417}
418
419/**
420 * __virtio_test_bit - helper to test feature bits
421 *
422 * For use by transports. Devices should normally use virtio_has_feature,
423 * which includes more checks.
424 *
425 * @udev: the transport device
426 * @fbit: the feature bit
427 */
428static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
429{
430 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
431
432 /* Did you forget to fix assumptions on max features? */
433 if (__builtin_constant_p(fbit))
434 BUILD_BUG_ON(fbit >= 64);
435 else
436 WARN_ON(fbit >= 64);
437
438 return uc_priv->features & BIT_ULL(fbit);
439}
440
441/**
442 * __virtio_set_bit - helper to set feature bits
443 *
444 * For use by transports.
445 *
446 * @udev: the transport device
447 * @fbit: the feature bit
448 */
449static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
450{
451 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
452
453 /* Did you forget to fix assumptions on max features? */
454 if (__builtin_constant_p(fbit))
455 BUILD_BUG_ON(fbit >= 64);
456 else
457 WARN_ON(fbit >= 64);
458
459 uc_priv->features |= BIT_ULL(fbit);
460}
461
462/**
463 * __virtio_clear_bit - helper to clear feature bits
464 *
465 * For use by transports.
466 *
467 * @vdev: the transport device
468 * @fbit: the feature bit
469 */
470static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
471{
472 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
473
474 /* Did you forget to fix assumptions on max features? */
475 if (__builtin_constant_p(fbit))
476 BUILD_BUG_ON(fbit >= 64);
477 else
478 WARN_ON(fbit >= 64);
479
480 uc_priv->features &= ~BIT_ULL(fbit);
481}
482
483/**
484 * virtio_has_feature - helper to determine if this device has this feature
485 *
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.
488 *
489 * @vdev: the virtio device
490 * @fbit: the feature bit
491 */
492static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
493{
494 if (!(vdev->flags & DM_FLAG_BOUND))
495 WARN_ON(true);
496
497 return __virtio_test_bit(vdev->parent, fbit);
498}
499
500static inline bool virtio_legacy_is_little_endian(void)
501{
502#ifdef __LITTLE_ENDIAN
503 return true;
504#else
505 return false;
506#endif
507}
508
509static inline bool virtio_is_little_endian(struct udevice *vdev)
510{
511 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
512
513 return !uc_priv->legacy || virtio_legacy_is_little_endian();
514}
515
516/* Memory accessors */
517static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
518{
519 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
520}
521
522static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
523{
524 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
525}
526
527static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
528{
529 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
530}
531
532static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
533{
534 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
535}
536
537static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
538{
539 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
540}
541
542static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
543{
544 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
545}
546
547/* Read @count fields, @bytes each */
548static inline void __virtio_cread_many(struct udevice *vdev,
549 unsigned int offset,
550 void *buf, size_t count, size_t bytes)
551{
552 u32 old, gen;
553 int i;
554
555 /* no need to check return value as generation can be optional */
556 virtio_generation(vdev, &gen);
557 do {
558 old = gen;
559
560 for (i = 0; i < count; i++)
561 virtio_get_config(vdev, offset + bytes * i,
562 buf + i * bytes, bytes);
563
564 virtio_generation(vdev, &gen);
565 } while (gen != old);
566}
567
568static inline void virtio_cread_bytes(struct udevice *vdev,
569 unsigned int offset,
570 void *buf, size_t len)
571{
572 __virtio_cread_many(vdev, offset, buf, len, 1);
573}
574
575static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
576{
577 u8 ret;
578
579 virtio_get_config(vdev, offset, &ret, sizeof(ret));
580 return ret;
581}
582
583static inline void virtio_cwrite8(struct udevice *vdev,
584 unsigned int offset, u8 val)
585{
586 virtio_set_config(vdev, offset, &val, sizeof(val));
587}
588
589static inline u16 virtio_cread16(struct udevice *vdev,
590 unsigned int offset)
591{
592 u16 ret;
593
594 virtio_get_config(vdev, offset, &ret, sizeof(ret));
595 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
596}
597
598static inline void virtio_cwrite16(struct udevice *vdev,
599 unsigned int offset, u16 val)
600{
601 val = (__force u16)cpu_to_virtio16(vdev, val);
602 virtio_set_config(vdev, offset, &val, sizeof(val));
603}
604
605static inline u32 virtio_cread32(struct udevice *vdev,
606 unsigned int offset)
607{
608 u32 ret;
609
610 virtio_get_config(vdev, offset, &ret, sizeof(ret));
611 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
612}
613
614static inline void virtio_cwrite32(struct udevice *vdev,
615 unsigned int offset, u32 val)
616{
617 val = (__force u32)cpu_to_virtio32(vdev, val);
618 virtio_set_config(vdev, offset, &val, sizeof(val));
619}
620
621static inline u64 virtio_cread64(struct udevice *vdev,
622 unsigned int offset)
623{
624 u64 ret;
625
626 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
627 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
628}
629
630static inline void virtio_cwrite64(struct udevice *vdev,
631 unsigned int offset, u64 val)
632{
633 val = (__force u64)cpu_to_virtio64(vdev, val);
634 virtio_set_config(vdev, offset, &val, sizeof(val));
635}
636
637/* Config space read accessor */
638#define virtio_cread(vdev, structname, member, ptr) \
639 do { \
640 /* Must match the member's type, and be integer */ \
641 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
642 (*ptr) = 1; \
643 \
644 switch (sizeof(*ptr)) { \
645 case 1: \
646 *(ptr) = virtio_cread8(vdev, \
647 offsetof(structname, member)); \
648 break; \
649 case 2: \
650 *(ptr) = virtio_cread16(vdev, \
651 offsetof(structname, member)); \
652 break; \
653 case 4: \
654 *(ptr) = virtio_cread32(vdev, \
655 offsetof(structname, member)); \
656 break; \
657 case 8: \
658 *(ptr) = virtio_cread64(vdev, \
659 offsetof(structname, member)); \
660 break; \
661 default: \
662 WARN_ON(true); \
663 } \
664 } while (0)
665
666/* Config space write accessor */
667#define virtio_cwrite(vdev, structname, member, ptr) \
668 do { \
669 /* Must match the member's type, and be integer */ \
670 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
671 WARN_ON((*ptr) == 1); \
672 \
673 switch (sizeof(*ptr)) { \
674 case 1: \
675 virtio_cwrite8(vdev, \
676 offsetof(structname, member), \
677 *(ptr)); \
678 break; \
679 case 2: \
680 virtio_cwrite16(vdev, \
681 offsetof(structname, member), \
682 *(ptr)); \
683 break; \
684 case 4: \
685 virtio_cwrite32(vdev, \
686 offsetof(structname, member), \
687 *(ptr)); \
688 break; \
689 case 8: \
690 virtio_cwrite64(vdev, \
691 offsetof(structname, member), \
692 *(ptr)); \
693 break; \
694 default: \
695 WARN_ON(true); \
696 } \
697 } while (0)
698
699/* Conditional config space accessors */
700#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
701 ({ \
702 int _r = 0; \
703 if (!virtio_has_feature(vdev, fbit)) \
704 _r = -ENOENT; \
705 else \
706 virtio_cread(vdev, structname, member, ptr); \
707 _r; \
708 })
709
710#endif /* __VIRTIO_H__ */