2 * Copyright (C) 2011 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
9 #include "qemu-common.h"
10 #include "xen-host-pci-device.h"
12 #define XEN_HOST_PCI_MAX_EXT_CAP \
13 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
15 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
16 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
18 # define XEN_HOST_PCI_LOG(f, a...) (void)0
23 * IO resources have these defined flags.
25 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
27 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
28 #define IORESOURCE_IO 0x00000100
29 #define IORESOURCE_MEM 0x00000200
31 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
32 #define IORESOURCE_MEM_64 0x00100000
34 static int xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
35 const char *name, char *buf, ssize_t size)
39 rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
40 d->domain, d->bus, d->dev, d->func, name);
42 if (rc >= size || rc < 0) {
43 /* The ouput is truncated or an other error is encountered */
50 /* This size should be enough to read the first 7 lines of a resource file */
51 #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
52 static int xen_host_pci_get_resource(XenHostPCIDevice *d)
56 char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
57 unsigned long long start, end, flags, size;
61 rc = xen_host_pci_sysfs_path(d, "resource", path, sizeof (path));
65 fd = open(path, O_RDONLY);
67 XEN_HOST_PCI_LOG("Error: Can't open %s: %s\n", path, strerror(errno));
72 rc = read(fd, &buf, sizeof (buf) - 1);
73 if (rc < 0 && errno != EINTR) {
82 for (i = 0; i < PCI_NUM_REGIONS; i++) {
85 start = strtoll(s, &endptr, 16);
86 if (*endptr != ' ' || s == endptr) {
90 end = strtoll(s, &endptr, 16);
91 if (*endptr != ' ' || s == endptr) {
95 flags = strtoll(s, &endptr, 16);
96 if (*endptr != '\n' || s == endptr) {
102 size = end - start + 1;
107 if (flags & IORESOURCE_IO) {
108 type |= XEN_HOST_PCI_REGION_TYPE_IO;
110 if (flags & IORESOURCE_MEM) {
111 type |= XEN_HOST_PCI_REGION_TYPE_MEM;
113 if (flags & IORESOURCE_PREFETCH) {
114 type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
116 if (flags & IORESOURCE_MEM_64) {
117 type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
120 if (i < PCI_ROM_SLOT) {
121 d->io_regions[i].base_addr = start;
122 d->io_regions[i].size = size;
123 d->io_regions[i].type = type;
124 d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
126 d->rom.base_addr = start;
129 d->rom.bus_flags = flags & IORESOURCE_BITS;
132 if (i != PCI_NUM_REGIONS) {
133 /* Invalid format or input to short */
142 /* This size should be enough to read a long from a file */
143 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
144 static int xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
145 unsigned int *pvalue, int base)
148 char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
153 rc = xen_host_pci_sysfs_path(d, name, path, sizeof (path));
157 fd = open(path, O_RDONLY);
159 XEN_HOST_PCI_LOG("Error: Can't open %s: %s\n", path, strerror(errno));
163 rc = read(fd, &buf, sizeof (buf) - 1);
164 if (rc < 0 && errno != EINTR) {
170 value = strtol(buf, &endptr, base);
171 if (endptr == buf || *endptr != '\n') {
173 } else if ((value == LONG_MIN || value == LONG_MAX) && errno == ERANGE) {
184 static inline int xen_host_pci_get_hex_value(XenHostPCIDevice *d,
186 unsigned int *pvalue)
188 return xen_host_pci_get_value(d, name, pvalue, 16);
191 static inline int xen_host_pci_get_dec_value(XenHostPCIDevice *d,
193 unsigned int *pvalue)
195 return xen_host_pci_get_value(d, name, pvalue, 10);
198 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
203 if (xen_host_pci_sysfs_path(d, "physfn", path, sizeof (path))) {
206 return !stat(path, &buf);
209 static int xen_host_pci_config_open(XenHostPCIDevice *d)
214 rc = xen_host_pci_sysfs_path(d, "config", path, sizeof (path));
218 d->config_fd = open(path, O_RDWR);
219 if (d->config_fd < 0) {
225 static int xen_host_pci_config_read(XenHostPCIDevice *d,
226 int pos, void *buf, int len)
231 rc = pread(d->config_fd, buf, len, pos);
232 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
239 static int xen_host_pci_config_write(XenHostPCIDevice *d,
240 int pos, const void *buf, int len)
245 rc = pwrite(d->config_fd, buf, len, pos);
246 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
254 int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
257 int rc = xen_host_pci_config_read(d, pos, &buf, 1);
264 int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
267 int rc = xen_host_pci_config_read(d, pos, &buf, 2);
269 *p = le16_to_cpu(buf);
274 int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
277 int rc = xen_host_pci_config_read(d, pos, &buf, 4);
279 *p = le32_to_cpu(buf);
284 int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
286 return xen_host_pci_config_read(d, pos, buf, len);
289 int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
291 return xen_host_pci_config_write(d, pos, &data, 1);
294 int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
296 data = cpu_to_le16(data);
297 return xen_host_pci_config_write(d, pos, &data, 2);
300 int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
302 data = cpu_to_le32(data);
303 return xen_host_pci_config_write(d, pos, &data, 4);
306 int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
308 return xen_host_pci_config_write(d, pos, buf, len);
311 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
314 int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
315 int pos = PCI_CONFIG_SPACE_SIZE;
318 if (xen_host_pci_get_long(d, pos, &header)) {
322 * If we have no capabilities, this is indicated by cap ID,
323 * cap version and next pointer all being 0.
329 if (PCI_EXT_CAP_ID(header) == cap) {
333 pos = PCI_EXT_CAP_NEXT(header);
334 if (pos < PCI_CONFIG_SPACE_SIZE) {
339 } while (max_cap > 0);
344 int xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
345 uint8_t bus, uint8_t dev, uint8_t func)
356 rc = xen_host_pci_config_open(d);
360 rc = xen_host_pci_get_resource(d);
364 rc = xen_host_pci_get_hex_value(d, "vendor", &v);
369 rc = xen_host_pci_get_hex_value(d, "device", &v);
374 rc = xen_host_pci_get_dec_value(d, "irq", &v);
379 d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
383 if (d->config_fd >= 0) {
390 void xen_host_pci_device_put(XenHostPCIDevice *d)
392 if (d->config_fd >= 0) {