xen_igd_pt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * QEMU Intel IGD Passthrough Host Bridge Emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/pci/pci.h"
  28. #include "hw/pci/pci_host.h"
  29. #include "hw/pci-host/i440fx.h"
  30. #include "qapi/error.h"
  31. typedef struct {
  32. uint8_t offset;
  33. uint8_t len;
  34. } IGDHostInfo;
  35. /* Here we just expose minimal host bridge offset subset. */
  36. static const IGDHostInfo igd_host_bridge_infos[] = {
  37. {PCI_REVISION_ID, 2},
  38. {PCI_SUBSYSTEM_VENDOR_ID, 2},
  39. {PCI_SUBSYSTEM_ID, 2},
  40. {0x50, 2}, /* SNB: processor graphics control register */
  41. {0x52, 2}, /* processor graphics control register */
  42. {0xa4, 4}, /* SNB: graphics base of stolen memory */
  43. {0xa8, 4}, /* SNB: base of GTT stolen memory */
  44. };
  45. static void host_pci_config_read(int pos, int len, uint32_t *val, Error **errp)
  46. {
  47. int rc, config_fd;
  48. /* Access real host bridge. */
  49. char *path = g_strdup_printf("/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
  50. 0, 0, 0, 0, "config");
  51. config_fd = open(path, O_RDWR);
  52. if (config_fd < 0) {
  53. error_setg_errno(errp, errno, "Failed to open: %s", path);
  54. goto out;
  55. }
  56. if (lseek(config_fd, pos, SEEK_SET) != pos) {
  57. error_setg_errno(errp, errno, "Failed to seek: %s", path);
  58. goto out_close_fd;
  59. }
  60. do {
  61. rc = read(config_fd, (uint8_t *)val, len);
  62. } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
  63. if (rc != len) {
  64. error_setg_errno(errp, errno, "Failed to read: %s", path);
  65. }
  66. out_close_fd:
  67. close(config_fd);
  68. out:
  69. g_free(path);
  70. }
  71. static void igd_pt_i440fx_realize(PCIDevice *pci_dev, Error **errp)
  72. {
  73. ERRP_GUARD();
  74. uint32_t val = 0;
  75. size_t i;
  76. int pos, len;
  77. for (i = 0; i < ARRAY_SIZE(igd_host_bridge_infos); i++) {
  78. pos = igd_host_bridge_infos[i].offset;
  79. len = igd_host_bridge_infos[i].len;
  80. host_pci_config_read(pos, len, &val, errp);
  81. if (*errp) {
  82. return;
  83. }
  84. pci_default_write_config(pci_dev, pos, val, len);
  85. }
  86. }
  87. static void igd_passthrough_i440fx_class_init(ObjectClass *klass, void *data)
  88. {
  89. DeviceClass *dc = DEVICE_CLASS(klass);
  90. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  91. k->realize = igd_pt_i440fx_realize;
  92. dc->desc = "IGD Passthrough Host bridge";
  93. }
  94. static const TypeInfo igd_passthrough_i440fx_info = {
  95. .name = TYPE_IGD_PASSTHROUGH_I440FX_PCI_DEVICE,
  96. .parent = TYPE_I440FX_PCI_DEVICE,
  97. .instance_size = sizeof(PCII440FXState),
  98. .class_init = igd_passthrough_i440fx_class_init,
  99. };
  100. static void igd_pt_i440fx_register_types(void)
  101. {
  102. type_register_static(&igd_passthrough_i440fx_info);
  103. }
  104. type_init(igd_pt_i440fx_register_types)