1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef __VIRTIO_H__
21#define __VIRTIO_H__
22
23#define VIRTIO_ID_NET 1
24#define VIRTIO_ID_BLOCK 2
25#define VIRTIO_ID_MAX_NUM 3
26
27#define VIRTIO_NET_DRV_NAME "virtio-net"
28#define VIRTIO_BLK_DRV_NAME "virtio-blk"
29
30
31
32
33#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
34
35#define VIRTIO_CONFIG_S_DRIVER 2
36
37#define VIRTIO_CONFIG_S_DRIVER_OK 4
38
39#define VIRTIO_CONFIG_S_FEATURES_OK 8
40
41#define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
42
43#define VIRTIO_CONFIG_S_FAILED 0x80
44
45
46
47
48
49
50#define VIRTIO_TRANSPORT_F_START 28
51#define VIRTIO_TRANSPORT_F_END 38
52
53#ifndef VIRTIO_CONFIG_NO_LEGACY
54
55
56
57
58#define VIRTIO_F_NOTIFY_ON_EMPTY 24
59
60
61#define VIRTIO_F_ANY_LAYOUT 27
62#endif
63
64
65#define VIRTIO_F_VERSION_1 32
66
67
68
69
70
71
72
73
74#define VIRTIO_F_IOMMU_PLATFORM 33
75
76
77#define VIRTIO_F_SR_IOV 37
78
79
80
81
82
83
84
85struct virtio_sg {
86 void *addr;
87 size_t length;
88};
89
90struct virtqueue;
91
92
93struct dm_virtio_ops {
94
95
96
97
98
99
100
101
102
103 int (*get_config)(struct udevice *vdev, unsigned int offset,
104 void *buf, unsigned int len);
105
106
107
108
109
110
111
112
113
114 int (*set_config)(struct udevice *vdev, unsigned int offset,
115 const void *buf, unsigned int len);
116
117
118
119
120
121
122
123 int (*generation)(struct udevice *vdev, u32 *counter);
124
125
126
127
128
129
130
131 int (*get_status)(struct udevice *vdev, u8 *status);
132
133
134
135
136
137
138
139 int (*set_status)(struct udevice *vdev, u8 status);
140
141
142
143
144
145
146 int (*reset)(struct udevice *vdev);
147
148
149
150
151
152
153
154 int (*get_features)(struct udevice *vdev, u64 *features);
155
156
157
158
159
160
161 int (*set_features)(struct udevice *vdev);
162
163
164
165
166
167
168
169
170 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
171 struct virtqueue *vqs[]);
172
173
174
175
176
177
178 int (*del_vqs)(struct udevice *vdev);
179
180
181
182
183
184
185
186 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
187};
188
189
190#define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206struct virtio_dev_priv {
207 struct list_head vqs;
208 struct udevice *vdev;
209 bool legacy;
210 u32 device;
211 u32 vendor;
212 u64 features;
213 const u32 *feature_table;
214 u32 feature_table_size;
215 const u32 *feature_table_legacy;
216 u32 feature_table_size_legacy;
217};
218
219
220
221
222
223
224
225
226
227
228int virtio_get_config(struct udevice *vdev, unsigned int offset,
229 void *buf, unsigned int len);
230
231
232
233
234
235
236
237
238
239
240int virtio_set_config(struct udevice *vdev, unsigned int offset,
241 void *buf, unsigned int len);
242
243
244
245
246
247
248
249
250int virtio_generation(struct udevice *vdev, u32 *counter);
251
252
253
254
255
256
257
258
259int virtio_get_status(struct udevice *vdev, u8 *status);
260
261
262
263
264
265
266
267
268int virtio_set_status(struct udevice *vdev, u8 status);
269
270
271
272
273
274
275
276int virtio_reset(struct udevice *vdev);
277
278
279
280
281
282
283
284
285int virtio_get_features(struct udevice *vdev, u64 *features);
286
287
288
289
290
291
292
293int virtio_set_features(struct udevice *vdev);
294
295
296
297
298
299
300
301
302
303int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
304 struct virtqueue *vqs[]);
305
306
307
308
309
310
311
312int virtio_del_vqs(struct udevice *vdev);
313
314
315
316
317
318
319
320
321int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
322
323
324
325
326
327
328
329void virtio_add_status(struct udevice *vdev, u8 status);
330
331
332
333
334
335
336
337int virtio_finalize_features(struct udevice *vdev);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355void virtio_driver_features_init(struct virtio_dev_priv *priv,
356 const u32 *feature,
357 u32 feature_size,
358 const u32 *feature_legacy,
359 u32 feature_legacy_size);
360
361
362
363
364
365
366int virtio_init(void);
367
368static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
369{
370 if (little_endian)
371 return le16_to_cpu((__force __le16)val);
372 else
373 return be16_to_cpu((__force __be16)val);
374}
375
376static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
377{
378 if (little_endian)
379 return (__force __virtio16)cpu_to_le16(val);
380 else
381 return (__force __virtio16)cpu_to_be16(val);
382}
383
384static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
385{
386 if (little_endian)
387 return le32_to_cpu((__force __le32)val);
388 else
389 return be32_to_cpu((__force __be32)val);
390}
391
392static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
393{
394 if (little_endian)
395 return (__force __virtio32)cpu_to_le32(val);
396 else
397 return (__force __virtio32)cpu_to_be32(val);
398}
399
400static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
401{
402 if (little_endian)
403 return le64_to_cpu((__force __le64)val);
404 else
405 return be64_to_cpu((__force __be64)val);
406}
407
408static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
409{
410 if (little_endian)
411 return (__force __virtio64)cpu_to_le64(val);
412 else
413 return (__force __virtio64)cpu_to_be64(val);
414}
415
416
417
418
419
420
421
422
423
424
425static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
426{
427 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
428
429
430 if (__builtin_constant_p(fbit))
431 BUILD_BUG_ON(fbit >= 64);
432 else
433 WARN_ON(fbit >= 64);
434
435 return uc_priv->features & BIT_ULL(fbit);
436}
437
438
439
440
441
442
443
444
445
446static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
447{
448 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
449
450
451 if (__builtin_constant_p(fbit))
452 BUILD_BUG_ON(fbit >= 64);
453 else
454 WARN_ON(fbit >= 64);
455
456 uc_priv->features |= BIT_ULL(fbit);
457}
458
459
460
461
462
463
464
465
466
467static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
468{
469 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
470
471
472 if (__builtin_constant_p(fbit))
473 BUILD_BUG_ON(fbit >= 64);
474 else
475 WARN_ON(fbit >= 64);
476
477 uc_priv->features &= ~BIT_ULL(fbit);
478}
479
480
481
482
483
484
485
486
487
488
489static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
490{
491 if (!(vdev->flags & DM_FLAG_BOUND))
492 WARN_ON(true);
493
494 return __virtio_test_bit(vdev->parent, fbit);
495}
496
497static inline bool virtio_legacy_is_little_endian(void)
498{
499#ifdef __LITTLE_ENDIAN
500 return true;
501#else
502 return false;
503#endif
504}
505
506static inline bool virtio_is_little_endian(struct udevice *vdev)
507{
508 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
509
510 return !uc_priv->legacy || virtio_legacy_is_little_endian();
511}
512
513
514static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
515{
516 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
517}
518
519static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
520{
521 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
522}
523
524static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
525{
526 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
527}
528
529static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
530{
531 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
532}
533
534static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
535{
536 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
537}
538
539static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
540{
541 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
542}
543
544
545static inline void __virtio_cread_many(struct udevice *vdev,
546 unsigned int offset,
547 void *buf, size_t count, size_t bytes)
548{
549 u32 old, gen;
550 int i;
551
552
553 virtio_generation(vdev, &gen);
554 do {
555 old = gen;
556
557 for (i = 0; i < count; i++)
558 virtio_get_config(vdev, offset + bytes * i,
559 buf + i * bytes, bytes);
560
561 virtio_generation(vdev, &gen);
562 } while (gen != old);
563}
564
565static inline void virtio_cread_bytes(struct udevice *vdev,
566 unsigned int offset,
567 void *buf, size_t len)
568{
569 __virtio_cread_many(vdev, offset, buf, len, 1);
570}
571
572static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
573{
574 u8 ret;
575
576 virtio_get_config(vdev, offset, &ret, sizeof(ret));
577 return ret;
578}
579
580static inline void virtio_cwrite8(struct udevice *vdev,
581 unsigned int offset, u8 val)
582{
583 virtio_set_config(vdev, offset, &val, sizeof(val));
584}
585
586static inline u16 virtio_cread16(struct udevice *vdev,
587 unsigned int offset)
588{
589 u16 ret;
590
591 virtio_get_config(vdev, offset, &ret, sizeof(ret));
592 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
593}
594
595static inline void virtio_cwrite16(struct udevice *vdev,
596 unsigned int offset, u16 val)
597{
598 val = (__force u16)cpu_to_virtio16(vdev, val);
599 virtio_set_config(vdev, offset, &val, sizeof(val));
600}
601
602static inline u32 virtio_cread32(struct udevice *vdev,
603 unsigned int offset)
604{
605 u32 ret;
606
607 virtio_get_config(vdev, offset, &ret, sizeof(ret));
608 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
609}
610
611static inline void virtio_cwrite32(struct udevice *vdev,
612 unsigned int offset, u32 val)
613{
614 val = (__force u32)cpu_to_virtio32(vdev, val);
615 virtio_set_config(vdev, offset, &val, sizeof(val));
616}
617
618static inline u64 virtio_cread64(struct udevice *vdev,
619 unsigned int offset)
620{
621 u64 ret;
622
623 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
624 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
625}
626
627static inline void virtio_cwrite64(struct udevice *vdev,
628 unsigned int offset, u64 val)
629{
630 val = (__force u64)cpu_to_virtio64(vdev, val);
631 virtio_set_config(vdev, offset, &val, sizeof(val));
632}
633
634
635#define virtio_cread(vdev, structname, member, ptr) \
636 do { \
637 \
638 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
639 (*ptr) = 1; \
640 \
641 switch (sizeof(*ptr)) { \
642 case 1: \
643 *(ptr) = virtio_cread8(vdev, \
644 offsetof(structname, member)); \
645 break; \
646 case 2: \
647 *(ptr) = virtio_cread16(vdev, \
648 offsetof(structname, member)); \
649 break; \
650 case 4: \
651 *(ptr) = virtio_cread32(vdev, \
652 offsetof(structname, member)); \
653 break; \
654 case 8: \
655 *(ptr) = virtio_cread64(vdev, \
656 offsetof(structname, member)); \
657 break; \
658 default: \
659 WARN_ON(true); \
660 } \
661 } while (0)
662
663
664#define virtio_cwrite(vdev, structname, member, ptr) \
665 do { \
666 \
667 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
668 WARN_ON((*ptr) == 1); \
669 \
670 switch (sizeof(*ptr)) { \
671 case 1: \
672 virtio_cwrite8(vdev, \
673 offsetof(structname, member), \
674 *(ptr)); \
675 break; \
676 case 2: \
677 virtio_cwrite16(vdev, \
678 offsetof(structname, member), \
679 *(ptr)); \
680 break; \
681 case 4: \
682 virtio_cwrite32(vdev, \
683 offsetof(structname, member), \
684 *(ptr)); \
685 break; \
686 case 8: \
687 virtio_cwrite64(vdev, \
688 offsetof(structname, member), \
689 *(ptr)); \
690 break; \
691 default: \
692 WARN_ON(true); \
693 } \
694 } while (0)
695
696
697#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
698 ({ \
699 int _r = 0; \
700 if (!virtio_has_feature(vdev, fbit)) \
701 _r = -ENOENT; \
702 else \
703 virtio_cread(vdev, structname, member, ptr); \
704 _r; \
705 })
706
707#endif
708