1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "hw/pci/pci.h"
29#include "hw/pci/pci_host.h"
30#include "hw/pci-host/i440fx.h"
31#include "qapi/error.h"
32
33typedef struct {
34 uint8_t offset;
35 uint8_t len;
36} IGDHostInfo;
37
38
39static const IGDHostInfo igd_host_bridge_infos[] = {
40 {PCI_REVISION_ID, 2},
41 {PCI_SUBSYSTEM_VENDOR_ID, 2},
42 {PCI_SUBSYSTEM_ID, 2},
43 {0x50, 2},
44 {0x52, 2},
45 {0xa4, 4},
46 {0xa8, 4},
47};
48
49static void host_pci_config_read(int pos, int len, uint32_t *val, Error **errp)
50{
51 int rc, config_fd;
52
53 char *path = g_strdup_printf("/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
54 0, 0, 0, 0, "config");
55
56 config_fd = open(path, O_RDWR);
57 if (config_fd < 0) {
58 error_setg_errno(errp, errno, "Failed to open: %s", path);
59 goto out;
60 }
61
62 if (lseek(config_fd, pos, SEEK_SET) != pos) {
63 error_setg_errno(errp, errno, "Failed to seek: %s", path);
64 goto out_close_fd;
65 }
66
67 do {
68 rc = read(config_fd, (uint8_t *)val, len);
69 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
70 if (rc != len) {
71 error_setg_errno(errp, errno, "Failed to read: %s", path);
72 }
73
74 out_close_fd:
75 close(config_fd);
76 out:
77 g_free(path);
78}
79
80static void igd_pt_i440fx_realize(PCIDevice *pci_dev, Error **errp)
81{
82 ERRP_GUARD();
83 uint32_t val = 0;
84 size_t i;
85 int pos, len;
86
87 for (i = 0; i < ARRAY_SIZE(igd_host_bridge_infos); i++) {
88 pos = igd_host_bridge_infos[i].offset;
89 len = igd_host_bridge_infos[i].len;
90 host_pci_config_read(pos, len, &val, errp);
91 if (*errp) {
92 return;
93 }
94 pci_default_write_config(pci_dev, pos, val, len);
95 }
96}
97
98static void igd_passthrough_i440fx_class_init(ObjectClass *klass, void *data)
99{
100 DeviceClass *dc = DEVICE_CLASS(klass);
101 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
102
103 k->realize = igd_pt_i440fx_realize;
104 dc->desc = "IGD Passthrough Host bridge";
105}
106
107static const TypeInfo igd_passthrough_i440fx_info = {
108 .name = TYPE_IGD_PASSTHROUGH_I440FX_PCI_DEVICE,
109 .parent = TYPE_I440FX_PCI_DEVICE,
110 .instance_size = sizeof(PCII440FXState),
111 .class_init = igd_passthrough_i440fx_class_init,
112};
113
114static void igd_pt_i440fx_register_types(void)
115{
116 type_register_static(&igd_passthrough_i440fx_info);
117}
118
119type_init(igd_pt_i440fx_register_types)
120