xen_pt_graphics.c 13 KB

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