xen_pt_graphics.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * graphics passthrough
  3. */
  4. #include "qemu/osdep.h"
  5. #include "qapi/error.h"
  6. #include "hw/xen/xen_pt.h"
  7. #include "hw/xen/xen_igd.h"
  8. #include "xen-host-pci-device.h"
  9. static unsigned long igd_guest_opregion;
  10. static unsigned long igd_host_opregion;
  11. #define XEN_PCI_INTEL_OPREGION_MASK 0xfff
  12. typedef struct VGARegion {
  13. int type; /* Memory or port I/O */
  14. uint64_t guest_base_addr;
  15. uint64_t machine_base_addr;
  16. uint64_t size; /* size of the region */
  17. int rc;
  18. } VGARegion;
  19. #define IORESOURCE_IO 0x00000100
  20. #define IORESOURCE_MEM 0x00000200
  21. static struct VGARegion vga_args[] = {
  22. {
  23. .type = IORESOURCE_IO,
  24. .guest_base_addr = 0x3B0,
  25. .machine_base_addr = 0x3B0,
  26. .size = 0xC,
  27. .rc = -1,
  28. },
  29. {
  30. .type = IORESOURCE_IO,
  31. .guest_base_addr = 0x3C0,
  32. .machine_base_addr = 0x3C0,
  33. .size = 0x20,
  34. .rc = -1,
  35. },
  36. {
  37. .type = IORESOURCE_MEM,
  38. .guest_base_addr = 0xa0000 >> XC_PAGE_SHIFT,
  39. .machine_base_addr = 0xa0000 >> XC_PAGE_SHIFT,
  40. .size = 0x20,
  41. .rc = -1,
  42. },
  43. };
  44. /*
  45. * register VGA resources for the domain with assigned gfx
  46. */
  47. int xen_pt_register_vga_regions(XenHostPCIDevice *dev)
  48. {
  49. int i = 0;
  50. if (!is_igd_vga_passthrough(dev)) {
  51. return 0;
  52. }
  53. for (i = 0 ; i < ARRAY_SIZE(vga_args); i++) {
  54. if (vga_args[i].type == IORESOURCE_IO) {
  55. vga_args[i].rc = xc_domain_ioport_mapping(xen_xc, xen_domid,
  56. vga_args[i].guest_base_addr,
  57. vga_args[i].machine_base_addr,
  58. vga_args[i].size, DPCI_ADD_MAPPING);
  59. } else {
  60. vga_args[i].rc = xc_domain_memory_mapping(xen_xc, xen_domid,
  61. vga_args[i].guest_base_addr,
  62. vga_args[i].machine_base_addr,
  63. vga_args[i].size, DPCI_ADD_MAPPING);
  64. }
  65. if (vga_args[i].rc) {
  66. XEN_PT_ERR(NULL, "VGA %s mapping failed! (rc: %i)\n",
  67. vga_args[i].type == IORESOURCE_IO ? "ioport" : "memory",
  68. vga_args[i].rc);
  69. return vga_args[i].rc;
  70. }
  71. }
  72. return 0;
  73. }
  74. /*
  75. * unregister VGA resources for the domain with assigned gfx
  76. */
  77. int xen_pt_unregister_vga_regions(XenHostPCIDevice *dev)
  78. {
  79. int i = 0;
  80. int ret = 0;
  81. if (!is_igd_vga_passthrough(dev)) {
  82. return 0;
  83. }
  84. for (i = 0 ; i < ARRAY_SIZE(vga_args); i++) {
  85. if (vga_args[i].type == IORESOURCE_IO) {
  86. vga_args[i].rc = xc_domain_ioport_mapping(xen_xc, xen_domid,
  87. vga_args[i].guest_base_addr,
  88. vga_args[i].machine_base_addr,
  89. vga_args[i].size, DPCI_REMOVE_MAPPING);
  90. } else {
  91. vga_args[i].rc = xc_domain_memory_mapping(xen_xc, xen_domid,
  92. vga_args[i].guest_base_addr,
  93. vga_args[i].machine_base_addr,
  94. vga_args[i].size, DPCI_REMOVE_MAPPING);
  95. }
  96. if (vga_args[i].rc) {
  97. XEN_PT_ERR(NULL, "VGA %s unmapping failed! (rc: %i)\n",
  98. vga_args[i].type == IORESOURCE_IO ? "ioport" : "memory",
  99. vga_args[i].rc);
  100. return vga_args[i].rc;
  101. }
  102. }
  103. if (igd_guest_opregion) {
  104. ret = xc_domain_memory_mapping(xen_xc, xen_domid,
  105. (unsigned long)(igd_guest_opregion >> XC_PAGE_SHIFT),
  106. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT),
  107. 3,
  108. DPCI_REMOVE_MAPPING);
  109. if (ret) {
  110. return ret;
  111. }
  112. }
  113. return 0;
  114. }
  115. static void *get_vgabios(XenPCIPassthroughState *s, int *size,
  116. XenHostPCIDevice *dev)
  117. {
  118. return pci_assign_dev_load_option_rom(&s->dev, size,
  119. dev->domain, dev->bus,
  120. dev->dev, dev->func);
  121. }
  122. /* Refer to Seabios. */
  123. struct rom_header {
  124. uint16_t signature;
  125. uint8_t size;
  126. uint8_t initVector[4];
  127. uint8_t reserved[17];
  128. uint16_t pcioffset;
  129. uint16_t pnpoffset;
  130. } __attribute__((packed));
  131. struct pci_data {
  132. uint32_t signature;
  133. uint16_t vendor;
  134. uint16_t device;
  135. uint16_t vitaldata;
  136. uint16_t dlen;
  137. uint8_t drevision;
  138. uint8_t class_lo;
  139. uint16_t class_hi;
  140. uint16_t ilen;
  141. uint16_t irevision;
  142. uint8_t type;
  143. uint8_t indicator;
  144. uint16_t reserved;
  145. } __attribute__((packed));
  146. void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,
  147. Error **errp)
  148. {
  149. unsigned char *bios = NULL;
  150. struct rom_header *rom;
  151. int bios_size;
  152. char *c = NULL;
  153. char checksum = 0;
  154. uint32_t len = 0;
  155. struct pci_data *pd = NULL;
  156. if (!is_igd_vga_passthrough(dev)) {
  157. error_setg(errp, "Need to enable igd-passthrough");
  158. return;
  159. }
  160. bios = get_vgabios(s, &bios_size, dev);
  161. if (!bios) {
  162. error_setg(errp, "VGA: Can't get VBIOS");
  163. return;
  164. }
  165. if (bios_size < sizeof(struct rom_header)) {
  166. error_setg(errp, "VGA: VBIOS image corrupt (too small)");
  167. return;
  168. }
  169. /* Currently we fixed this address as a primary. */
  170. rom = (struct rom_header *)bios;
  171. if (rom->pcioffset + sizeof(struct pci_data) > bios_size) {
  172. error_setg(errp, "VGA: VBIOS image corrupt (bad pcioffset field)");
  173. return;
  174. }
  175. pd = (void *)(bios + (unsigned char)rom->pcioffset);
  176. /* We may need to fixup Device Identification. */
  177. if (pd->device != s->real_device.device_id) {
  178. pd->device = s->real_device.device_id;
  179. len = rom->size * 512;
  180. if (len > bios_size) {
  181. error_setg(errp, "VGA: VBIOS image corrupt (bad size field)");
  182. return;
  183. }
  184. /* Then adjust the bios checksum */
  185. for (c = (char *)bios; c < ((char *)bios + len); c++) {
  186. checksum += *c;
  187. }
  188. if (checksum) {
  189. bios[len - 1] -= checksum;
  190. XEN_PT_LOG(&s->dev, "vga bios checksum is adjusted %x!\n",
  191. checksum);
  192. }
  193. }
  194. /* Currently we fixed this address as a primary for legacy BIOS. */
  195. cpu_physical_memory_write(0xc0000, bios, bios_size);
  196. }
  197. uint32_t igd_read_opregion(XenPCIPassthroughState *s)
  198. {
  199. uint32_t val = 0;
  200. if (!igd_guest_opregion) {
  201. return val;
  202. }
  203. val = igd_guest_opregion;
  204. XEN_PT_LOG(&s->dev, "Read opregion val=%x\n", val);
  205. return val;
  206. }
  207. #define XEN_PCI_INTEL_OPREGION_PAGES 0x3
  208. #define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1
  209. void igd_write_opregion(XenPCIPassthroughState *s, uint32_t val)
  210. {
  211. int ret;
  212. if (igd_guest_opregion) {
  213. XEN_PT_LOG(&s->dev, "opregion register already been set, ignoring %x\n",
  214. val);
  215. return;
  216. }
  217. /* We just work with LE. */
  218. xen_host_pci_get_block(&s->real_device, XEN_PCI_INTEL_OPREGION,
  219. (uint8_t *)&igd_host_opregion, 4);
  220. igd_guest_opregion = (unsigned long)(val & ~XEN_PCI_INTEL_OPREGION_MASK)
  221. | (igd_host_opregion & XEN_PCI_INTEL_OPREGION_MASK);
  222. ret = xc_domain_iomem_permission(xen_xc, xen_domid,
  223. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT),
  224. XEN_PCI_INTEL_OPREGION_PAGES,
  225. XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED);
  226. if (ret) {
  227. XEN_PT_ERR(&s->dev, "[%d]:Can't enable to access IGD host opregion:"
  228. " 0x%lx.\n", ret,
  229. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT)),
  230. igd_guest_opregion = 0;
  231. return;
  232. }
  233. ret = xc_domain_memory_mapping(xen_xc, xen_domid,
  234. (unsigned long)(igd_guest_opregion >> XC_PAGE_SHIFT),
  235. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT),
  236. XEN_PCI_INTEL_OPREGION_PAGES,
  237. DPCI_ADD_MAPPING);
  238. if (ret) {
  239. XEN_PT_ERR(&s->dev, "[%d]:Can't map IGD host opregion:0x%lx to"
  240. " guest opregion:0x%lx.\n", ret,
  241. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT),
  242. (unsigned long)(igd_guest_opregion >> XC_PAGE_SHIFT));
  243. igd_guest_opregion = 0;
  244. return;
  245. }
  246. XEN_PT_LOG(&s->dev, "Map OpRegion: 0x%lx -> 0x%lx\n",
  247. (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT),
  248. (unsigned long)(igd_guest_opregion >> XC_PAGE_SHIFT));
  249. }
  250. typedef struct {
  251. uint16_t gpu_device_id;
  252. uint16_t pch_device_id;
  253. uint8_t pch_revision_id;
  254. } IGDDeviceIDInfo;
  255. /*
  256. * In real world different GPU should have different PCH. But actually
  257. * the different PCH DIDs likely map to different PCH SKUs. We do the
  258. * same thing for the GPU. For PCH, the different SKUs are going to be
  259. * all the same silicon design and implementation, just different
  260. * features turn on and off with fuses. The SW interfaces should be
  261. * consistent across all SKUs in a given family (eg LPT). But just same
  262. * features may not be supported.
  263. *
  264. * Most of these different PCH features probably don't matter to the
  265. * Gfx driver, but obviously any difference in display port connections
  266. * will so it should be fine with any PCH in case of passthrough.
  267. *
  268. * So currently use one PCH version, 0x8c4e, to cover all HSW(Haswell)
  269. * scenarios, 0x9cc3 for BDW(Broadwell).
  270. */
  271. static const IGDDeviceIDInfo igd_combo_id_infos[] = {
  272. /* HSW Classic */
  273. {0x0402, 0x8c4e, 0x04}, /* HSWGT1D, HSWD_w7 */
  274. {0x0406, 0x8c4e, 0x04}, /* HSWGT1M, HSWM_w7 */
  275. {0x0412, 0x8c4e, 0x04}, /* HSWGT2D, HSWD_w7 */
  276. {0x0416, 0x8c4e, 0x04}, /* HSWGT2M, HSWM_w7 */
  277. {0x041E, 0x8c4e, 0x04}, /* HSWGT15D, HSWD_w7 */
  278. /* HSW ULT */
  279. {0x0A06, 0x8c4e, 0x04}, /* HSWGT1UT, HSWM_w7 */
  280. {0x0A16, 0x8c4e, 0x04}, /* HSWGT2UT, HSWM_w7 */
  281. {0x0A26, 0x8c4e, 0x06}, /* HSWGT3UT, HSWM_w7 */
  282. {0x0A2E, 0x8c4e, 0x04}, /* HSWGT3UT28W, HSWM_w7 */
  283. {0x0A1E, 0x8c4e, 0x04}, /* HSWGT2UX, HSWM_w7 */
  284. {0x0A0E, 0x8c4e, 0x04}, /* HSWGT1ULX, HSWM_w7 */
  285. /* HSW CRW */
  286. {0x0D26, 0x8c4e, 0x04}, /* HSWGT3CW, HSWM_w7 */
  287. {0x0D22, 0x8c4e, 0x04}, /* HSWGT3CWDT, HSWD_w7 */
  288. /* HSW Server */
  289. {0x041A, 0x8c4e, 0x04}, /* HSWSVGT2, HSWD_w7 */
  290. /* HSW SRVR */
  291. {0x040A, 0x8c4e, 0x04}, /* HSWSVGT1, HSWD_w7 */
  292. /* BSW */
  293. {0x1606, 0x9cc3, 0x03}, /* BDWULTGT1, BDWM_w7 */
  294. {0x1616, 0x9cc3, 0x03}, /* BDWULTGT2, BDWM_w7 */
  295. {0x1626, 0x9cc3, 0x03}, /* BDWULTGT3, BDWM_w7 */
  296. {0x160E, 0x9cc3, 0x03}, /* BDWULXGT1, BDWM_w7 */
  297. {0x161E, 0x9cc3, 0x03}, /* BDWULXGT2, BDWM_w7 */
  298. {0x1602, 0x9cc3, 0x03}, /* BDWHALOGT1, BDWM_w7 */
  299. {0x1612, 0x9cc3, 0x03}, /* BDWHALOGT2, BDWM_w7 */
  300. {0x1622, 0x9cc3, 0x03}, /* BDWHALOGT3, BDWM_w7 */
  301. {0x162B, 0x9cc3, 0x03}, /* BDWHALO28W, BDWM_w7 */
  302. {0x162A, 0x9cc3, 0x03}, /* BDWGT3WRKS, BDWM_w7 */
  303. {0x162D, 0x9cc3, 0x03}, /* BDWGT3SRVR, BDWM_w7 */
  304. };
  305. static void isa_bridge_class_init(ObjectClass *klass, void *data)
  306. {
  307. DeviceClass *dc = DEVICE_CLASS(klass);
  308. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  309. dc->desc = "ISA bridge faked to support IGD PT";
  310. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  311. k->vendor_id = PCI_VENDOR_ID_INTEL;
  312. k->class_id = PCI_CLASS_BRIDGE_ISA;
  313. };
  314. static const TypeInfo isa_bridge_info = {
  315. .name = "igd-passthrough-isa-bridge",
  316. .parent = TYPE_PCI_DEVICE,
  317. .instance_size = sizeof(PCIDevice),
  318. .class_init = isa_bridge_class_init,
  319. .interfaces = (InterfaceInfo[]) {
  320. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  321. { },
  322. },
  323. };
  324. static void pt_graphics_register_types(void)
  325. {
  326. type_register_static(&isa_bridge_info);
  327. }
  328. type_init(pt_graphics_register_types)
  329. void xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState *s,
  330. XenHostPCIDevice *dev)
  331. {
  332. PCIBus *bus = pci_get_bus(&s->dev);
  333. struct PCIDevice *bridge_dev;
  334. int i, num;
  335. const uint16_t gpu_dev_id = dev->device_id;
  336. uint16_t pch_dev_id = 0xffff;
  337. uint8_t pch_rev_id = 0;
  338. num = ARRAY_SIZE(igd_combo_id_infos);
  339. for (i = 0; i < num; i++) {
  340. if (gpu_dev_id == igd_combo_id_infos[i].gpu_device_id) {
  341. pch_dev_id = igd_combo_id_infos[i].pch_device_id;
  342. pch_rev_id = igd_combo_id_infos[i].pch_revision_id;
  343. }
  344. }
  345. if (pch_dev_id == 0xffff) {
  346. return;
  347. }
  348. /* Currently IGD drivers always need to access PCH by 1f.0. */
  349. bridge_dev = pci_create_simple(bus, PCI_DEVFN(0x1f, 0),
  350. "igd-passthrough-isa-bridge");
  351. /*
  352. * Note that vendor id is always PCI_VENDOR_ID_INTEL.
  353. */
  354. if (!bridge_dev) {
  355. fprintf(stderr, "set igd-passthrough-isa-bridge failed!\n");
  356. return;
  357. }
  358. pci_config_set_device_id(bridge_dev->config, pch_dev_id);
  359. pci_config_set_revision(bridge_dev->config, pch_rev_id);
  360. }