-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
1017-Fix-IGD-passthrough-with-linux-stubdomain.patch
194 lines (184 loc) · 7.1 KB
/
1017-Fix-IGD-passthrough-with-linux-stubdomain.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
From e6482ffa566bae21c006c90d0879c64baa678579 Mon Sep 17 00:00:00 2001
From: Grzegorz Uriasz <[email protected]>
Date: Thu, 13 Feb 2020 02:30:36 +0000
Subject: [PATCH] Fix IGD passthrough with linux stubdomain
- Give permissions for VGA IOPORTS
- Give permission for opregions
- Map vbios to stubdom
---
tools/libs/light/libxl_pci.c | 152 +++++++++++++++++++++++++++++------
1 file changed, 126 insertions(+), 26 deletions(-)
diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c
index 0302b5990f06..35e3e1360423 100644
--- a/tools/libs/light/libxl_pci.c
+++ b/tools/libs/light/libxl_pci.c
@@ -2594,17 +2594,128 @@ void libxl__device_pci_destroy_all(libxl__egc *egc, uint32_t domid,
libxl_device_pci_list_free(pcis, num);
}
+static int libxl__grant_legacy_vga_permissions(libxl__gc *gc, const uint32_t domid) {
+ int ret, i;
+ uint64_t vga_iomem_start = 0xa0000 >> XC_PAGE_SHIFT;
+ uint64_t vga_iomem_npages = 0x20; // vga ram + vbios
+ uint64_t vga_vbios_start = 0xc0000 >> XC_PAGE_SHIFT;
+ uint64_t vga_vbios_npages = 0x20;
+ uint32_t stubdom_domid = libxl_get_stubdom_id(CTX, domid);
+ uint64_t vga_ioport_start[] = {0x3B0, 0x3C0};
+ uint64_t vga_ioport_size[] = {0xC, 0x20};
+
+ // VGA RAM
+ ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
+ vga_iomem_start, vga_iomem_npages, 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give stubdom%d access to iomem range "
+ "%"PRIx64"-%"PRIx64" for VGA passthru",
+ stubdom_domid,
+ vga_iomem_start, (vga_iomem_start + (vga_iomem_npages << XC_PAGE_SHIFT) - 1));
+ return ret;
+ }
+ ret = xc_domain_iomem_permission(CTX->xch, domid,
+ vga_iomem_start, vga_iomem_npages, 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give dom%d access to iomem range "
+ "%"PRIx64"-%"PRIx64" for VGA passthru",
+ domid, vga_iomem_start, (vga_iomem_start + (vga_iomem_npages << XC_PAGE_SHIFT) - 1));
+ return ret;
+ }
+
+ // VGA ROM
+ ret = xc_domain_memory_mapping(CTX->xch, stubdom_domid, vga_vbios_start, vga_vbios_start, vga_vbios_npages, DPCI_ADD_MAPPING);
+ if (ret < 0) {
+ LOGED(ERROR, domid, "failed to map VBIOS to stubdom%d", stubdom_domid);
+ return ret;
+ }
+
+ // VGA IOPORTS
+ for (i = 0 ; i < 2 ; i++) {
+ ret = xc_domain_ioport_permission(CTX->xch, stubdom_domid,
+ vga_ioport_start[i], vga_ioport_size[i], 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give stubdom%d access to ioport range "
+ "%"PRIx64"-%"PRIx64" for VGA passthru",
+ stubdom_domid,
+ vga_ioport_start[i], (vga_ioport_start[i] + vga_ioport_size[i] - 1));
+ return ret;
+ }
+ ret = xc_domain_ioport_permission(CTX->xch, domid,
+ vga_ioport_start[i], vga_ioport_size[i], 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give dom%d access to ioport range "
+ "%"PRIx64"-%"PRIx64" for VGA passthru",
+ domid, vga_ioport_start[i], (vga_ioport_start[i] + vga_ioport_size[i] - 1));
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static int libxl__grant_igd_opregion_permission(libxl__gc *gc, const uint32_t domid) {
+ char* sysfs_path;
+ FILE* f;
+ uint32_t igd_host_opregion;
+ int ret = 0;
+ uint32_t stubdom_domid = libxl_get_stubdom_id(CTX, domid);
+
+ sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/config", 0, 0, 2, 0);
+ f = fopen(sysfs_path, "r");
+ if (!f) {
+ LOGED(ERROR, domid, "Unable to access IGD config space");
+ return ERROR_FAIL;
+ }
+
+ ret = fseek(f, 0xfc, SEEK_SET);
+ if (ret < 0) {
+ LOGED(ERROR, domid, "Unable to lseek in PCI config space");
+ goto out;
+ }
+
+ ret = fread((void*)&igd_host_opregion, 4, 1, f);
+ if (ret < 0) {
+ LOGED(ERROR, domid, "Unable to read opregion register");
+ goto out;
+ }
+
+ ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
+ (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give stubdom%d access to %"PRIx32" opregions for igd passthrough", stubdom_domid, igd_host_opregion);
+ goto out;
+ }
+
+ ret = xc_domain_iomem_permission(CTX->xch, domid,
+ (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
+ if (ret < 0) {
+ LOGED(ERROR, domid,
+ "failed to give dom%d access to %"PRIx32" opregions for igd passthrough", domid, igd_host_opregion);
+ goto out;
+ }
+
+ out:
+ if(f)
+ fclose(f);
+ return ret;
+}
+
int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
libxl_domain_config *const d_config)
{
- int i, ret;
+ int i, ret = 0;
+ bool vga_found = false, igd_found = false;
if (!libxl_defbool_val(d_config->b_info.u.hvm.gfx_passthru))
return 0;
- for (i = 0 ; i < d_config->num_pcidevs ; i++) {
- uint64_t vga_iomem_start = 0xa0000 >> XC_PAGE_SHIFT;
- uint32_t stubdom_domid;
+ for (i = 0 ; i < d_config->num_pcidevs && !igd_found ; i++) {
libxl_device_pci *pci = &d_config->pcidevs[i];
unsigned long pci_device_class;
@@ -2613,30 +2724,19 @@ int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
if (pci_device_class != 0x030000) /* VGA class */
continue;
- stubdom_domid = libxl_get_stubdom_id(CTX, domid);
- ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
- vga_iomem_start, 0x20, 1);
- if (ret < 0) {
- LOGED(ERROR, domid,
- "failed to give stubdom%d access to iomem range "
- "%"PRIx64"-%"PRIx64" for VGA passthru",
- stubdom_domid,
- vga_iomem_start, (vga_iomem_start + 0x20 - 1));
- return ret;
- }
- ret = xc_domain_iomem_permission(CTX->xch, domid,
- vga_iomem_start, 0x20, 1);
- if (ret < 0) {
- LOGED(ERROR, domid,
- "failed to give dom%d access to iomem range "
- "%"PRIx64"-%"PRIx64" for VGA passthru",
- domid, vga_iomem_start, (vga_iomem_start + 0x20 - 1));
- return ret;
- }
- break;
+ vga_found = true;
+ if (pci->bus == 0 && pci->dev == 2 && pci->func == 0)
+ igd_found = true;
}
- return 0;
+ if (vga_found)
+ ret = libxl__grant_legacy_vga_permissions(gc, domid);
+ if (ret < 0)
+ return ret;
+ if (igd_found)
+ ret = libxl__grant_igd_opregion_permission(gc, domid);
+
+ return ret;
}
static int libxl_device_pci_compare(const libxl_device_pci *d1,
--
2.44.0