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/osdep.h"
10 #include "qapi/error.h"
11 #include "qemu/cutils.h"
12 #include "xen-host-pci-device.h"
14 #define XEN_HOST_PCI_MAX_EXT_CAP \
15 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
17 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
18 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
20 # define XEN_HOST_PCI_LOG(f, a...) (void)0
25 * IO resources have these defined flags.
27 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
29 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
30 #define IORESOURCE_IO 0x00000100
31 #define IORESOURCE_MEM 0x00000200
33 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
34 #define IORESOURCE_MEM_64 0x00100000
36 static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
37 const char *name, char *buf, ssize_t size)
41 rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
42 d->domain, d->bus, d->dev, d->func, name);
43 assert(rc >= 0 && rc < size);
47 /* This size should be enough to read the first 7 lines of a resource file */
48 #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
49 static void xen_host_pci_get_resource(XenHostPCIDevice *d, Error **errp)
53 char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
54 unsigned long long start, end, flags, size;
58 xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
60 fd = open(path, O_RDONLY);
62 error_setg_file_open(errp, errno, path);
67 rc = read(fd, &buf, sizeof(buf) - 1);
68 if (rc < 0 && errno != EINTR) {
69 error_setg_errno(errp, errno, "read err");
76 for (i = 0; i < PCI_NUM_REGIONS; i++) {
79 start = strtoll(s, &endptr, 16);
80 if (*endptr != ' ' || s == endptr) {
84 end = strtoll(s, &endptr, 16);
85 if (*endptr != ' ' || s == endptr) {
89 flags = strtoll(s, &endptr, 16);
90 if (*endptr != '\n' || s == endptr) {
96 size = end - start + 1;
101 if (flags & IORESOURCE_IO) {
102 type |= XEN_HOST_PCI_REGION_TYPE_IO;
104 if (flags & IORESOURCE_MEM) {
105 type |= XEN_HOST_PCI_REGION_TYPE_MEM;
107 if (flags & IORESOURCE_PREFETCH) {
108 type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
110 if (flags & IORESOURCE_MEM_64) {
111 type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
114 if (i < PCI_ROM_SLOT) {
115 d->io_regions[i].base_addr = start;
116 d->io_regions[i].size = size;
117 d->io_regions[i].type = type;
118 d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
120 d->rom.base_addr = start;
123 d->rom.bus_flags = flags & IORESOURCE_BITS;
127 if (i != PCI_NUM_REGIONS) {
128 error_setg(errp, "Invalid format or input too short: %s", buf);
135 /* This size should be enough to read a long from a file */
136 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
137 static void xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
138 unsigned int *pvalue, int base, Error **errp)
141 char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
146 xen_host_pci_sysfs_path(d, name, path, sizeof(path));
148 fd = open(path, O_RDONLY);
150 error_setg_file_open(errp, errno, path);
155 rc = read(fd, &buf, sizeof(buf) - 1);
156 if (rc < 0 && errno != EINTR) {
157 error_setg_errno(errp, errno, "read err");
163 rc = qemu_strtoul(buf, &endptr, base, &value);
165 assert(value <= UINT_MAX);
168 error_setg_errno(errp, -rc, "failed to parse value '%s'", buf);
175 static inline void xen_host_pci_get_hex_value(XenHostPCIDevice *d,
177 unsigned int *pvalue,
180 xen_host_pci_get_value(d, name, pvalue, 16, errp);
183 static inline void xen_host_pci_get_dec_value(XenHostPCIDevice *d,
185 unsigned int *pvalue,
188 xen_host_pci_get_value(d, name, pvalue, 10, errp);
191 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
196 xen_host_pci_sysfs_path(d, "physfn", path, sizeof(path));
198 return !stat(path, &buf);
201 static void xen_host_pci_config_open(XenHostPCIDevice *d, Error **errp)
205 xen_host_pci_sysfs_path(d, "config", path, sizeof(path));
207 d->config_fd = open(path, O_RDWR);
208 if (d->config_fd == -1) {
209 error_setg_file_open(errp, errno, path);
213 static int xen_host_pci_config_read(XenHostPCIDevice *d,
214 int pos, void *buf, int len)
219 rc = pread(d->config_fd, buf, len, pos);
220 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
227 static int xen_host_pci_config_write(XenHostPCIDevice *d,
228 int pos, const void *buf, int len)
233 rc = pwrite(d->config_fd, buf, len, pos);
234 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
242 int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
245 int rc = xen_host_pci_config_read(d, pos, &buf, 1);
252 int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
255 int rc = xen_host_pci_config_read(d, pos, &buf, 2);
257 *p = le16_to_cpu(buf);
262 int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
265 int rc = xen_host_pci_config_read(d, pos, &buf, 4);
267 *p = le32_to_cpu(buf);
272 int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
274 return xen_host_pci_config_read(d, pos, buf, len);
277 int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
279 return xen_host_pci_config_write(d, pos, &data, 1);
282 int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
284 data = cpu_to_le16(data);
285 return xen_host_pci_config_write(d, pos, &data, 2);
288 int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
290 data = cpu_to_le32(data);
291 return xen_host_pci_config_write(d, pos, &data, 4);
294 int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
296 return xen_host_pci_config_write(d, pos, buf, len);
299 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
302 int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
303 int pos = PCI_CONFIG_SPACE_SIZE;
306 if (xen_host_pci_get_long(d, pos, &header)) {
310 * If we have no capabilities, this is indicated by cap ID,
311 * cap version and next pointer all being 0.
317 if (PCI_EXT_CAP_ID(header) == cap) {
321 pos = PCI_EXT_CAP_NEXT(header);
322 if (pos < PCI_CONFIG_SPACE_SIZE) {
327 } while (max_cap > 0);
332 void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
333 uint8_t bus, uint8_t dev, uint8_t func,
345 xen_host_pci_config_open(d, errp);
350 xen_host_pci_get_resource(d, errp);
355 xen_host_pci_get_hex_value(d, "vendor", &v, errp);
361 xen_host_pci_get_hex_value(d, "device", &v, errp);
367 xen_host_pci_get_dec_value(d, "irq", &v, errp);
373 xen_host_pci_get_hex_value(d, "class", &v, errp);
379 d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
385 if (d->config_fd >= 0) {
391 bool xen_host_pci_device_closed(XenHostPCIDevice *d)
393 return d->config_fd == -1;
396 void xen_host_pci_device_put(XenHostPCIDevice *d)
398 if (d->config_fd >= 0) {