]> git.proxmox.com Git - mirror_qemu.git/blame - hw/xen/xen-host-pci-device.c
spapr_pci: Fix interrupt leak in rtas_ibm_change_msi() error path
[mirror_qemu.git] / hw / xen / xen-host-pci-device.c
CommitLineData
396af688
AP
1/*
2 * Copyright (C) 2011 Citrix Ltd.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 *
7 */
8
21cbfe5f 9#include "qemu/osdep.h"
da34e65c 10#include "qapi/error.h"
396af688 11#include "qemu-common.h"
f348b6d1 12#include "qemu/cutils.h"
47b43a1f 13#include "xen-host-pci-device.h"
396af688
AP
14
15#define XEN_HOST_PCI_MAX_EXT_CAP \
16 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
17
18#ifdef XEN_HOST_PCI_DEVICE_DEBUG
19# define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
20#else
21# define XEN_HOST_PCI_LOG(f, a...) (void)0
22#endif
23
24/*
25 * from linux/ioport.h
26 * IO resources have these defined flags.
27 */
28#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
29
30#define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
31#define IORESOURCE_IO 0x00000100
32#define IORESOURCE_MEM 0x00000200
33
34#define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
35#define IORESOURCE_MEM_64 0x00100000
36
599d0c45
C
37static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
38 const char *name, char *buf, ssize_t size)
396af688
AP
39{
40 int rc;
41
42 rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
43 d->domain, d->bus, d->dev, d->func, name);
599d0c45 44 assert(rc >= 0 && rc < size);
396af688
AP
45}
46
47
52a8e968
SW
48/* This size should be enough to read the first 7 lines of a resource file */
49#define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
376ba75f 50static void xen_host_pci_get_resource(XenHostPCIDevice *d, Error **errp)
396af688
AP
51{
52 int i, rc, fd;
53 char path[PATH_MAX];
52a8e968 54 char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
396af688
AP
55 unsigned long long start, end, flags, size;
56 char *endptr, *s;
57 uint8_t type;
58
599d0c45
C
59 xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
60
396af688
AP
61 fd = open(path, O_RDONLY);
62 if (fd == -1) {
376ba75f
C
63 error_setg_file_open(errp, errno, path);
64 return;
396af688
AP
65 }
66
67 do {
376ba75f 68 rc = read(fd, &buf, sizeof(buf) - 1);
396af688 69 if (rc < 0 && errno != EINTR) {
376ba75f 70 error_setg_errno(errp, errno, "read err");
396af688
AP
71 goto out;
72 }
73 } while (rc < 0);
74 buf[rc] = 0;
396af688
AP
75
76 s = buf;
77 for (i = 0; i < PCI_NUM_REGIONS; i++) {
78 type = 0;
79
80 start = strtoll(s, &endptr, 16);
81 if (*endptr != ' ' || s == endptr) {
82 break;
83 }
84 s = endptr + 1;
85 end = strtoll(s, &endptr, 16);
86 if (*endptr != ' ' || s == endptr) {
87 break;
88 }
89 s = endptr + 1;
90 flags = strtoll(s, &endptr, 16);
91 if (*endptr != '\n' || s == endptr) {
92 break;
93 }
94 s = endptr + 1;
95
96 if (start) {
97 size = end - start + 1;
98 } else {
99 size = 0;
100 }
101
102 if (flags & IORESOURCE_IO) {
103 type |= XEN_HOST_PCI_REGION_TYPE_IO;
104 }
105 if (flags & IORESOURCE_MEM) {
106 type |= XEN_HOST_PCI_REGION_TYPE_MEM;
107 }
108 if (flags & IORESOURCE_PREFETCH) {
109 type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
110 }
111 if (flags & IORESOURCE_MEM_64) {
112 type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
113 }
114
115 if (i < PCI_ROM_SLOT) {
116 d->io_regions[i].base_addr = start;
117 d->io_regions[i].size = size;
118 d->io_regions[i].type = type;
119 d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
120 } else {
121 d->rom.base_addr = start;
122 d->rom.size = size;
123 d->rom.type = type;
124 d->rom.bus_flags = flags & IORESOURCE_BITS;
125 }
126 }
376ba75f 127
396af688 128 if (i != PCI_NUM_REGIONS) {
376ba75f 129 error_setg(errp, "Invalid format or input too short: %s", buf);
396af688
AP
130 }
131
132out:
133 close(fd);
396af688
AP
134}
135
136/* This size should be enough to read a long from a file */
137#define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
376ba75f
C
138static void xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
139 unsigned int *pvalue, int base, Error **errp)
396af688
AP
140{
141 char path[PATH_MAX];
142 char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
143 int fd, rc;
144 unsigned long value;
f524bc3b 145 const char *endptr;
396af688 146
599d0c45
C
147 xen_host_pci_sysfs_path(d, name, path, sizeof(path));
148
396af688
AP
149 fd = open(path, O_RDONLY);
150 if (fd == -1) {
376ba75f
C
151 error_setg_file_open(errp, errno, path);
152 return;
396af688 153 }
376ba75f 154
396af688 155 do {
376ba75f 156 rc = read(fd, &buf, sizeof(buf) - 1);
396af688 157 if (rc < 0 && errno != EINTR) {
376ba75f 158 error_setg_errno(errp, errno, "read err");
396af688
AP
159 goto out;
160 }
161 } while (rc < 0);
376ba75f 162
396af688 163 buf[rc] = 0;
f524bc3b
C
164 rc = qemu_strtoul(buf, &endptr, base, &value);
165 if (!rc) {
166 assert(value <= UINT_MAX);
396af688 167 *pvalue = value;
376ba75f
C
168 } else {
169 error_setg_errno(errp, -rc, "failed to parse value '%s'", buf);
396af688 170 }
376ba75f 171
396af688
AP
172out:
173 close(fd);
396af688
AP
174}
175
376ba75f
C
176static inline void xen_host_pci_get_hex_value(XenHostPCIDevice *d,
177 const char *name,
178 unsigned int *pvalue,
179 Error **errp)
396af688 180{
376ba75f 181 xen_host_pci_get_value(d, name, pvalue, 16, errp);
396af688
AP
182}
183
376ba75f
C
184static inline void xen_host_pci_get_dec_value(XenHostPCIDevice *d,
185 const char *name,
186 unsigned int *pvalue,
187 Error **errp)
396af688 188{
376ba75f 189 xen_host_pci_get_value(d, name, pvalue, 10, errp);
396af688
AP
190}
191
192static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
193{
194 char path[PATH_MAX];
195 struct stat buf;
196
599d0c45
C
197 xen_host_pci_sysfs_path(d, "physfn", path, sizeof(path));
198
396af688
AP
199 return !stat(path, &buf);
200}
201
376ba75f 202static void xen_host_pci_config_open(XenHostPCIDevice *d, Error **errp)
396af688
AP
203{
204 char path[PATH_MAX];
396af688 205
599d0c45
C
206 xen_host_pci_sysfs_path(d, "config", path, sizeof(path));
207
396af688 208 d->config_fd = open(path, O_RDWR);
376ba75f
C
209 if (d->config_fd == -1) {
210 error_setg_file_open(errp, errno, path);
396af688 211 }
396af688
AP
212}
213
214static int xen_host_pci_config_read(XenHostPCIDevice *d,
215 int pos, void *buf, int len)
216{
217 int rc;
218
219 do {
220 rc = pread(d->config_fd, buf, len, pos);
221 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
222 if (rc != len) {
223 return -errno;
224 }
225 return 0;
226}
227
228static int xen_host_pci_config_write(XenHostPCIDevice *d,
229 int pos, const void *buf, int len)
230{
231 int rc;
232
233 do {
234 rc = pwrite(d->config_fd, buf, len, pos);
235 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
236 if (rc != len) {
237 return -errno;
238 }
239 return 0;
240}
241
242
243int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
244{
245 uint8_t buf;
246 int rc = xen_host_pci_config_read(d, pos, &buf, 1);
247 if (!rc) {
248 *p = buf;
249 }
250 return rc;
251}
252
253int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
254{
255 uint16_t buf;
256 int rc = xen_host_pci_config_read(d, pos, &buf, 2);
257 if (!rc) {
258 *p = le16_to_cpu(buf);
259 }
260 return rc;
261}
262
263int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
264{
265 uint32_t buf;
266 int rc = xen_host_pci_config_read(d, pos, &buf, 4);
267 if (!rc) {
268 *p = le32_to_cpu(buf);
269 }
270 return rc;
271}
272
273int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
274{
275 return xen_host_pci_config_read(d, pos, buf, len);
276}
277
278int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
279{
280 return xen_host_pci_config_write(d, pos, &data, 1);
281}
282
283int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
284{
285 data = cpu_to_le16(data);
286 return xen_host_pci_config_write(d, pos, &data, 2);
287}
288
289int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
290{
291 data = cpu_to_le32(data);
292 return xen_host_pci_config_write(d, pos, &data, 4);
293}
294
295int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
296{
297 return xen_host_pci_config_write(d, pos, buf, len);
298}
299
300int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
301{
302 uint32_t header = 0;
303 int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
304 int pos = PCI_CONFIG_SPACE_SIZE;
305
306 do {
307 if (xen_host_pci_get_long(d, pos, &header)) {
308 break;
309 }
310 /*
311 * If we have no capabilities, this is indicated by cap ID,
312 * cap version and next pointer all being 0.
313 */
314 if (header == 0) {
315 break;
316 }
317
318 if (PCI_EXT_CAP_ID(header) == cap) {
319 return pos;
320 }
321
322 pos = PCI_EXT_CAP_NEXT(header);
323 if (pos < PCI_CONFIG_SPACE_SIZE) {
324 break;
325 }
326
327 max_cap--;
328 } while (max_cap > 0);
329
330 return -1;
331}
332
376ba75f
C
333void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
334 uint8_t bus, uint8_t dev, uint8_t func,
335 Error **errp)
396af688
AP
336{
337 unsigned int v;
376ba75f 338 Error *err = NULL;
396af688
AP
339
340 d->config_fd = -1;
341 d->domain = domain;
342 d->bus = bus;
343 d->dev = dev;
344 d->func = func;
345
376ba75f
C
346 xen_host_pci_config_open(d, &err);
347 if (err) {
396af688
AP
348 goto error;
349 }
376ba75f
C
350
351 xen_host_pci_get_resource(d, &err);
352 if (err) {
396af688
AP
353 goto error;
354 }
376ba75f
C
355
356 xen_host_pci_get_hex_value(d, "vendor", &v, &err);
357 if (err) {
396af688
AP
358 goto error;
359 }
360 d->vendor_id = v;
376ba75f
C
361
362 xen_host_pci_get_hex_value(d, "device", &v, &err);
363 if (err) {
396af688
AP
364 goto error;
365 }
366 d->device_id = v;
376ba75f
C
367
368 xen_host_pci_get_dec_value(d, "irq", &v, &err);
369 if (err) {
396af688
AP
370 goto error;
371 }
372 d->irq = v;
376ba75f
C
373
374 xen_host_pci_get_hex_value(d, "class", &v, &err);
375 if (err) {
79814179
TC
376 goto error;
377 }
378 d->class_code = v;
376ba75f 379
396af688
AP
380 d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
381
376ba75f
C
382 return;
383
396af688 384error:
376ba75f
C
385 error_propagate(errp, err);
386
396af688
AP
387 if (d->config_fd >= 0) {
388 close(d->config_fd);
389 d->config_fd = -1;
390 }
396af688
AP
391}
392
bce33948
KRW
393bool xen_host_pci_device_closed(XenHostPCIDevice *d)
394{
395 return d->config_fd == -1;
396}
397
396af688
AP
398void xen_host_pci_device_put(XenHostPCIDevice *d)
399{
400 if (d->config_fd >= 0) {
401 close(d->config_fd);
402 d->config_fd = -1;
403 }
404}