4 #include "qemu/osdep.h"
6 #include "xen-host-pci-device.h"
7 #include "hw/xen/xen_backend.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
, OBJECT(&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 /* Currently we fixed this address as a primary. */
188 rom
= (struct rom_header
*)bios
;
189 pd
= (void *)(bios
+ (unsigned char)rom
->pcioffset
);
191 /* We may need to fixup Device Identification. */
192 if (pd
->device
!= s
->real_device
.device_id
) {
193 pd
->device
= s
->real_device
.device_id
;
195 len
= rom
->size
* 512;
196 /* Then adjust the bios checksum */
197 for (c
= (char *)bios
; c
< ((char *)bios
+ len
); c
++) {
201 bios
[len
- 1] -= checksum
;
202 XEN_PT_LOG(&s
->dev
, "vga bios checksum is adjusted %x!\n",
207 /* Currently we fixed this address as a primary for legacy BIOS. */
208 cpu_physical_memory_rw(0xc0000, bios
, bios_size
, 1);
211 uint32_t igd_read_opregion(XenPCIPassthroughState
*s
)
215 if (!igd_guest_opregion
) {
219 val
= igd_guest_opregion
;
221 XEN_PT_LOG(&s
->dev
, "Read opregion val=%x\n", val
);
225 #define XEN_PCI_INTEL_OPREGION_PAGES 0x3
226 #define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1
227 void igd_write_opregion(XenPCIPassthroughState
*s
, uint32_t val
)
231 if (igd_guest_opregion
) {
232 XEN_PT_LOG(&s
->dev
, "opregion register already been set, ignoring %x\n",
237 /* We just work with LE. */
238 xen_host_pci_get_block(&s
->real_device
, XEN_PCI_INTEL_OPREGION
,
239 (uint8_t *)&igd_host_opregion
, 4);
240 igd_guest_opregion
= (unsigned long)(val
& ~XEN_PCI_INTEL_OPREGION_MASK
)
241 | (igd_host_opregion
& XEN_PCI_INTEL_OPREGION_MASK
);
243 ret
= xc_domain_iomem_permission(xen_xc
, xen_domid
,
244 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
245 XEN_PCI_INTEL_OPREGION_PAGES
,
246 XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED
);
249 XEN_PT_ERR(&s
->dev
, "[%d]:Can't enable to access IGD host opregion:"
251 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
)),
252 igd_guest_opregion
= 0;
256 ret
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
257 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
),
258 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
259 XEN_PCI_INTEL_OPREGION_PAGES
,
263 XEN_PT_ERR(&s
->dev
, "[%d]:Can't map IGD host opregion:0x%lx to"
264 " guest opregion:0x%lx.\n", ret
,
265 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
266 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));
267 igd_guest_opregion
= 0;
271 XEN_PT_LOG(&s
->dev
, "Map OpRegion: 0x%lx -> 0x%lx\n",
272 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
273 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));