4 #include "qemu/osdep.h"
5 #include "qapi/error.h"
7 #include "xen-host-pci-device.h"
8 #include "hw/xen/xen-legacy-backend.h"
10 static unsigned long igd_guest_opregion
;
11 static unsigned long igd_host_opregion
;
13 #define XEN_PCI_INTEL_OPREGION_MASK 0xfff
15 typedef struct VGARegion
{
16 int type
; /* Memory or port I/O */
17 uint64_t guest_base_addr
;
18 uint64_t machine_base_addr
;
19 uint64_t size
; /* size of the region */
23 #define IORESOURCE_IO 0x00000100
24 #define IORESOURCE_MEM 0x00000200
26 static struct VGARegion vga_args
[] = {
28 .type
= IORESOURCE_IO
,
29 .guest_base_addr
= 0x3B0,
30 .machine_base_addr
= 0x3B0,
35 .type
= IORESOURCE_IO
,
36 .guest_base_addr
= 0x3C0,
37 .machine_base_addr
= 0x3C0,
42 .type
= IORESOURCE_MEM
,
43 .guest_base_addr
= 0xa0000 >> XC_PAGE_SHIFT
,
44 .machine_base_addr
= 0xa0000 >> XC_PAGE_SHIFT
,
51 * register VGA resources for the domain with assigned gfx
53 int xen_pt_register_vga_regions(XenHostPCIDevice
*dev
)
57 if (!is_igd_vga_passthrough(dev
)) {
61 for (i
= 0 ; i
< ARRAY_SIZE(vga_args
); i
++) {
62 if (vga_args
[i
].type
== IORESOURCE_IO
) {
63 vga_args
[i
].rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
64 vga_args
[i
].guest_base_addr
,
65 vga_args
[i
].machine_base_addr
,
66 vga_args
[i
].size
, DPCI_ADD_MAPPING
);
68 vga_args
[i
].rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
69 vga_args
[i
].guest_base_addr
,
70 vga_args
[i
].machine_base_addr
,
71 vga_args
[i
].size
, DPCI_ADD_MAPPING
);
75 XEN_PT_ERR(NULL
, "VGA %s mapping failed! (rc: %i)\n",
76 vga_args
[i
].type
== IORESOURCE_IO
? "ioport" : "memory",
78 return vga_args
[i
].rc
;
86 * unregister VGA resources for the domain with assigned gfx
88 int xen_pt_unregister_vga_regions(XenHostPCIDevice
*dev
)
93 if (!is_igd_vga_passthrough(dev
)) {
97 for (i
= 0 ; i
< ARRAY_SIZE(vga_args
); i
++) {
98 if (vga_args
[i
].type
== IORESOURCE_IO
) {
99 vga_args
[i
].rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
100 vga_args
[i
].guest_base_addr
,
101 vga_args
[i
].machine_base_addr
,
102 vga_args
[i
].size
, DPCI_REMOVE_MAPPING
);
104 vga_args
[i
].rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
105 vga_args
[i
].guest_base_addr
,
106 vga_args
[i
].machine_base_addr
,
107 vga_args
[i
].size
, DPCI_REMOVE_MAPPING
);
110 if (vga_args
[i
].rc
) {
111 XEN_PT_ERR(NULL
, "VGA %s unmapping failed! (rc: %i)\n",
112 vga_args
[i
].type
== IORESOURCE_IO
? "ioport" : "memory",
114 return vga_args
[i
].rc
;
118 if (igd_guest_opregion
) {
119 ret
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
120 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
),
121 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
123 DPCI_REMOVE_MAPPING
);
132 static void *get_vgabios(XenPCIPassthroughState
*s
, int *size
,
133 XenHostPCIDevice
*dev
)
135 return pci_assign_dev_load_option_rom(&s
->dev
, size
,
136 dev
->domain
, dev
->bus
,
137 dev
->dev
, dev
->func
);
140 /* Refer to Seabios. */
144 uint8_t initVector
[4];
145 uint8_t reserved
[17];
148 } __attribute__((packed
));
164 } __attribute__((packed
));
166 void xen_pt_setup_vga(XenPCIPassthroughState
*s
, XenHostPCIDevice
*dev
,
169 unsigned char *bios
= NULL
;
170 struct rom_header
*rom
;
175 struct pci_data
*pd
= NULL
;
177 if (!is_igd_vga_passthrough(dev
)) {
178 error_setg(errp
, "Need to enable igd-passthrough");
182 bios
= get_vgabios(s
, &bios_size
, dev
);
184 error_setg(errp
, "VGA: Can't get VBIOS");
188 if (bios_size
< sizeof(struct rom_header
)) {
189 error_setg(errp
, "VGA: VBIOS image corrupt (too small)");
193 /* Currently we fixed this address as a primary. */
194 rom
= (struct rom_header
*)bios
;
196 if (rom
->pcioffset
+ sizeof(struct pci_data
) > bios_size
) {
197 error_setg(errp
, "VGA: VBIOS image corrupt (bad pcioffset field)");
201 pd
= (void *)(bios
+ (unsigned char)rom
->pcioffset
);
203 /* We may need to fixup Device Identification. */
204 if (pd
->device
!= s
->real_device
.device_id
) {
205 pd
->device
= s
->real_device
.device_id
;
207 len
= rom
->size
* 512;
208 if (len
> bios_size
) {
209 error_setg(errp
, "VGA: VBIOS image corrupt (bad size field)");
213 /* Then adjust the bios checksum */
214 for (c
= (char *)bios
; c
< ((char *)bios
+ len
); c
++) {
218 bios
[len
- 1] -= checksum
;
219 XEN_PT_LOG(&s
->dev
, "vga bios checksum is adjusted %x!\n",
224 /* Currently we fixed this address as a primary for legacy BIOS. */
225 cpu_physical_memory_write(0xc0000, bios
, bios_size
);
228 uint32_t igd_read_opregion(XenPCIPassthroughState
*s
)
232 if (!igd_guest_opregion
) {
236 val
= igd_guest_opregion
;
238 XEN_PT_LOG(&s
->dev
, "Read opregion val=%x\n", val
);
242 #define XEN_PCI_INTEL_OPREGION_PAGES 0x3
243 #define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1
244 void igd_write_opregion(XenPCIPassthroughState
*s
, uint32_t val
)
248 if (igd_guest_opregion
) {
249 XEN_PT_LOG(&s
->dev
, "opregion register already been set, ignoring %x\n",
254 /* We just work with LE. */
255 xen_host_pci_get_block(&s
->real_device
, XEN_PCI_INTEL_OPREGION
,
256 (uint8_t *)&igd_host_opregion
, 4);
257 igd_guest_opregion
= (unsigned long)(val
& ~XEN_PCI_INTEL_OPREGION_MASK
)
258 | (igd_host_opregion
& XEN_PCI_INTEL_OPREGION_MASK
);
260 ret
= xc_domain_iomem_permission(xen_xc
, xen_domid
,
261 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
262 XEN_PCI_INTEL_OPREGION_PAGES
,
263 XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED
);
266 XEN_PT_ERR(&s
->dev
, "[%d]:Can't enable to access IGD host opregion:"
268 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
)),
269 igd_guest_opregion
= 0;
273 ret
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
274 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
),
275 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
276 XEN_PCI_INTEL_OPREGION_PAGES
,
280 XEN_PT_ERR(&s
->dev
, "[%d]:Can't map IGD host opregion:0x%lx to"
281 " guest opregion:0x%lx.\n", ret
,
282 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
283 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));
284 igd_guest_opregion
= 0;
288 XEN_PT_LOG(&s
->dev
, "Map OpRegion: 0x%lx -> 0x%lx\n",
289 (unsigned long)(igd_host_opregion
>> XC_PAGE_SHIFT
),
290 (unsigned long)(igd_guest_opregion
>> XC_PAGE_SHIFT
));