usb: Fix compilation for Windows
[qemu/ar7.git] / hw / xen / xen_pt_graphics.c
blob71e745f8ca2077cdf75b17f97a4de2c489f81183
1 /*
2 * graphics passthrough
3 */
4 #include "qemu/osdep.h"
5 #include "xen_pt.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 */
19 int rc;
20 } VGARegion;
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,
30 .size = 0xC,
31 .rc = -1,
34 .type = IORESOURCE_IO,
35 .guest_base_addr = 0x3C0,
36 .machine_base_addr = 0x3C0,
37 .size = 0x20,
38 .rc = -1,
41 .type = IORESOURCE_MEM,
42 .guest_base_addr = 0xa0000 >> XC_PAGE_SHIFT,
43 .machine_base_addr = 0xa0000 >> XC_PAGE_SHIFT,
44 .size = 0x20,
45 .rc = -1,
50 * register VGA resources for the domain with assigned gfx
52 int xen_pt_register_vga_regions(XenHostPCIDevice *dev)
54 int i = 0;
56 if (!is_igd_vga_passthrough(dev)) {
57 return 0;
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);
66 } else {
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);
73 if (vga_args[i].rc) {
74 XEN_PT_ERR(NULL, "VGA %s mapping failed! (rc: %i)\n",
75 vga_args[i].type == IORESOURCE_IO ? "ioport" : "memory",
76 vga_args[i].rc);
77 return vga_args[i].rc;
81 return 0;
85 * unregister VGA resources for the domain with assigned gfx
87 int xen_pt_unregister_vga_regions(XenHostPCIDevice *dev)
89 int i = 0;
90 int ret = 0;
92 if (!is_igd_vga_passthrough(dev)) {
93 return 0;
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);
102 } else {
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",
112 vga_args[i].rc);
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);
123 if (ret) {
124 return ret;
128 return 0;
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. */
140 struct rom_header {
141 uint16_t signature;
142 uint8_t size;
143 uint8_t initVector[4];
144 uint8_t reserved[17];
145 uint16_t pcioffset;
146 uint16_t pnpoffset;
147 } __attribute__((packed));
149 struct pci_data {
150 uint32_t signature;
151 uint16_t vendor;
152 uint16_t device;
153 uint16_t vitaldata;
154 uint16_t dlen;
155 uint8_t drevision;
156 uint8_t class_lo;
157 uint16_t class_hi;
158 uint16_t ilen;
159 uint16_t irevision;
160 uint8_t type;
161 uint8_t indicator;
162 uint16_t reserved;
163 } __attribute__((packed));
165 void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,
166 Error **errp)
168 unsigned char *bios = NULL;
169 struct rom_header *rom;
170 int bios_size;
171 char *c = NULL;
172 char checksum = 0;
173 uint32_t len = 0;
174 struct pci_data *pd = NULL;
176 if (!is_igd_vga_passthrough(dev)) {
177 error_setg(errp, "Need to enable igd-passthrough");
178 return;
181 bios = get_vgabios(s, &bios_size, dev);
182 if (!bios) {
183 error_setg(errp, "VGA: Can't get VBIOS");
184 return;
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++) {
198 checksum += *c;
200 if (checksum) {
201 bios[len - 1] -= checksum;
202 XEN_PT_LOG(&s->dev, "vga bios checksum is adjusted %x!\n",
203 checksum);
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)
213 uint32_t val = 0;
215 if (!igd_guest_opregion) {
216 return val;
219 val = igd_guest_opregion;
221 XEN_PT_LOG(&s->dev, "Read opregion val=%x\n", val);
222 return 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)
229 int ret;
231 if (igd_guest_opregion) {
232 XEN_PT_LOG(&s->dev, "opregion register already been set, ignoring %x\n",
233 val);
234 return;
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);
248 if (ret) {
249 XEN_PT_ERR(&s->dev, "[%d]:Can't enable to access IGD host opregion:"
250 " 0x%lx.\n", ret,
251 (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT)),
252 igd_guest_opregion = 0;
253 return;
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,
260 DPCI_ADD_MAPPING);
262 if (ret) {
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;
268 return;
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));