4 #include "qemu/osdep.h"
5 #include "qapi/error.h"
7 #include "xen-host-pci-device.h"
9 static unsigned long igd_guest_opregion
;
10 static unsigned long igd_host_opregion
;
12 #define XEN_PCI_INTEL_OPREGION_MASK 0xfff
14 typedef struct VGARegion
{
15 int type
; /* Memory or port I/O */
16 uint64_t guest_base_addr
;
17 uint64_t machine_base_addr
;
18 uint64_t size
; /* size of the region */
22 #define IORESOURCE_IO 0x00000100
23 #define IORESOURCE_MEM 0x00000200
25 static struct VGARegion vga_args
[] = {
27 .type
= IORESOURCE_IO
,
28 .guest_base_addr
= 0x3B0,
29 .machine_base_addr
= 0x3B0,
34 .type
= IORESOURCE_IO
,
35 .guest_base_addr
= 0x3C0,
36 .machine_base_addr
= 0x3C0,
41 .type
= IORESOURCE_MEM
,
42 .guest_base_addr
= 0xa0000 >> XC_PAGE_SHIFT
,
43 .machine_base_addr
= 0xa0000 >> XC_PAGE_SHIFT
,
50 * register VGA resources for the domain with assigned gfx
52 int xen_pt_register_vga_regions(XenHostPCIDevice
*dev
)
56 if (!is_igd_vga_passthrough(dev
)) {
60 for (i
= 0 ; i
< ARRAY_SIZE(vga_args
); i
++) {
61 if (vga_args
[i
].type
== IORESOURCE_IO
) {
62 vga_args
[i
].rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
63 vga_args
[i
].guest_base_addr
,
64 vga_args
[i
].machine_base_addr
,
65 vga_args
[i
].size
, DPCI_ADD_MAPPING
);
67 vga_args
[i
].rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
68 vga_args
[i
].guest_base_addr
,
69 vga_args
[i
].machine_base_addr
,
70 vga_args
[i
].size
, DPCI_ADD_MAPPING
);
74 XEN_PT_ERR(NULL
, "VGA %s mapping failed! (rc: %i)\n",
75 vga_args
[i
].type
== IORESOURCE_IO
? "ioport" : "memory",
77 return vga_args
[i
].rc
;
85 * unregister VGA resources for the domain with assigned gfx
87 int xen_pt_unregister_vga_regions(XenHostPCIDevice
*dev
)
92 if (!is_igd_vga_passthrough(dev
)) {
96 for (i
= 0 ; i
< ARRAY_SIZE(vga_args
); i
++) {
97 if (vga_args
[i
].type
== IORESOURCE_IO
) {
98 vga_args
[i
].rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
99 vga_args
[i
].guest_base_addr
,
100 vga_args
[i
].machine_base_addr
,
101 vga_args
[i
].size
, DPCI_REMOVE_MAPPING
);
103 vga_args
[i
].rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
104 vga_args
[i
].guest_base_addr
,
105 vga_args
[i
].machine_base_addr
,
106 vga_args
[i
].size
, DPCI_REMOVE_MAPPING
);
109 if (vga_args
[i
].rc
) {
110 XEN_PT_ERR(NULL
, "VGA %s unmapping failed! (rc: %i)\n",
111 vga_args
[i
].type
== IORESOURCE_IO
? "ioport" : "memory",
113 return vga_args
[i
].rc
;
117 if (igd_guest_opregion
) {
118 ret
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
119 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
),
120 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
122 DPCI_REMOVE_MAPPING
);
131 static void *get_vgabios(XenPCIPassthroughState
*s
, int *size
,
132 XenHostPCIDevice
*dev
)
134 return pci_assign_dev_load_option_rom(&s
->dev
, size
,
135 dev
->domain
, dev
->bus
,
136 dev
->dev
, dev
->func
);
139 /* Refer to Seabios. */
143 uint8_t initVector
[4];
144 uint8_t reserved
[17];
147 } __attribute__((packed
));
163 } __attribute__((packed
));
165 void xen_pt_setup_vga(XenPCIPassthroughState
*s
, XenHostPCIDevice
*dev
,
168 unsigned char *bios
= NULL
;
169 struct rom_header
*rom
;
174 struct pci_data
*pd
= NULL
;
176 if (!is_igd_vga_passthrough(dev
)) {
177 error_setg(errp
, "Need to enable igd-passthrough");
181 bios
= get_vgabios(s
, &bios_size
, dev
);
183 error_setg(errp
, "VGA: Can't get VBIOS");
187 if (bios_size
< sizeof(struct rom_header
)) {
188 error_setg(errp
, "VGA: VBIOS image corrupt (too small)");
192 /* Currently we fixed this address as a primary. */
193 rom
= (struct rom_header
*)bios
;
195 if (rom
->pcioffset
+ sizeof(struct pci_data
) > bios_size
) {
196 error_setg(errp
, "VGA: VBIOS image corrupt (bad pcioffset field)");
200 pd
= (void *)(bios
+ (unsigned char)rom
->pcioffset
);
202 /* We may need to fixup Device Identification. */
203 if (pd
->device
!= s
->real_device
.device_id
) {
204 pd
->device
= s
->real_device
.device_id
;
206 len
= rom
->size
* 512;
207 if (len
> bios_size
) {
208 error_setg(errp
, "VGA: VBIOS image corrupt (bad size field)");
212 /* Then adjust the bios checksum */
213 for (c
= (char *)bios
; c
< ((char *)bios
+ len
); c
++) {
217 bios
[len
- 1] -= checksum
;
218 XEN_PT_LOG(&s
->dev
, "vga bios checksum is adjusted %x!\n",
223 /* Currently we fixed this address as a primary for legacy BIOS. */
224 cpu_physical_memory_write(0xc0000, bios
, bios_size
);
227 uint32_t igd_read_opregion(XenPCIPassthroughState
*s
)
231 if (!igd_guest_opregion
) {
235 val
= igd_guest_opregion
;
237 XEN_PT_LOG(&s
->dev
, "Read opregion val=%x\n", val
);
241 #define XEN_PCI_INTEL_OPREGION_PAGES 0x3
242 #define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1
243 void igd_write_opregion(XenPCIPassthroughState
*s
, uint32_t val
)
247 if (igd_guest_opregion
) {
248 XEN_PT_LOG(&s
->dev
, "opregion register already been set, ignoring %x\n",
253 /* We just work with LE. */
254 xen_host_pci_get_block(&s
->real_device
, XEN_PCI_INTEL_OPREGION
,
255 (uint8_t *)&igd_host_opregion
, 4);
256 igd_guest_opregion
= (unsigned long)(val
& ~XEN_PCI_INTEL_OPREGION_MASK
)
257 | (igd_host_opregion
& XEN_PCI_INTEL_OPREGION_MASK
);
259 ret
= xc_domain_iomem_permission(xen_xc
, xen_domid
,
260 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
261 XEN_PCI_INTEL_OPREGION_PAGES
,
262 XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED
);
265 XEN_PT_ERR(&s
->dev
, "[%d]:Can't enable to access IGD host opregion:"
267 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
)),
268 igd_guest_opregion
= 0;
272 ret
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
273 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
),
274 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
275 XEN_PCI_INTEL_OPREGION_PAGES
,
279 XEN_PT_ERR(&s
->dev
, "[%d]:Can't map IGD host opregion:0x%lx to"
280 " guest opregion:0x%lx.\n", ret
,
281 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
282 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));
283 igd_guest_opregion
= 0;
287 XEN_PT_LOG(&s
->dev
, "Map OpRegion: 0x%lx -> 0x%lx\n",
288 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
289 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));
293 uint16_t gpu_device_id
;
294 uint16_t pch_device_id
;
295 uint8_t pch_revision_id
;
299 * In real world different GPU should have different PCH. But actually
300 * the different PCH DIDs likely map to different PCH SKUs. We do the
301 * same thing for the GPU. For PCH, the different SKUs are going to be
302 * all the same silicon design and implementation, just different
303 * features turn on and off with fuses. The SW interfaces should be
304 * consistent across all SKUs in a given family (eg LPT). But just same
305 * features may not be supported.
307 * Most of these different PCH features probably don't matter to the
308 * Gfx driver, but obviously any difference in display port connections
309 * will so it should be fine with any PCH in case of passthrough.
311 * So currently use one PCH version, 0x8c4e, to cover all HSW(Haswell)
312 * scenarios, 0x9cc3 for BDW(Broadwell).
314 static const IGDDeviceIDInfo igd_combo_id_infos
[] = {
316 {0x0402, 0x8c4e, 0x04}, /* HSWGT1D, HSWD_w7 */
317 {0x0406, 0x8c4e, 0x04}, /* HSWGT1M, HSWM_w7 */
318 {0x0412, 0x8c4e, 0x04}, /* HSWGT2D, HSWD_w7 */
319 {0x0416, 0x8c4e, 0x04}, /* HSWGT2M, HSWM_w7 */
320 {0x041E, 0x8c4e, 0x04}, /* HSWGT15D, HSWD_w7 */
322 {0x0A06, 0x8c4e, 0x04}, /* HSWGT1UT, HSWM_w7 */
323 {0x0A16, 0x8c4e, 0x04}, /* HSWGT2UT, HSWM_w7 */
324 {0x0A26, 0x8c4e, 0x06}, /* HSWGT3UT, HSWM_w7 */
325 {0x0A2E, 0x8c4e, 0x04}, /* HSWGT3UT28W, HSWM_w7 */
326 {0x0A1E, 0x8c4e, 0x04}, /* HSWGT2UX, HSWM_w7 */
327 {0x0A0E, 0x8c4e, 0x04}, /* HSWGT1ULX, HSWM_w7 */
329 {0x0D26, 0x8c4e, 0x04}, /* HSWGT3CW, HSWM_w7 */
330 {0x0D22, 0x8c4e, 0x04}, /* HSWGT3CWDT, HSWD_w7 */
332 {0x041A, 0x8c4e, 0x04}, /* HSWSVGT2, HSWD_w7 */
334 {0x040A, 0x8c4e, 0x04}, /* HSWSVGT1, HSWD_w7 */
336 {0x1606, 0x9cc3, 0x03}, /* BDWULTGT1, BDWM_w7 */
337 {0x1616, 0x9cc3, 0x03}, /* BDWULTGT2, BDWM_w7 */
338 {0x1626, 0x9cc3, 0x03}, /* BDWULTGT3, BDWM_w7 */
339 {0x160E, 0x9cc3, 0x03}, /* BDWULXGT1, BDWM_w7 */
340 {0x161E, 0x9cc3, 0x03}, /* BDWULXGT2, BDWM_w7 */
341 {0x1602, 0x9cc3, 0x03}, /* BDWHALOGT1, BDWM_w7 */
342 {0x1612, 0x9cc3, 0x03}, /* BDWHALOGT2, BDWM_w7 */
343 {0x1622, 0x9cc3, 0x03}, /* BDWHALOGT3, BDWM_w7 */
344 {0x162B, 0x9cc3, 0x03}, /* BDWHALO28W, BDWM_w7 */
345 {0x162A, 0x9cc3, 0x03}, /* BDWGT3WRKS, BDWM_w7 */
346 {0x162D, 0x9cc3, 0x03}, /* BDWGT3SRVR, BDWM_w7 */
349 static void isa_bridge_class_init(ObjectClass
*klass
, void *data
)
351 DeviceClass
*dc
= DEVICE_CLASS(klass
);
352 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
354 dc
->desc
= "ISA bridge faked to support IGD PT";
355 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
356 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
357 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
360 static const TypeInfo isa_bridge_info
= {
361 .name
= "igd-passthrough-isa-bridge",
362 .parent
= TYPE_PCI_DEVICE
,
363 .instance_size
= sizeof(PCIDevice
),
364 .class_init
= isa_bridge_class_init
,
365 .interfaces
= (InterfaceInfo
[]) {
366 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
371 static void pt_graphics_register_types(void)
373 type_register_static(&isa_bridge_info
);
375 type_init(pt_graphics_register_types
)
377 void xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState
*s
,
378 XenHostPCIDevice
*dev
)
380 PCIBus
*bus
= pci_get_bus(&s
->dev
);
381 struct PCIDevice
*bridge_dev
;
383 const uint16_t gpu_dev_id
= dev
->device_id
;
384 uint16_t pch_dev_id
= 0xffff;
385 uint8_t pch_rev_id
= 0;
387 num
= ARRAY_SIZE(igd_combo_id_infos
);
388 for (i
= 0; i
< num
; i
++) {
389 if (gpu_dev_id
== igd_combo_id_infos
[i
].gpu_device_id
) {
390 pch_dev_id
= igd_combo_id_infos
[i
].pch_device_id
;
391 pch_rev_id
= igd_combo_id_infos
[i
].pch_revision_id
;
395 if (pch_dev_id
== 0xffff) {
399 /* Currently IGD drivers always need to access PCH by 1f.0. */
400 bridge_dev
= pci_create_simple(bus
, PCI_DEVFN(0x1f, 0),
401 "igd-passthrough-isa-bridge");
404 * Note that vendor id is always PCI_VENDOR_ID_INTEL.
407 fprintf(stderr
, "set igd-passthrough-isa-bridge failed!\n");
410 pci_config_set_device_id(bridge_dev
->config
, pch_dev_id
);
411 pci_config_set_revision(bridge_dev
->config
, pch_rev_id
);