This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
qemu-vgt.patch
1667 lines (1624 loc) · 47.7 KB
/
qemu-vgt.patch
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
diff --git a/cpu-all.h b/cpu-all.h
index c9c51b8..cc70228 100644
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -476,6 +476,10 @@ hwaddr cpu_get_phys_page_debug(CPUArchState *env, target_ulong addr);
extern int phys_ram_fd;
extern ram_addr_t ram_size;
+extern int vgt_low_gm_sz;
+extern int vgt_high_gm_sz;
+extern int vgt_fence_sz;
+extern const char *vgt_monitor_config_file;
/* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
#define RAM_PREALLOC_MASK (1 << 0)
diff --git a/hw/Makefile.objs b/hw/Makefile.objs
index d581d8d..e2506ca 100644
--- a/hw/Makefile.objs
+++ b/hw/Makefile.objs
@@ -133,6 +133,7 @@ common-obj-$(CONFIG_VGA_ISA_MM) += vga-isa-mm.o
common-obj-$(CONFIG_VMWARE_VGA) += vmware_vga.o
common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
common-obj-$(CONFIG_VGA_CIRRUS) += cirrus_vga.o
+common-obj-y += vga-xengt.o
common-obj-$(CONFIG_RC4030) += rc4030.o
common-obj-$(CONFIG_DP8393X) += dp8393x.o
diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs
index 0d3f6a8..d890e7d 100644
--- a/hw/i386/Makefile.objs
+++ b/hw/i386/Makefile.objs
@@ -9,7 +9,7 @@ obj-y += pc_sysfw.o
obj-y += lpc_ich9.o q35.o pc_q35.o
obj-$(CONFIG_XEN) += xen_platform.o xen_apic.o
obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
-obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o
+obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o xen_pt_igd.o
obj-y += kvm/
obj-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
diff --git a/hw/pc_piix.c b/hw/pc_piix.c
index aa3e7f4..904c82c 100644
--- a/hw/pc_piix.c
+++ b/hw/pc_piix.c
@@ -46,8 +46,11 @@
#include "cpu.h"
#ifdef CONFIG_XEN
# include <xen/hvm/hvm_info_table.h>
+# include "vga-xengt.h"
+#endif
+#ifdef CONFIG_XEN_PCI_PASSTHROUGH
+#include "xen_pt.h"
#endif
-
#define MAX_IDE_BUS 2
static const int ide_iobase[MAX_IDE_BUS] = { 0x1f0, 0x170 };
@@ -92,9 +95,9 @@ static void pc_init1(MemoryRegion *system_memory,
kvmclock_create();
}
- if (ram_size >= 0xe0000000 ) {
- above_4g_mem_size = ram_size - 0xe0000000;
- below_4g_mem_size = 0xe0000000;
+ if (ram_size >= 0xc0000000) {
+ above_4g_mem_size = ram_size - 0xc0000000;
+ below_4g_mem_size = 0xc0000000;
} else {
above_4g_mem_size = 0;
below_4g_mem_size = ram_size;
@@ -162,7 +165,32 @@ static void pc_init1(MemoryRegion *system_memory,
pc_register_ferr_irq(gsi[13]);
- pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
+ /*
+ * Initialize XenGT hooks before normal VGA init. The
+ * ideal case is to have IGD presented as the primary
+ * graphics card in 00:02.0, and then have other emulated
+ * PCI VGA card all disabled. We still rely on Qemu to
+ * emulate legacy ISA ports, so requires the ISA vga logic.
+ */
+ if (xengt_vga_enabled && pci_enabled) {
+ xengt_vga_init(pci_bus);
+ isa_create_simple(isa_bus, "isa-vga");
+ } else if (gfx_passthru) {
+ /*
+ * Do not create emulated VGA card here.
+ * and wait QEMU create qdev: xen-pci-passthrough,
+ * which is a pass-throughed IGD device.
+ *
+ * We need a dummy graphic console here.
+ * otherwise QEMU will not have a vaild console,
+ * and we have to passthrough mouse and keyboard
+ * to control VM.
+ */
+ graphic_console_init(NULL, NULL, NULL, NULL, NULL);
+ } else {
+ pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
+ }
+
if (xen_enabled()) {
pci_create_simple(pci_bus, -1, "xen-platform");
}
diff --git a/hw/pci.c b/hw/pci.c
index 97a0cd7..3c9e1a6 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -34,6 +34,7 @@
#include "msi.h"
#include "msix.h"
#include "exec-memory.h"
+#include "xen_pt.h"
//#define DEBUG_PCI
#ifdef DEBUG_PCI
@@ -768,6 +769,11 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
if (devfn < 0) {
for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
devfn += PCI_FUNC_MAX) {
+
+ /* If vGT/XenGT is in use, reserve 00:02.* for the IGD */
+ if ((xengt_vga_enabled || gfx_passthru) && devfn == 0x10)
+ continue;
+
if (!bus->devices[devfn])
goto found;
}
@@ -799,6 +805,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
pci_dev->irq_state = 0;
pci_config_alloc(pci_dev);
+ printf("set vendor id(%x) for devfn(%x)\n", pc->vendor_id, devfn);
pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
pci_config_set_device_id(pci_dev->config, pc->device_id);
pci_config_set_revision(pci_dev->config, pc->revision);
diff --git a/hw/pci.h b/hw/pci.h
index 4da0c2a..d32a58f 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -102,6 +102,8 @@ typedef struct PCIIORegion {
#include "pci_regs.h"
+#define PCI_INTEL_OPREGION 0xfc
+
/* PCI HEADER_TYPE */
#define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
diff --git a/hw/piix_pci.c b/hw/piix_pci.c
index ba1b3de..27f7c56 100644
--- a/hw/piix_pci.c
+++ b/hw/piix_pci.c
@@ -31,6 +31,13 @@
#include "range.h"
#include "xen.h"
#include "pam.h"
+#include "sysemu.h"
+#ifdef CONFIG_XEN
+#include "vga-xengt.h"
+#endif
+#ifdef CONFIG_XEN_PCI_PASSTHROUGH
+#include "xen_pt.h"
+#endif
/*
* I440FX chipset data sheet.
@@ -136,6 +143,8 @@ static void i440fx_write_config(PCIDevice *dev,
range_covers_byte(address, len, I440FX_SMRAM)) {
i440fx_update_memory_mappings(d);
}
+
+ vgt_bridge_pci_write(dev, address, val, len);
}
static int i440fx_load_old(QEMUFile* f, void *opaque, int version_id)
@@ -204,6 +213,11 @@ static int i440fx_initfn(PCIDevice *dev)
d->dev.config[I440FX_SMRAM] = 0x02;
cpu_smm_register(&i440fx_set_smm, d);
+
+ if (xengt_vga_enabled) {
+ vgt_bridge_pci_conf_init(dev);
+ }
+
return 0;
}
@@ -296,6 +310,14 @@ static PCIBus *i440fx_common_init(const char *device_name,
i440fx_update_memory_mappings(f);
+#if CONFIG_XEN_PCI_PASSTHROUGH
+ if (gfx_passthru) {
+ d->config_read = igd_pci_read;
+ d->config_write = igd_pci_write;
+ intel_pch_init(b);
+ }
+#endif
+
return b;
}
@@ -547,6 +569,10 @@ static void i440fx_class_init(ObjectClass *klass, void *data)
dc->desc = "Host bridge";
dc->no_user = 1;
dc->vmsd = &vmstate_i440fx;
+ if (xengt_vga_enabled) {
+ k->config_read = vgt_bridge_pci_read;
+ }
+
}
static const TypeInfo i440fx_info = {
diff --git a/hw/vga-xengt.c b/hw/vga-xengt.c
new file mode 100644
index 0000000..e5bafa9
--- /dev/null
+++ b/hw/vga-xengt.c
@@ -0,0 +1,638 @@
+/*
+ * QEMU vGT/XenGT Legacy VGA support
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ * Copyright (c) Citrix Systems, Inc
+ * Copyright (c) Intel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "hw.h"
+#include "console.h"
+#include "pc.h"
+#include "pci.h"
+#include "pci_host.h"
+#include "pci_bridge.h"
+#include "pci_internals.h"
+#include "vga_int.h"
+#include "pixel_ops.h"
+#include "qemu-timer.h"
+#include "loader.h"
+#include "xen_pt.h"
+#include "vga-xengt.h"
+#include "qemu-log.h"
+
+typedef struct vgt_vga_state {
+ PCIDevice dev;
+ struct VGACommonState state;
+ int num_displays;
+ XenHostPCIDevice host_dev;
+ bool instance_created;
+} vgt_vga_state_t;
+
+#define EDID_SIZE 128
+#define MAX_INPUT_NUM 3
+#define MAX_FILE_NAME_LENGTH 128
+
+/* port definition must align with gvt-g driver */
+enum vgt_port {
+ PORT_A = 0,
+ PORT_B,
+ PORT_C,
+ PORT_D,
+ PORT_E,
+ MAX_PORTS
+};
+
+typedef struct vgt_monitor_info {
+ unsigned char port_type:4;
+ unsigned char port_is_dp:4; /* 0 = HDMI PORT, 1 = DP port, only valid for PORT_B/C/D */
+ unsigned char port_override;
+ unsigned char edid[EDID_SIZE];
+}vgt_monitor_info_t;
+
+/* These are the default values */
+int vgt_low_gm_sz = 64; /* in MB */
+int vgt_high_gm_sz = 448; /* in MB */
+int vgt_fence_sz = 4;
+int vgt_primary = 1; /* -1 means "not specified */
+const char *vgt_monitor_config_file = NULL;
+
+
+static inline unsigned int port_info_to_type(unsigned char port_is_dp, int port)
+{
+ /* port type definition must align with gvt-g driver */
+ enum vgt_port_type {
+ VGT_CRT = 0,
+ VGT_DP_A,
+ VGT_DP_B,
+ VGT_DP_C,
+ VGT_DP_D,
+ VGT_HDMI_B,
+ VGT_HDMI_C,
+ VGT_HDMI_D,
+ VGT_PORT_TYPE_MAX
+ } ret;
+
+ switch (port) {
+ case PORT_A:
+ ret = VGT_DP_A;
+ break;
+ case PORT_B:
+ ret = (port_is_dp) ? VGT_DP_B : VGT_HDMI_B;
+ break;
+ case PORT_C:
+ ret = (port_is_dp) ? VGT_DP_C : VGT_HDMI_C;
+ break;
+ case PORT_D:
+ ret = (port_is_dp) ? VGT_DP_D : VGT_HDMI_D;
+ break;
+ case PORT_E:
+ ret = VGT_CRT;
+ break;
+ default:
+ ret = VGT_PORT_TYPE_MAX;
+ break;
+ }
+
+ return ret;
+}
+
+static bool validate_monitor_configs(vgt_monitor_info_t *config)
+{
+ if (config->port_type >= MAX_PORTS) {
+ qemu_log("vGT: %s failed because the invalid port_type input: %d!\n",
+ __func__, config->port_type);
+ return false;
+ }
+ if (config->port_override >= MAX_PORTS) {
+ qemu_log("vGT: %s failed due to the invalid port_override input: %d!\n",
+ __func__, config->port_override);
+ return false;
+ }
+ if (config->edid[126] != 0) {
+ qemu_log("vGT: %s failed because there is extended block in EDID! "
+ "(EDID[126] is not zero)\n", __func__);
+ return false;
+ }
+
+ return true;
+}
+
+static void config_hvm_monitors(vgt_monitor_info_t *config)
+{
+ const char *path_prefix = "/sys/kernel/vgt/vm";
+ FILE *fp;
+ char file_name[MAX_FILE_NAME_LENGTH];
+ int ret;
+
+ // override
+ snprintf(file_name, MAX_FILE_NAME_LENGTH, "%s%d/PORT_%c/port_override",
+ path_prefix, xen_domid, 'A' + config->port_type);
+ if ((fp = fopen(file_name, "w")) == NULL) {
+ qemu_log("vGT: %s failed to open file %s! errno = %d\n",
+ __func__, file_name, errno);
+ return;
+ }
+ fprintf(fp, "PORT_%c", 'A' + config->port_override);
+ if (fclose(fp) != 0) {
+ qemu_log("vGT: %s failed to close file: errno = %d\n", __func__, errno);
+ }
+
+ // type
+ snprintf(file_name, MAX_FILE_NAME_LENGTH, "%s%d/PORT_%c/type",
+ path_prefix, xen_domid, 'A' + config->port_type);
+ if ((fp = fopen(file_name, "w")) == NULL) {
+ qemu_log("vGT: %s failed to open file %s! errno = %d\n",
+ __func__, file_name, errno);
+ return;
+ }
+ fprintf(fp, "%d", port_info_to_type(config->port_is_dp, config->port_type));
+ if (fclose(fp) != 0) {
+ qemu_log("vGT: %s failed to close file: errno = %d\n", __func__, errno);
+ }
+
+ // edid
+ snprintf(file_name, MAX_FILE_NAME_LENGTH, "%s%d/PORT_%c/edid",
+ path_prefix, xen_domid, 'A' + config->port_type);
+ if ((fp = fopen(file_name, "w")) == NULL) {
+ qemu_log("vGT: %s failed to open file %s! errno = %d\n",
+ __func__, file_name, errno);
+ return;
+ }
+ ret = fwrite(config->edid, 1, EDID_SIZE, fp);
+ if (ret != EDID_SIZE) {
+ qemu_log("vGT: %s failed to write EDID with returned size %d: "
+ "errno = %d\n", __func__, ret, errno);
+ }
+ if (fclose(fp) != 0) {
+ qemu_log("vGT: %s failed to close file: errno = %d\n", __func__, errno);
+ }
+
+ // flush result to port structure
+ snprintf(file_name, MAX_FILE_NAME_LENGTH, "%s%d/PORT_%c/connection",
+ path_prefix, xen_domid, 'A' + config->port_type);
+ if ((fp = fopen(file_name, "w")) == NULL) {
+ qemu_log("vGT: %s failed to open file %s! errno = %d\n",
+ __func__, file_name, errno);
+ return;
+ }
+ fprintf(fp, "flush");
+ if (fclose(fp) != 0) {
+ qemu_log("vGT: %s failed to close file: errno = %d\n", __func__, errno);
+ }
+}
+
+#define CTOI(chr) \
+ (chr >= '0' && chr <= '9' ? chr - '0' : \
+ (chr >= 'a' && chr <= 'f' ? chr - 'a' + 10 :\
+ (chr >= 'A' && chr <= 'F' ? chr - 'A' + 10 : -1)))
+
+static int get_byte_from_txt_file(FILE *file, const char *file_name)
+{
+ int i;
+ int val[2];
+
+ for (i = 0; i < 2; ++ i) {
+ do {
+ unsigned char buf;
+ if (fread(&buf, 1, 1, file) != 1) {
+ qemu_log("vGT: %s failed to get byte from text file %s with errno: %d!\n",
+ __func__, file_name, errno);
+ return -1;
+ }
+
+ if (buf == '#') {
+ // ignore comments
+ int ret;
+ while (((ret = fread(&buf, 1, 1, file)) == 1) && (buf != '\n')) ;
+ if (ret != 1) {
+ qemu_log("vGT: %s failed to proceed after comment string "
+ "from text file %s with errno: %d!\n",
+ __func__, file_name, errno);
+ return -1;
+ }
+ }
+
+ val[i] = CTOI(buf);
+ } while (val[i] == -1);
+ }
+
+ return ((val[0] << 4) | val[1]);
+}
+
+static int get_config_header(unsigned char *buf, FILE *file, const char *file_name)
+{
+ int ret;
+ unsigned char chr;
+
+ if (fread(&chr, 1, 1, file) != 1) {
+ qemu_log("vGT: %s failed to get byte from text file %s with errno: %d!\n",
+ __func__, file_name, errno);
+ return -1;
+ }
+
+ if (chr == '#') {
+ // it is text format input.
+ while (((ret = fread(&chr, 1, 1, file)) == 1) && (chr != '\n')) ;
+ if (ret != 1) {
+ qemu_log("vGT: %s failed to proceed after comment string "
+ "from file %s with errno: %d!\n",
+ __func__, file_name, errno);
+ return -1;
+ }
+ ret = get_byte_from_txt_file(file, file_name);
+ buf[0] = 1;
+ buf[1] = (ret & 0xf);
+ } else {
+ if ((ret = fread(&buf[0], 1, 2, file)) != 2) {
+ qemu_log("vGT: %s failed to read file %s! "
+ "Expect to read %d bytes but only got %d bytes! errno: %d\n",
+ __func__, file_name, 2, ret, errno);
+ return -1;
+ }
+
+ if (buf[0] != 0) {
+ // it is text format input.
+ buf[1] -= '0';
+ }
+ }
+
+ return 0;
+}
+
+static void config_vgt_guest_monitors(void)
+{
+ FILE *monitor_config_f;
+ unsigned char buf[4];
+ vgt_monitor_info_t monitor_configs[MAX_INPUT_NUM];
+ bool text_mode;
+ int input_items;
+ int ret, i;
+
+ if (!vgt_monitor_config_file) {
+ return;
+ }
+
+ if ((monitor_config_f = fopen(vgt_monitor_config_file, "r")) == NULL) {
+ qemu_log("vGT: %s failed to open file %s! errno = %d\n",
+ __func__, vgt_monitor_config_file, errno);
+ return;
+ }
+
+ if (get_config_header(buf, monitor_config_f, vgt_monitor_config_file) != 0) {
+ goto finish_config;
+ }
+
+ text_mode = !!buf[0];
+ input_items = buf[1];
+
+ if (input_items <= 0 || input_items > MAX_INPUT_NUM) {
+ qemu_log("vGT: %s, Out of range input of the number of items! "
+ "Should be [1 - 3] but input is %d\n", __func__, input_items);
+ goto finish_config;
+ }
+
+ if (text_mode) {
+ unsigned int total = sizeof(vgt_monitor_info_t) * input_items;
+ unsigned char *p = (unsigned char *)monitor_configs;
+ for (i = 0; i < total; ++i, ++p) {
+ unsigned int val = get_byte_from_txt_file(monitor_config_f,
+ vgt_monitor_config_file);
+ if (val == -1) {
+ break;
+ } else {
+ *p = val;
+ }
+ }
+ if (i < total) {
+ goto finish_config;
+ }
+ } else {
+ unsigned int total = sizeof(vgt_monitor_info_t) * input_items;
+ ret = fread(monitor_configs, sizeof(vgt_monitor_info_t), input_items,
+ monitor_config_f);
+ if (ret != total) {
+ qemu_log("vGT: %s failed to read file %s! "
+ "Expect to read %d bytes but only got %d bytes! errno: %d\n",
+ __func__, vgt_monitor_config_file, total, ret, errno);
+ goto finish_config;
+ }
+ }
+
+ for (i = 0; i < input_items; ++ i) {
+ if (validate_monitor_configs(&monitor_configs[i]) == false) {
+ qemu_log("vGT: %s the monitor config[%d] input from %s is not valid!\n",
+ __func__, i, vgt_monitor_config_file);
+ goto finish_config;
+ }
+ }
+ for (i = 0; i < input_items; ++ i) {
+ config_hvm_monitors(&monitor_configs[i]);
+ }
+
+finish_config:
+ if (fclose(monitor_config_f) != 0) {
+ qemu_log("vGT: %s failed to close file %s: errno = %d\n", __func__,
+ vgt_monitor_config_file, errno);
+ }
+ return;
+}
+
+/*
+ * Inform vGT driver to create a vGT instance
+ */
+static void create_vgt_instance(void)
+{
+ /* FIXME: this should be substituded as a environment variable */
+ const char *path = "/sys/kernel/vgt/control/create_vgt_instance";
+ FILE *vgt_file;
+ int err = 0;
+
+ qemu_log("vGT: %s: domid=%d, low_gm_sz=%dMB, high_gm_sz=%dMB, "
+ "fence_sz=%d, vgt_primary=%d\n", __func__, xen_domid,
+ vgt_low_gm_sz, vgt_high_gm_sz, vgt_fence_sz, vgt_primary);
+ if (vgt_low_gm_sz <= 0 || vgt_high_gm_sz <=0 ||
+ vgt_primary < -1 || vgt_primary > 1 ||
+ vgt_fence_sz <=0) {
+ qemu_log("vGT: %s failed: invalid parameters!\n", __func__);
+ abort();
+ }
+
+ if ((vgt_file = fopen(path, "w")) == NULL) {
+ err = errno;
+ qemu_log("vGT: open %s failed\n", path);
+ }
+ /* The format of the string is:
+ * domid,aperture_size,gm_size,fence_size. This means we want the vgt
+ * driver to create a vgt instanc for Domain domid with the required
+ * parameters. NOTE: aperture_size and gm_size are in MB.
+ */
+ if (!err && fprintf(vgt_file, "%d,%u,%u,%u,%d\n", xen_domid,
+ vgt_low_gm_sz, vgt_high_gm_sz, vgt_fence_sz, vgt_primary) < 0) {
+ err = errno;
+ }
+
+ if (!err && fclose(vgt_file) != 0) {
+ err = errno;
+ }
+
+ if (err) {
+ qemu_log("vGT: %s failed: errno=%d\n", __func__, err);
+ exit(-1);
+ }
+
+ config_vgt_guest_monitors();
+}
+
+/*
+ * Inform vGT driver to close a vGT instance
+ */
+static void destroy_vgt_instance(void)
+{
+ const char *path = "/sys/kernel/vgt/control/create_vgt_instance";
+ FILE *vgt_file;
+ int err = 0;
+
+ qemu_log("vGT: %s: domid=%d\n", __func__, xen_domid);
+
+ if ((vgt_file = fopen(path, "w")) == NULL) {
+ fprintf(stdout, "vGT: open %s failed\n", path);
+ err = errno;
+ }
+
+ /* -domid means we want the vgt driver to free the vgt instance
+ * of Domain domid.
+ * */
+ if (!err && fprintf(vgt_file, "%d\n", -xen_domid) < 0) {
+ err = errno;
+ }
+
+ if (!err && fclose(vgt_file) != 0) {
+ err = errno;
+ }
+
+ if (err) {
+ qemu_log("vGT: %s: failed: errno=%d\n", __func__, err);
+ exit(-1);
+ }
+}
+
+static int pch_map_irq(PCIDevice *pci_dev, int irq_num)
+{
+ return irq_num;
+}
+
+void vgt_bridge_pci_write(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
+{
+#if 0
+ vgt_vga_state_t *o = DO_UPCAST(vgt_vga_state_t, dev, dev);
+#endif
+ assert(dev->devfn == 0x00);
+
+ XEN_PT_LOG(dev, "vGT Config Write: addr=%x len=%x val=%x\n", addr, len, val);
+
+ switch (addr) {
+#if 0
+ case 0x58: // PAVPC Offset
+ xen_host_pci_set_block(o->host_dev, addr, val, len);
+ break;
+#endif
+ default:
+ pci_default_write_config(dev, addr, val, len);
+ }
+}
+
+static void vgt_bridge_pci_conf_init_from_host(PCIDevice *dev,
+ uint32_t addr, int len)
+{
+ XenHostPCIDevice host_dev;
+
+ if (len > 4) {
+ XEN_PT_LOG(dev, "WARNIGN: length %x too large for config addr %x, ignore init\n",
+ len, addr);
+ return;
+ }
+
+ /* FIXME: need a better scheme to grab the root complex. This
+ * only for a single VM scenario.
+ */
+ if ( xen_host_pci_device_get(&host_dev, 0, 0, 0, 0) < 0) {
+ fprintf(stderr, " Error, failed to get host PCI device\n");
+ }
+
+ xen_host_pci_get_block(&host_dev, addr, dev->config + addr, len);
+ xen_host_pci_device_put(&host_dev);
+}
+
+static void vgt_host_bridge_cap_init(PCIDevice *dev)
+{
+ assert(dev->devfn == 0x00);
+ uint8_t cap_ptr = 0;
+ XenHostPCIDevice host_dev;
+
+ xen_host_pci_device_get(&host_dev, 0, 0, 0, 0);
+ xen_host_pci_get_byte(&host_dev, 0x34, &cap_ptr);
+
+ while (cap_ptr !=0) {
+ vgt_bridge_pci_conf_init_from_host(dev, cap_ptr, 4); /* capability */
+ vgt_bridge_pci_conf_init_from_host(dev, cap_ptr + 4, 4); /* capability */
+ vgt_bridge_pci_conf_init_from_host(dev, cap_ptr + 8, 4); /* capability */
+ vgt_bridge_pci_conf_init_from_host(dev, cap_ptr + 12, 4); /* capability */
+ //XEN_PT_LOG(pci_dev, "Add vgt host bridge capability: offset=0x%x, cap=0x%x\n", cap_ptr,
+ // pt_pci_host_read(0, PCI_SLOT(pci_dev->devfn), 0, cap_ptr, 1) & 0xFF );
+ xen_host_pci_get_byte(&host_dev, cap_ptr + 1, &cap_ptr);
+ }
+
+ xen_host_pci_device_put(&host_dev);
+}
+
+
+void vgt_bridge_pci_conf_init(PCIDevice *pci_dev)
+{
+ printf("vgt_bridge_pci_conf_init\n");
+ printf("vendor id: %x\n", *(uint16_t *)((char *)pci_dev->config + 0x00));
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x00, 2); /* vendor id */
+ printf("vendor id: %x\n", *(uint16_t *)((char *)pci_dev->config + 0x00));
+ printf("device id: %x\n", *(uint16_t *)((char *)pci_dev->config + 0x02));
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x02, 2); /* device id */
+ printf("device id: %x\n", *(uint16_t *)((char *)pci_dev->config + 0x02));
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x06, 2); /* status */
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x08, 2); /* revision id */
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x34, 1); /* capability */
+ vgt_host_bridge_cap_init(pci_dev);
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x50, 2); /* SNB: processor graphics control register */
+ vgt_bridge_pci_conf_init_from_host(pci_dev, 0x52, 2); /* processor graphics control register */
+}
+
+uint32_t vgt_bridge_pci_read(PCIDevice *pci_dev, uint32_t config_addr, int len)
+{
+ uint32_t val;
+
+ val = pci_default_read_config(pci_dev, config_addr, len);
+ XEN_PT_LOG(pci_dev, "addr=%x len=%x val=%x\n", config_addr, len, val);
+
+ return val;
+}
+
+static void vgt_reset(DeviceState *dev)
+{
+}
+
+static void vgt_cleanupfn(PCIDevice *dev)
+{
+ vgt_vga_state_t *d = DO_UPCAST(vgt_vga_state_t, dev, dev);
+
+ if (d->instance_created) {
+ destroy_vgt_instance();
+ }
+}
+
+static int vgt_initfn(PCIDevice *dev)
+{
+ vgt_vga_state_t *d = DO_UPCAST(vgt_vga_state_t, dev, dev);
+
+ printf("vgt_initfn\n");
+ d->instance_created = FALSE;
+
+ create_vgt_instance();
+ return 0;
+}
+
+DeviceState *xengt_vga_init(PCIBus *pci_bus)
+{
+ PCIDevice *dev;
+ XenHostPCIDevice host_dev;
+ PCIBridge *br;
+
+ if (xen_host_pci_device_get(&host_dev, 0, 0, 0x1f, 0) < 0) {
+ fprintf(stderr, " Error, failed to get host PCI device\n");
+ return NULL;
+ }
+
+ if (host_dev.vendor_id != 0x8086) {
+ xen_host_pci_device_put(&host_dev);
+ fprintf(stderr, " Error, vga-xengt is only supported on Intel GPUs\n");
+ return NULL;
+ }
+
+ xen_host_pci_device_put(&host_dev);
+
+ dev = pci_create_multifunction(pci_bus, PCI_DEVFN(0x1f, 0), true,
+ "xengt-isa");
+ if (!dev) {
+ fprintf(stderr, "Warning: vga-xengt not available\n");
+ return NULL;
+ }
+
+ qdev_init_nofail(&dev->qdev);
+
+ pci_config_set_vendor_id(dev->config, host_dev.vendor_id);
+ pci_config_set_device_id(dev->config, host_dev.device_id);
+ pci_config_set_revision(dev->config, host_dev.revision_id);
+ pci_config_set_class(dev->config, host_dev.class_code);
+ br = DO_UPCAST(PCIBridge, dev, dev);
+ pci_bridge_map_irq(br, "IGD Bridge",
+ pch_map_irq);
+
+ printf("Create xengt ISA bridge successfully\n");
+
+ dev = pci_create_multifunction(pci_bus, PCI_DEVFN(0x2, 0), true,
+ "xengt-vga");
+ if (!dev) {
+ fprintf(stderr, "Warning: vga-xengt not available\n");
+ return NULL;
+ }
+ qdev_init_nofail(&dev->qdev);
+
+ printf("Create xengt VGA successfully\n");
+ return &dev->qdev;
+}
+
+static void vgt_class_initfn(ObjectClass *klass, void *data)
+{
+ printf("vgt_class_initfn\n");
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ PCIDeviceClass *ic = PCI_DEVICE_CLASS(klass);
+ ic->init = vgt_initfn;
+ dc->reset = vgt_reset;
+ ic->exit = vgt_cleanupfn;
+ dc->vmsd = &vmstate_vga_common;
+}
+
+static TypeInfo vgt_info = {
+ .name = "xengt-vga",
+ .parent = TYPE_PCI_DEVICE,
+ .instance_size = sizeof(vgt_vga_state_t),
+ .class_init = vgt_class_initfn,
+};
+
+static TypeInfo isa_info = {
+ .name = "xengt-isa",
+ .parent = TYPE_PCI_DEVICE,
+ .instance_size = sizeof(vgt_vga_state_t),
+};
+
+static void vgt_register_types(void)
+{
+ type_register_static(&vgt_info);
+ type_register_static(&isa_info);
+}
+
+type_init(vgt_register_types)
diff --git a/hw/vga-xengt.h b/hw/vga-xengt.h
new file mode 100644
index 0000000..6215ede
--- /dev/null
+++ b/hw/vga-xengt.h
@@ -0,0 +1,8 @@
+#ifndef __XENGT_H__
+#define __XENGT_H__
+
+DeviceState *xengt_vga_init(PCIBus *pci_bus);
+void vgt_bridge_pci_conf_init(PCIDevice *dev);
+void vgt_bridge_pci_write(PCIDevice *dev, uint32_t addr, uint32_t val, int len);
+uint32_t vgt_bridge_pci_read(PCIDevice *pci_dev, uint32_t config_addr, int len);
+#endif
diff --git a/hw/vga.c b/hw/vga.c
index 2b0200a..202cd44 100644
--- a/hw/vga.c
+++ b/hw/vga.c
@@ -618,7 +618,14 @@ uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
} else if (s->vbe_index == VBE_DISPI_INDEX_VIDEO_MEMORY_64K) {
val = s->vram_size / (64 * 1024);
} else {
- val = 0;
+ switch (s->vbe_index) {
+ case VBE_DISPI_INDEX_LFB_ADDRESS_H:
+ val = VBE_DISPI_LFB_PHYSICAL_ADDRESS >> 16;
+ break;
+ default:
+ val = 0;
+ break;
+ }
}
#ifdef DEBUG_BOCHS_VBE
printf("VBE: read index=0x%x val=0x%x\n", s->vbe_index, val);
diff --git a/hw/vga_int.h b/hw/vga_int.h
index bcb738d..03d7741 100644
--- a/hw/vga_int.h
+++ b/hw/vga_int.h
@@ -45,6 +45,7 @@
#define VBE_DISPI_INDEX_Y_OFFSET 0x9
#define VBE_DISPI_INDEX_NB 0xa /* size of vbe_regs[] */
#define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa /* read-only, not in vbe_regs */
+#define VBE_DISPI_INDEX_LFB_ADDRESS_H 0xb
#define VBE_DISPI_ID0 0xB0C0
#define VBE_DISPI_ID1 0xB0C1
diff --git a/hw/xen-host-pci-device.c b/hw/xen-host-pci-device.c
index 743b37b..16ed13e 100644
--- a/hw/xen-host-pci-device.c
+++ b/hw/xen-host-pci-device.c
@@ -378,6 +378,16 @@ int xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
d->irq = v;
d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
+ rc = xen_host_pci_get_byte(d, 0x08, &d->revision_id);
+ if (rc) {
+ goto error;
+ }
+
+ rc = xen_host_pci_get_word(d, 0x0a, &d->class_code);
+ if (rc) {
+ goto error;
+ }
+
return 0;
error:
if (d->config_fd >= 0) {
diff --git a/hw/xen-host-pci-device.h b/hw/xen-host-pci-device.h
index 0079dac..ee25706 100644
--- a/hw/xen-host-pci-device.h
+++ b/hw/xen-host-pci-device.h
@@ -25,6 +25,8 @@ typedef struct XenHostPCIDevice {
uint16_t vendor_id;
uint16_t device_id;
+ uint8_t revision_id;
+ uint16_t class_code;
int irq;
XenHostPCIIORegion io_regions[PCI_NUM_REGIONS - 1];
diff --git a/hw/xen_pt.c b/hw/xen_pt.c
index 7a3846e..223af3c 100644
--- a/hw/xen_pt.c
+++ b/hw/xen_pt.c
@@ -450,6 +450,21 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s)
d->rom.size, d->rom.base_addr);
}
+ if (gfx_passthru) {
+ /*
+ * Do not use vendor and class from PCIDevice,
+ * since no one populates them at this time.
+ */
+ uint16_t vendor = pci_get_word(s->dev.config + PCI_VENDOR_ID);
+ uint16_t class = pci_get_word(s->dev.config + PCI_CLASS_DEVICE);
+
+ /* Legacy VGA passthrough. */
+ if (vendor == PCI_VENDOR_ID_INTEL && class == PCI_CLASS_DISPLAY_VGA) {
+ register_vga_regions(&s->dev);
+ setup_vga_pt(&s->dev);
+ }
+ }
+
return 0;
}
@@ -470,6 +485,14 @@ static void xen_pt_unregister_regions(XenPCIPassthroughState *s)
if (d->rom.base_addr && d->rom.size) {
memory_region_destroy(&s->rom);
}
+
+ if (gfx_passthru) {
+ uint16_t vendor = pci_get_word(s->dev.config + PCI_VENDOR_ID);
+ uint16_t class = pci_get_word(s->dev.config + PCI_CLASS_DEVICE);
+
+ if (vendor == PCI_VENDOR_ID_INTEL && class == PCI_CLASS_DISPLAY_VGA)
+ unregister_vga_regions(&s->dev);
+ }
}
/* region mapping */
diff --git a/hw/xen_pt.h b/hw/xen_pt.h
index f15e69a..aaa0d6b 100644
--- a/hw/xen_pt.h
+++ b/hw/xen_pt.h
@@ -298,5 +298,22 @@ static inline bool xen_pt_has_msix_mapping(XenPCIPassthroughState *s, int bar)
return s->msix && s->msix->bar_index == bar;
}
+extern int gfx_passthru;
+
+uint32_t igd_read_opregion(struct XenHostPCIDevice *dev);
+
+void igd_write_opregion(struct XenHostPCIDevice *dev, uint32_t val);
+
+uint32_t igd_pci_read(PCIDevice *pci_dev, uint32_t config_addr, int len);
+
+void igd_pci_write(PCIDevice *pci_dev, uint32_t config_addr, uint32_t val, int len);
+
+void intel_pch_init(PCIBus *bus);
+
+int setup_vga_pt(struct PCIDevice *dev);