1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
|
/** @file
A non-transitional driver for VirtIo 1.0 PCI devices.
Copyright (C) 2016, Red Hat, Inc.
Copyright (C) 2017, AMD Inc, All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <IndustryStandard/Pci.h>
#include <IndustryStandard/Virtio.h>
#include <Protocol/PciIo.h>
#include <Protocol/PciRootBridgeIo.h>
#include <Protocol/VirtioDevice.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PciCapLib.h>
#include <Library/PciCapPciIoLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include "Virtio10.h"
//
// Utility functions
//
/**
Transfer data between the caller and a register in a virtio-1.0 register
block.
@param[in] PciIo The EFI_PCI_IO_PROTOCOL instance that represents
the device.
@param[in] Config The "fat pointer" structure that identifies the
register block to access.
@param[in] Write TRUE if the register should be written, FALSE if
the register should be read.
@param[in] FieldOffset The offset of the register within the register
block.
@param[in] FieldSize The size of the register within the register
block. Can be one of 1, 2, 4 and 8. Accesses to
8-byte registers are broken up into two 4-byte
accesses.
@param[in,out] Buffer When Write is TRUE, the register is written with
data from Buffer. When Write is FALSE, the caller
receives the register value into Buffer.
@retval EFI_SUCCESS Register access successful.
@retval EFI_INVALID_PARAMETER The register block pointed-to by Config
doesn't exist; or FieldOffset and FieldSize
would overflow the register block; or
FieldSize is invalid.
@return Error codes from
EFI_PCI_IO_PROTOCOL.(Io|Mem).(Read|Write)
member functions.
**/
STATIC
EFI_STATUS
Virtio10Transfer (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN VIRTIO_1_0_CONFIG *Config,
IN BOOLEAN Write,
IN UINTN FieldOffset,
IN UINTN FieldSize,
IN OUT VOID *Buffer
)
{
UINTN Count;
EFI_PCI_IO_PROTOCOL_WIDTH Width;
EFI_PCI_IO_PROTOCOL_ACCESS *BarType;
EFI_PCI_IO_PROTOCOL_IO_MEM Access;
if (!Config->Exists ||
(FieldSize > Config->Length) ||
(FieldOffset > Config->Length - FieldSize))
{
return EFI_INVALID_PARAMETER;
}
Count = 1;
switch (FieldSize) {
case 1:
Width = EfiPciIoWidthUint8;
break;
case 2:
Width = EfiPciIoWidthUint16;
break;
case 8:
Count = 2;
//
// fall through
//
case 4:
Width = EfiPciIoWidthUint32;
break;
default:
return EFI_INVALID_PARAMETER;
}
BarType = (Config->BarType == Virtio10BarTypeMem) ? &PciIo->Mem : &PciIo->Io;
Access = Write ? BarType->Write : BarType->Read;
return Access (
PciIo,
Width,
Config->Bar,
Config->Offset + FieldOffset,
Count,
Buffer
);
}
/**
Determine if a PCI BAR is IO or MMIO.
@param[in] PciIo The EFI_PCI_IO_PROTOCOL instance that represents the
device.
@param[in] BarIndex The number of the BAR whose type the caller is
interested in.
@param[out] BarType On output, a VIRTIO_1_0_BAR_TYPE value that gives the
type of the BAR.
@retval EFI_SUCCESS The BAR type has been recognized and stored in
BarType.
@retval EFI_UNSUPPORTED The BAR type couldn't be identified.
@return Error codes from
EFI_PCI_IO_PROTOCOL.GetBarAttributes().
**/
STATIC
EFI_STATUS
GetBarType (
IN EFI_PCI_IO_PROTOCOL *PciIo,
IN UINT8 BarIndex,
OUT VIRTIO_1_0_BAR_TYPE *BarType
)
{
EFI_STATUS Status;
VOID *Resources;
Status = PciIo->GetBarAttributes (PciIo, BarIndex, NULL, &Resources);
if (EFI_ERROR (Status)) {
return Status;
}
Status = EFI_UNSUPPORTED;
if (*(UINT8 *)Resources == ACPI_QWORD_ADDRESS_SPACE_DESCRIPTOR) {
EFI_ACPI_QWORD_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
Descriptor = Resources;
switch (Descriptor->ResType) {
case ACPI_ADDRESS_SPACE_TYPE_MEM:
*BarType = Virtio10BarTypeMem;
Status = EFI_SUCCESS;
break;
case ACPI_ADDRESS_SPACE_TYPE_IO:
*BarType = Virtio10BarTypeIo;
Status = EFI_SUCCESS;
break;
default:
break;
}
}
FreePool (Resources);
return Status;
}
/*
Traverse the PCI capabilities list of a virtio-1.0 device, and capture the
locations of the interesting virtio-1.0 register blocks.
@param[in,out] Device The VIRTIO_1_0_DEV structure that identifies
the device. On input, the caller is responsible
that the Device->PciIo member be live, and that
the CommonConfig, NotifyConfig,
NotifyOffsetMultiplier and SpecificConfig
members be zeroed. On output, said members
will have been updated from the PCI
capabilities found.
@retval EFI_SUCCESS Traversal successful.
@return Error codes from PciCapPciIoLib, PciCapLib, and the
GetBarType() helper function.
*/
STATIC
EFI_STATUS
ParseCapabilities (
IN OUT VIRTIO_1_0_DEV *Device
)
{
EFI_STATUS Status;
PCI_CAP_DEV *PciDevice;
PCI_CAP_LIST *CapList;
UINT16 VendorInstance;
PCI_CAP *VendorCap;
Status = PciCapPciIoDeviceInit (Device->PciIo, &PciDevice);
if (EFI_ERROR (Status)) {
return Status;
}
Status = PciCapListInit (PciDevice, &CapList);
if (EFI_ERROR (Status)) {
goto UninitPciDevice;
}
for (VendorInstance = 0;
!EFI_ERROR (
PciCapListFindCap (
CapList,
PciCapNormal,
EFI_PCI_CAPABILITY_ID_VENDOR,
VendorInstance,
&VendorCap
)
);
VendorInstance++)
{
UINT8 CapLen;
VIRTIO_PCI_CAP VirtIoCap;
VIRTIO_1_0_CONFIG *ParsedConfig;
//
// Big enough to accommodate a VIRTIO_PCI_CAP structure?
//
Status = PciCapRead (
PciDevice,
VendorCap,
OFFSET_OF (EFI_PCI_CAPABILITY_VENDOR_HDR, Length),
&CapLen,
sizeof CapLen
);
if (EFI_ERROR (Status)) {
goto UninitCapList;
}
if (CapLen < sizeof VirtIoCap) {
//
// Too small, move to next.
//
continue;
}
//
// Read interesting part of capability.
//
Status = PciCapRead (PciDevice, VendorCap, 0, &VirtIoCap, sizeof VirtIoCap);
if (EFI_ERROR (Status)) {
goto UninitCapList;
}
switch (VirtIoCap.ConfigType) {
case VIRTIO_PCI_CAP_COMMON_CFG:
ParsedConfig = &Device->CommonConfig;
break;
case VIRTIO_PCI_CAP_NOTIFY_CFG:
ParsedConfig = &Device->NotifyConfig;
break;
case VIRTIO_PCI_CAP_DEVICE_CFG:
ParsedConfig = &Device->SpecificConfig;
break;
default:
//
// Capability is not interesting.
//
continue;
}
//
// Save the location of the register block into ParsedConfig.
//
Status = GetBarType (Device->PciIo, VirtIoCap.Bar, &ParsedConfig->BarType);
if (EFI_ERROR (Status)) {
goto UninitCapList;
}
ParsedConfig->Bar = VirtIoCap.Bar;
ParsedConfig->Offset = VirtIoCap.Offset;
ParsedConfig->Length = VirtIoCap.Length;
if (VirtIoCap.ConfigType == VIRTIO_PCI_CAP_NOTIFY_CFG) {
//
// This capability has an additional field called NotifyOffsetMultiplier;
// parse it too.
//
if (CapLen < sizeof VirtIoCap + sizeof Device->NotifyOffsetMultiplier) {
//
// Too small, move to next.
//
continue;
}
Status = PciCapRead (
PciDevice,
VendorCap,
sizeof VirtIoCap,
&Device->NotifyOffsetMultiplier,
sizeof Device->NotifyOffsetMultiplier
);
if (EFI_ERROR (Status)) {
goto UninitCapList;
}
}
//
// Capability parsed successfully.
//
ParsedConfig->Exists = TRUE;
}
ASSERT_EFI_ERROR (Status);
UninitCapList:
PciCapListUninit (CapList);
UninitPciDevice:
PciCapPciIoDeviceUninit (PciDevice);
return Status;
}
/**
Accumulate the BAR type of a virtio-1.0 register block into a UINT64
attribute map, such that the latter is suitable for enabling IO / MMIO
decoding with EFI_PCI_IO_PROTOCOL.Attributes().
@param[in] Config The "fat pointer" structure that identifies the
register block. It is allowed for the register
block not to exist.
@param[in,out] Attributes On output, if the register block exists,
EFI_PCI_IO_ATTRIBUTE_MEMORY or
EFI_PCI_IO_ATTRIBUTE_IO is OR-ed into Attributes,
according to the register block's BAR type.
**/
STATIC
VOID
UpdateAttributes (
IN VIRTIO_1_0_CONFIG *Config,
IN OUT UINT64 *Attributes
)
{
if (Config->Exists) {
*Attributes |= (Config->BarType == Virtio10BarTypeMem) ?
EFI_PCI_IO_ATTRIBUTE_MEMORY :
EFI_PCI_IO_ATTRIBUTE_IO;
}
}
//
// VIRTIO_DEVICE_PROTOCOL member functions
//
STATIC
EFI_STATUS
EFIAPI
Virtio10GetDeviceFeatures (
IN VIRTIO_DEVICE_PROTOCOL *This,
OUT UINT64 *DeviceFeatures
)
{
VIRTIO_1_0_DEV *Dev;
UINT32 Selector;
UINT32 Features32[2];
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
for (Selector = 0; Selector < 2; ++Selector) {
EFI_STATUS Status;
//
// Select the low or high half of the features.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DeviceFeatureSelect),
sizeof Selector,
&Selector
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Fetch that half.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
FALSE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DeviceFeature),
sizeof Features32[Selector],
&Features32[Selector]
);
if (EFI_ERROR (Status)) {
return Status;
}
}
*DeviceFeatures = LShiftU64 (Features32[1], 32) | Features32[0];
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetGuestFeatures (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT64 Features
)
{
VIRTIO_1_0_DEV *Dev;
UINT32 Selector;
UINT32 Features32[2];
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Features32[0] = (UINT32)Features;
Features32[1] = (UINT32)RShiftU64 (Features, 32);
for (Selector = 0; Selector < 2; ++Selector) {
EFI_STATUS Status;
//
// Select the low or high half of the features.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DriverFeatureSelect),
sizeof Selector,
&Selector
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Write that half.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DriverFeature),
sizeof Features32[Selector],
&Features32[Selector]
);
if (EFI_ERROR (Status)) {
return Status;
}
}
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetQueueAddress (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN VRING *Ring,
IN UINT64 RingBaseShift
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
UINT64 Address;
UINT16 Enable;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Address = (UINTN)Ring->Desc;
Address += RingBaseShift;
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueDesc),
sizeof Address,
&Address
);
if (EFI_ERROR (Status)) {
return Status;
}
Address = (UINTN)Ring->Avail.Flags;
Address += RingBaseShift;
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueAvail),
sizeof Address,
&Address
);
if (EFI_ERROR (Status)) {
return Status;
}
Address = (UINTN)Ring->Used.Flags;
Address += RingBaseShift;
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueUsed),
sizeof Address,
&Address
);
if (EFI_ERROR (Status)) {
return Status;
}
Enable = 1;
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueEnable),
sizeof Enable,
&Enable
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetQueueSel (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT16 Index
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueSelect),
sizeof Index,
&Index
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetQueueNotify (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT16 Index
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
UINT16 SavedQueueSelect;
UINT16 NotifyOffset;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
//
// Read NotifyOffset first. NotifyOffset is queue specific, so we have
// to stash & restore the current queue selector around it.
//
// So, start with saving the current queue selector.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
FALSE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueSelect),
sizeof SavedQueueSelect,
&SavedQueueSelect
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Select the requested queue.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueSelect),
sizeof Index,
&Index
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Read the QueueNotifyOff field.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
FALSE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueNotifyOff),
sizeof NotifyOffset,
&NotifyOffset
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Re-select the original queue.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueSelect),
sizeof SavedQueueSelect,
&SavedQueueSelect
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// We can now kick the queue.
//
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->NotifyConfig,
TRUE,
NotifyOffset * Dev->NotifyOffsetMultiplier,
sizeof Index,
&Index
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetQueueAlign (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT32 Alignment
)
{
return (Alignment == EFI_PAGE_SIZE) ? EFI_SUCCESS : EFI_UNSUPPORTED;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetPageSize (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT32 PageSize
)
{
return (PageSize == EFI_PAGE_SIZE) ? EFI_SUCCESS : EFI_UNSUPPORTED;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10GetQueueNumMax (
IN VIRTIO_DEVICE_PROTOCOL *This,
OUT UINT16 *QueueNumMax
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
FALSE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, QueueSize),
sizeof *QueueNumMax,
QueueNumMax
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetQueueNum (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT16 QueueSize
)
{
EFI_STATUS Status;
UINT16 CurrentSize;
//
// This member function is required for VirtIo MMIO, and a no-op in
// VirtIo PCI 0.9.5. In VirtIo 1.0, drivers can theoretically use this
// member to reduce memory consumption, but none of our drivers do. So
// just check that they set the size that is already in effect.
//
Status = Virtio10GetQueueNumMax (This, &CurrentSize);
if (EFI_ERROR (Status)) {
return Status;
}
return (CurrentSize == QueueSize) ? EFI_SUCCESS : EFI_UNSUPPORTED;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10GetDeviceStatus (
IN VIRTIO_DEVICE_PROTOCOL *This,
OUT UINT8 *DeviceStatus
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
FALSE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DeviceStatus),
sizeof *DeviceStatus,
DeviceStatus
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10SetDeviceStatus (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINT8 DeviceStatus
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->CommonConfig,
TRUE,
OFFSET_OF (VIRTIO_PCI_COMMON_CFG, DeviceStatus),
sizeof DeviceStatus,
&DeviceStatus
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10WriteDevice (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINTN FieldOffset,
IN UINTN FieldSize,
IN UINT64 Value
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->SpecificConfig,
TRUE,
FieldOffset,
FieldSize,
&Value
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10ReadDevice (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINTN FieldOffset,
IN UINTN FieldSize,
IN UINTN BufferSize,
OUT VOID *Buffer
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
if (FieldSize != BufferSize) {
return EFI_INVALID_PARAMETER;
}
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Virtio10Transfer (
Dev->PciIo,
&Dev->SpecificConfig,
FALSE,
FieldOffset,
FieldSize,
Buffer
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10AllocateSharedPages (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINTN Pages,
IN OUT VOID **HostAddress
)
{
VIRTIO_1_0_DEV *Dev;
EFI_STATUS Status;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Dev->PciIo->AllocateBuffer (
Dev->PciIo,
AllocateAnyPages,
EfiBootServicesData,
Pages,
HostAddress,
EFI_PCI_ATTRIBUTE_MEMORY_CACHED
);
return Status;
}
STATIC
VOID
EFIAPI
Virtio10FreeSharedPages (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN UINTN Pages,
IN VOID *HostAddress
)
{
VIRTIO_1_0_DEV *Dev;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Dev->PciIo->FreeBuffer (
Dev->PciIo,
Pages,
HostAddress
);
}
STATIC
EFI_STATUS
EFIAPI
Virtio10MapSharedBuffer (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN VIRTIO_MAP_OPERATION Operation,
IN VOID *HostAddress,
IN OUT UINTN *NumberOfBytes,
OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
OUT VOID **Mapping
)
{
EFI_STATUS Status;
VIRTIO_1_0_DEV *Dev;
EFI_PCI_IO_PROTOCOL_OPERATION PciIoOperation;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
//
// Map VIRTIO_MAP_OPERATION to EFI_PCI_IO_PROTOCOL_OPERATION
//
switch (Operation) {
case VirtioOperationBusMasterRead:
PciIoOperation = EfiPciIoOperationBusMasterRead;
break;
case VirtioOperationBusMasterWrite:
PciIoOperation = EfiPciIoOperationBusMasterWrite;
break;
case VirtioOperationBusMasterCommonBuffer:
PciIoOperation = EfiPciIoOperationBusMasterCommonBuffer;
break;
default:
return EFI_INVALID_PARAMETER;
}
Status = Dev->PciIo->Map (
Dev->PciIo,
PciIoOperation,
HostAddress,
NumberOfBytes,
DeviceAddress,
Mapping
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10UnmapSharedBuffer (
IN VIRTIO_DEVICE_PROTOCOL *This,
IN VOID *Mapping
)
{
EFI_STATUS Status;
VIRTIO_1_0_DEV *Dev;
Dev = VIRTIO_1_0_FROM_VIRTIO_DEVICE (This);
Status = Dev->PciIo->Unmap (
Dev->PciIo,
Mapping
);
return Status;
}
STATIC CONST VIRTIO_DEVICE_PROTOCOL mVirtIoTemplate = {
VIRTIO_SPEC_REVISION (1, 0, 0),
0, // SubSystemDeviceId, filled in dynamically
Virtio10GetDeviceFeatures,
Virtio10SetGuestFeatures,
Virtio10SetQueueAddress,
Virtio10SetQueueSel,
Virtio10SetQueueNotify,
Virtio10SetQueueAlign,
Virtio10SetPageSize,
Virtio10GetQueueNumMax,
Virtio10SetQueueNum,
Virtio10GetDeviceStatus,
Virtio10SetDeviceStatus,
Virtio10WriteDevice,
Virtio10ReadDevice,
Virtio10AllocateSharedPages,
Virtio10FreeSharedPages,
Virtio10MapSharedBuffer,
Virtio10UnmapSharedBuffer
};
//
// EFI_DRIVER_BINDING_PROTOCOL member functions
//
STATIC
EFI_STATUS
EFIAPI
Virtio10BindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE DeviceHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
EFI_STATUS Status;
EFI_PCI_IO_PROTOCOL *PciIo;
PCI_TYPE00 Pci;
Status = gBS->OpenProtocol (
DeviceHandle,
&gEfiPciIoProtocolGuid,
(VOID **)&PciIo,
This->DriverBindingHandle,
DeviceHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
0,
sizeof Pci / sizeof (UINT32),
&Pci
);
if (EFI_ERROR (Status)) {
goto CloseProtocol;
}
Status = EFI_UNSUPPORTED;
//
// Recognize non-transitional modern devices. Also, we'll have to parse the
// PCI capability list, so make sure the CapabilityPtr field will be valid.
//
if ((Pci.Hdr.VendorId == VIRTIO_VENDOR_ID) &&
(Pci.Hdr.DeviceId >= 0x1040) &&
(Pci.Hdr.DeviceId <= 0x107F) &&
(Pci.Hdr.RevisionID >= 0x01) &&
(Pci.Device.SubsystemID >= 0x40) &&
((Pci.Hdr.Status & EFI_PCI_STATUS_CAPABILITY) != 0))
{
//
// The virtio-vga device is special. It can be driven both as a VGA device
// with a linear framebuffer, and through its underlying, modern,
// virtio-gpu-pci device, which has no linear framebuffer itself. For
// compatibility with guest OSes that insist on inheriting a linear
// framebuffer from the firmware, we should leave virtio-vga to
// QemuVideoDxe, and support only virtio-gpu-pci here.
//
// Both virtio-vga and virtio-gpu-pci have DeviceId 0x1050, but only the
// former has device class PCI_CLASS_DISPLAY_VGA.
//
if ((Pci.Hdr.DeviceId != 0x1050) || !IS_PCI_VGA (&Pci)) {
Status = EFI_SUCCESS;
}
}
CloseProtocol:
gBS->CloseProtocol (
DeviceHandle,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
DeviceHandle
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10BindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE DeviceHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
VIRTIO_1_0_DEV *Device;
EFI_STATUS Status;
PCI_TYPE00 Pci;
UINT64 SetAttributes;
Device = AllocateZeroPool (sizeof *Device);
if (Device == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Device->Signature = VIRTIO_1_0_SIGNATURE;
CopyMem (&Device->VirtIo, &mVirtIoTemplate, sizeof mVirtIoTemplate);
Status = gBS->OpenProtocol (
DeviceHandle,
&gEfiPciIoProtocolGuid,
(VOID **)&Device->PciIo,
This->DriverBindingHandle,
DeviceHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
goto FreeDevice;
}
Status = Device->PciIo->Pci.Read (
Device->PciIo,
EfiPciIoWidthUint32,
0,
sizeof Pci / sizeof (UINT32),
&Pci
);
if (EFI_ERROR (Status)) {
goto ClosePciIo;
}
Device->VirtIo.SubSystemDeviceId = Pci.Hdr.DeviceId - 0x1040;
Status = ParseCapabilities (Device);
if (EFI_ERROR (Status)) {
goto ClosePciIo;
}
Status = Device->PciIo->Attributes (
Device->PciIo,
EfiPciIoAttributeOperationGet,
0,
&Device->OriginalPciAttributes
);
if (EFI_ERROR (Status)) {
goto ClosePciIo;
}
SetAttributes = (EFI_PCI_IO_ATTRIBUTE_BUS_MASTER |
EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE);
UpdateAttributes (&Device->CommonConfig, &SetAttributes);
UpdateAttributes (&Device->NotifyConfig, &SetAttributes);
UpdateAttributes (&Device->SpecificConfig, &SetAttributes);
Status = Device->PciIo->Attributes (
Device->PciIo,
EfiPciIoAttributeOperationEnable,
SetAttributes,
NULL
);
if (EFI_ERROR (Status)) {
goto ClosePciIo;
}
Status = gBS->InstallProtocolInterface (
&DeviceHandle,
&gVirtioDeviceProtocolGuid,
EFI_NATIVE_INTERFACE,
&Device->VirtIo
);
if (EFI_ERROR (Status)) {
goto RestorePciAttributes;
}
return EFI_SUCCESS;
RestorePciAttributes:
Device->PciIo->Attributes (
Device->PciIo,
EfiPciIoAttributeOperationSet,
Device->OriginalPciAttributes,
NULL
);
ClosePciIo:
gBS->CloseProtocol (
DeviceHandle,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
DeviceHandle
);
FreeDevice:
FreePool (Device);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
Virtio10BindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE DeviceHandle,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
{
EFI_STATUS Status;
VIRTIO_DEVICE_PROTOCOL *VirtIo;
VIRTIO_1_0_DEV *Device;
Status = gBS->OpenProtocol (
DeviceHandle,
&gVirtioDeviceProtocolGuid,
(VOID **)&VirtIo,
This->DriverBindingHandle,
DeviceHandle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return Status;
}
Device = VIRTIO_1_0_FROM_VIRTIO_DEVICE (VirtIo);
Status = gBS->UninstallProtocolInterface (
DeviceHandle,
&gVirtioDeviceProtocolGuid,
&Device->VirtIo
);
if (EFI_ERROR (Status)) {
return Status;
}
Device->PciIo->Attributes (
Device->PciIo,
EfiPciIoAttributeOperationSet,
Device->OriginalPciAttributes,
NULL
);
gBS->CloseProtocol (
DeviceHandle,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
DeviceHandle
);
FreePool (Device);
return EFI_SUCCESS;
}
STATIC EFI_DRIVER_BINDING_PROTOCOL mDriverBinding = {
&Virtio10BindingSupported,
&Virtio10BindingStart,
&Virtio10BindingStop,
0x10, // Version
NULL, // ImageHandle, to be overwritten
NULL // DriverBindingHandle, to be overwritten
};
//
// EFI_COMPONENT_NAME_PROTOCOL and EFI_COMPONENT_NAME2_PROTOCOL
// implementations
//
STATIC
EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {
{ "eng;en", L"Virtio 1.0 PCI Driver" },
{ NULL, NULL }
};
STATIC
EFI_COMPONENT_NAME_PROTOCOL mComponentName;
STATIC
EFI_STATUS
EFIAPI
Virtio10GetDriverName (
IN EFI_COMPONENT_NAME_PROTOCOL *This,
IN CHAR8 *Language,
OUT CHAR16 **DriverName
)
{
return LookupUnicodeString2 (
Language,
This->SupportedLanguages,
mDriverNameTable,
DriverName,
(BOOLEAN)(This == &mComponentName) // Iso639Language
);
}
STATIC
EFI_STATUS
EFIAPI
Virtio10GetDeviceName (
IN EFI_COMPONENT_NAME_PROTOCOL *This,
IN EFI_HANDLE DeviceHandle,
IN EFI_HANDLE ChildHandle,
IN CHAR8 *Language,
OUT CHAR16 **ControllerName
)
{
return EFI_UNSUPPORTED;
}
STATIC
EFI_COMPONENT_NAME_PROTOCOL mComponentName = {
&Virtio10GetDriverName,
&Virtio10GetDeviceName,
"eng"
};
STATIC
EFI_COMPONENT_NAME2_PROTOCOL mComponentName2 = {
(EFI_COMPONENT_NAME2_GET_DRIVER_NAME)&Virtio10GetDriverName,
(EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)&Virtio10GetDeviceName,
"en"
};
//
// Entry point of this driver
//
EFI_STATUS
EFIAPI
Virtio10EntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
return EfiLibInstallDriverBindingComponentName2 (
ImageHandle,
SystemTable,
&mDriverBinding,
ImageHandle,
&mComponentName,
&mComponentName2
);
}
|