Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20210208a' into...
[qemu/ar7.git] / hw / xen / xen-host-pci-device.c
blob8c6e9a1716a2e0a3dd52ed1c531517be65ecec30
1 /*
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.
7 */
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)
19 #else
20 # define XEN_HOST_PCI_LOG(f, a...) (void)0
21 #endif
24 * from linux/ioport.h
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)
39 int rc;
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)
51 int i, rc, fd;
52 char path[PATH_MAX];
53 char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
54 unsigned long long start, end, flags, size;
55 char *endptr, *s;
56 uint8_t type;
58 xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
60 fd = open(path, O_RDONLY);
61 if (fd == -1) {
62 error_setg_file_open(errp, errno, path);
63 return;
66 do {
67 rc = read(fd, &buf, sizeof(buf) - 1);
68 if (rc < 0 && errno != EINTR) {
69 error_setg_errno(errp, errno, "read err");
70 goto out;
72 } while (rc < 0);
73 buf[rc] = 0;
75 s = buf;
76 for (i = 0; i < PCI_NUM_REGIONS; i++) {
77 type = 0;
79 start = strtoll(s, &endptr, 16);
80 if (*endptr != ' ' || s == endptr) {
81 break;
83 s = endptr + 1;
84 end = strtoll(s, &endptr, 16);
85 if (*endptr != ' ' || s == endptr) {
86 break;
88 s = endptr + 1;
89 flags = strtoll(s, &endptr, 16);
90 if (*endptr != '\n' || s == endptr) {
91 break;
93 s = endptr + 1;
95 if (start) {
96 size = end - start + 1;
97 } else {
98 size = 0;
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;
119 } else {
120 d->rom.base_addr = start;
121 d->rom.size = size;
122 d->rom.type = type;
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);
131 out:
132 close(fd);
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)
140 char path[PATH_MAX];
141 char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
142 int fd, rc;
143 unsigned long value;
144 const char *endptr;
146 xen_host_pci_sysfs_path(d, name, path, sizeof(path));
148 fd = open(path, O_RDONLY);
149 if (fd == -1) {
150 error_setg_file_open(errp, errno, path);
151 return;
154 do {
155 rc = read(fd, &buf, sizeof(buf) - 1);
156 if (rc < 0 && errno != EINTR) {
157 error_setg_errno(errp, errno, "read err");
158 goto out;
160 } while (rc < 0);
162 buf[rc] = 0;
163 rc = qemu_strtoul(buf, &endptr, base, &value);
164 if (!rc) {
165 assert(value <= UINT_MAX);
166 *pvalue = value;
167 } else {
168 error_setg_errno(errp, -rc, "failed to parse value '%s'", buf);
171 out:
172 close(fd);
175 static inline void xen_host_pci_get_hex_value(XenHostPCIDevice *d,
176 const char *name,
177 unsigned int *pvalue,
178 Error **errp)
180 xen_host_pci_get_value(d, name, pvalue, 16, errp);
183 static inline void xen_host_pci_get_dec_value(XenHostPCIDevice *d,
184 const char *name,
185 unsigned int *pvalue,
186 Error **errp)
188 xen_host_pci_get_value(d, name, pvalue, 10, errp);
191 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
193 char path[PATH_MAX];
194 struct stat buf;
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)
203 char path[PATH_MAX];
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)
216 int rc;
218 do {
219 rc = pread(d->config_fd, buf, len, pos);
220 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
221 if (rc != len) {
222 return -errno;
224 return 0;
227 static int xen_host_pci_config_write(XenHostPCIDevice *d,
228 int pos, const void *buf, int len)
230 int rc;
232 do {
233 rc = pwrite(d->config_fd, buf, len, pos);
234 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
235 if (rc != len) {
236 return -errno;
238 return 0;
242 int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
244 uint8_t buf;
245 int rc = xen_host_pci_config_read(d, pos, &buf, 1);
246 if (!rc) {
247 *p = buf;
249 return rc;
252 int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
254 uint16_t buf;
255 int rc = xen_host_pci_config_read(d, pos, &buf, 2);
256 if (!rc) {
257 *p = le16_to_cpu(buf);
259 return rc;
262 int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
264 uint32_t buf;
265 int rc = xen_host_pci_config_read(d, pos, &buf, 4);
266 if (!rc) {
267 *p = le32_to_cpu(buf);
269 return rc;
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)
301 uint32_t header = 0;
302 int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
303 int pos = PCI_CONFIG_SPACE_SIZE;
305 do {
306 if (xen_host_pci_get_long(d, pos, &header)) {
307 break;
310 * If we have no capabilities, this is indicated by cap ID,
311 * cap version and next pointer all being 0.
313 if (header == 0) {
314 break;
317 if (PCI_EXT_CAP_ID(header) == cap) {
318 return pos;
321 pos = PCI_EXT_CAP_NEXT(header);
322 if (pos < PCI_CONFIG_SPACE_SIZE) {
323 break;
326 max_cap--;
327 } while (max_cap > 0);
329 return -1;
332 void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
333 uint8_t bus, uint8_t dev, uint8_t func,
334 Error **errp)
336 ERRP_GUARD();
337 unsigned int v;
339 d->config_fd = -1;
340 d->domain = domain;
341 d->bus = bus;
342 d->dev = dev;
343 d->func = func;
345 xen_host_pci_config_open(d, errp);
346 if (*errp) {
347 goto error;
350 xen_host_pci_get_resource(d, errp);
351 if (*errp) {
352 goto error;
355 xen_host_pci_get_hex_value(d, "vendor", &v, errp);
356 if (*errp) {
357 goto error;
359 d->vendor_id = v;
361 xen_host_pci_get_hex_value(d, "device", &v, errp);
362 if (*errp) {
363 goto error;
365 d->device_id = v;
367 xen_host_pci_get_dec_value(d, "irq", &v, errp);
368 if (*errp) {
369 goto error;
371 d->irq = v;
373 xen_host_pci_get_hex_value(d, "class", &v, errp);
374 if (*errp) {
375 goto error;
377 d->class_code = v;
379 d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
381 return;
383 error:
385 if (d->config_fd >= 0) {
386 close(d->config_fd);
387 d->config_fd = -1;
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) {
399 close(d->config_fd);
400 d->config_fd = -1;