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 "hw/xen/xen-legacy-backend.h"
13 #include "hw/xen/xen-bus-helper.h"
14 #include "xen-host-pci-device.h"
16 #define XEN_HOST_PCI_MAX_EXT_CAP \
17 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
19 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
20 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
22 # define XEN_HOST_PCI_LOG(f, a...) (void)0
27 * IO resources have these defined flags.
29 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
31 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
32 #define IORESOURCE_IO 0x00000100
33 #define IORESOURCE_MEM 0x00000200
35 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
36 #define IORESOURCE_MEM_64 0x00100000
39 * Non-passthrough (dom0) accesses are local PCI devices and use the given BDF
40 * Passthough (stubdom) accesses are through PV frontend PCI device. Those
41 * either have a BDF identical to the backend's BDF (xen-backend.passthrough=1)
42 * or a local virtual BDF (xen-backend.passthrough=0)
44 * We are always given the backend's BDF and need to lookup the appropriate
45 * local BDF for sysfs access.
47 static void xen_host_pci_fill_local_addr(XenHostPCIDevice
*d
, Error
**errp
)
49 unsigned int num_devs
, len
, i
;
50 unsigned int domain
, bus
, dev
, func
;
54 be_path
= qemu_xen_xs_read(xenstore
, 0, "device/pci/0/backend", &len
);
56 error_setg(errp
, "Failed to read device/pci/0/backend");
60 if (xs_node_scanf(xenstore
, 0, be_path
, "num_devs", NULL
,
61 "%d", &num_devs
) != 1) {
62 error_setg(errp
, "Failed to read or parse %s/num_devs", be_path
);
66 for (i
= 0; i
< num_devs
; i
++) {
67 snprintf(path
, sizeof(path
), "dev-%d", i
);
68 if (xs_node_scanf(xenstore
, 0, be_path
, path
, NULL
,
69 "%x:%x:%x.%x", &domain
, &bus
, &dev
, &func
) != 4) {
70 error_setg(errp
, "Failed to read or parse %s/%s", be_path
, path
);
73 if (domain
!= d
->domain
||
78 snprintf(path
, sizeof(path
), "vdev-%d", i
);
79 if (xs_node_scanf(xenstore
, 0, be_path
, path
, NULL
,
80 "%x:%x:%x.%x", &domain
, &bus
, &dev
, &func
) != 4) {
81 error_setg(errp
, "Failed to read or parse %s/%s", be_path
, path
);
84 d
->local_domain
= domain
;
90 error_setg(errp
, "Failed to find PCI device %x:%x:%x.%x in xenstore",
91 d
->domain
, d
->bus
, d
->dev
, d
->func
);
97 static void xen_host_pci_sysfs_path(const XenHostPCIDevice
*d
,
98 const char *name
, char *buf
, ssize_t size
)
102 rc
= snprintf(buf
, size
, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
103 d
->local_domain
, d
->local_bus
, d
->local_dev
, d
->local_func
,
105 assert(rc
>= 0 && rc
< size
);
109 /* This size should be enough to read the first 7 lines of a resource file */
110 #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
111 static void xen_host_pci_get_resource(XenHostPCIDevice
*d
, Error
**errp
)
115 char buf
[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE
];
116 unsigned long long start
, end
, flags
, size
;
120 xen_host_pci_sysfs_path(d
, "resource", path
, sizeof(path
));
122 fd
= open(path
, O_RDONLY
);
124 error_setg_file_open(errp
, errno
, path
);
129 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
130 if (rc
< 0 && errno
!= EINTR
) {
131 error_setg_errno(errp
, errno
, "read err");
138 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
141 start
= strtoll(s
, &endptr
, 16);
142 if (*endptr
!= ' ' || s
== endptr
) {
146 end
= strtoll(s
, &endptr
, 16);
147 if (*endptr
!= ' ' || s
== endptr
) {
151 flags
= strtoll(s
, &endptr
, 16);
152 if (*endptr
!= '\n' || s
== endptr
) {
158 size
= end
- start
+ 1;
163 if (flags
& IORESOURCE_IO
) {
164 type
|= XEN_HOST_PCI_REGION_TYPE_IO
;
166 if (flags
& IORESOURCE_MEM
) {
167 type
|= XEN_HOST_PCI_REGION_TYPE_MEM
;
169 if (flags
& IORESOURCE_PREFETCH
) {
170 type
|= XEN_HOST_PCI_REGION_TYPE_PREFETCH
;
172 if (flags
& IORESOURCE_MEM_64
) {
173 type
|= XEN_HOST_PCI_REGION_TYPE_MEM_64
;
176 if (i
< PCI_ROM_SLOT
) {
177 d
->io_regions
[i
].base_addr
= start
;
178 d
->io_regions
[i
].size
= size
;
179 d
->io_regions
[i
].type
= type
;
180 d
->io_regions
[i
].bus_flags
= flags
& IORESOURCE_BITS
;
182 d
->rom
.base_addr
= start
;
185 d
->rom
.bus_flags
= flags
& IORESOURCE_BITS
;
189 if (i
!= PCI_NUM_REGIONS
) {
190 error_setg(errp
, "Invalid format or input too short: %s", buf
);
197 /* This size should be enough to read a long from a file */
198 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
199 static void xen_host_pci_get_value(XenHostPCIDevice
*d
, const char *name
,
200 unsigned int *pvalue
, int base
, Error
**errp
)
203 char buf
[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE
];
208 xen_host_pci_sysfs_path(d
, name
, path
, sizeof(path
));
210 fd
= open(path
, O_RDONLY
);
212 error_setg_file_open(errp
, errno
, path
);
217 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
218 if (rc
< 0 && errno
!= EINTR
) {
219 error_setg_errno(errp
, errno
, "read err");
225 rc
= qemu_strtoul(buf
, &endptr
, base
, &value
);
227 assert(value
<= UINT_MAX
);
230 error_setg_errno(errp
, -rc
, "failed to parse value '%s'", buf
);
237 static inline void xen_host_pci_get_hex_value(XenHostPCIDevice
*d
,
239 unsigned int *pvalue
,
242 xen_host_pci_get_value(d
, name
, pvalue
, 16, errp
);
245 static inline void xen_host_pci_get_dec_value(XenHostPCIDevice
*d
,
247 unsigned int *pvalue
,
250 xen_host_pci_get_value(d
, name
, pvalue
, 10, errp
);
253 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice
*d
)
258 xen_host_pci_sysfs_path(d
, "physfn", path
, sizeof(path
));
260 return !stat(path
, &buf
);
263 static void xen_host_pci_config_open(XenHostPCIDevice
*d
, Error
**errp
)
267 xen_host_pci_sysfs_path(d
, "config", path
, sizeof(path
));
269 d
->config_fd
= open(path
, O_RDWR
);
270 if (d
->config_fd
== -1) {
271 error_setg_file_open(errp
, errno
, path
);
275 static int xen_host_pci_config_read(XenHostPCIDevice
*d
,
276 int pos
, void *buf
, int len
)
281 rc
= pread(d
->config_fd
, buf
, len
, pos
);
282 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
289 static int xen_host_pci_config_write(XenHostPCIDevice
*d
,
290 int pos
, const void *buf
, int len
)
295 rc
= pwrite(d
->config_fd
, buf
, len
, pos
);
296 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
304 int xen_host_pci_get_byte(XenHostPCIDevice
*d
, int pos
, uint8_t *p
)
307 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 1);
314 int xen_host_pci_get_word(XenHostPCIDevice
*d
, int pos
, uint16_t *p
)
317 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 2);
319 *p
= le16_to_cpu(buf
);
324 int xen_host_pci_get_long(XenHostPCIDevice
*d
, int pos
, uint32_t *p
)
327 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 4);
329 *p
= le32_to_cpu(buf
);
334 int xen_host_pci_get_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
336 return xen_host_pci_config_read(d
, pos
, buf
, len
);
339 int xen_host_pci_set_byte(XenHostPCIDevice
*d
, int pos
, uint8_t data
)
341 return xen_host_pci_config_write(d
, pos
, &data
, 1);
344 int xen_host_pci_set_word(XenHostPCIDevice
*d
, int pos
, uint16_t data
)
346 data
= cpu_to_le16(data
);
347 return xen_host_pci_config_write(d
, pos
, &data
, 2);
350 int xen_host_pci_set_long(XenHostPCIDevice
*d
, int pos
, uint32_t data
)
352 data
= cpu_to_le32(data
);
353 return xen_host_pci_config_write(d
, pos
, &data
, 4);
356 int xen_host_pci_set_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
358 return xen_host_pci_config_write(d
, pos
, buf
, len
);
361 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice
*d
, uint32_t cap
)
364 int max_cap
= XEN_HOST_PCI_MAX_EXT_CAP
;
365 int pos
= PCI_CONFIG_SPACE_SIZE
;
368 if (xen_host_pci_get_long(d
, pos
, &header
)) {
372 * If we have no capabilities, this is indicated by cap ID,
373 * cap version and next pointer all being 0.
379 if (PCI_EXT_CAP_ID(header
) == cap
) {
383 pos
= PCI_EXT_CAP_NEXT(header
);
384 if (pos
< PCI_CONFIG_SPACE_SIZE
) {
389 } while (max_cap
> 0);
394 void xen_host_pci_device_get(XenHostPCIDevice
*d
, uint16_t domain
,
395 uint8_t bus
, uint8_t dev
, uint8_t func
,
407 if (xen_is_stubdomain
) {
408 xen_host_pci_fill_local_addr(d
, errp
);
413 d
->local_domain
= d
->domain
;
414 d
->local_bus
= d
->bus
;
415 d
->local_dev
= d
->dev
;
416 d
->local_func
= d
->func
;
419 xen_host_pci_config_open(d
, errp
);
424 xen_host_pci_get_resource(d
, errp
);
429 xen_host_pci_get_hex_value(d
, "vendor", &v
, errp
);
435 xen_host_pci_get_hex_value(d
, "device", &v
, errp
);
441 xen_host_pci_get_dec_value(d
, "irq", &v
, errp
);
447 xen_host_pci_get_hex_value(d
, "class", &v
, errp
);
453 d
->is_virtfn
= xen_host_pci_dev_is_virtfn(d
);
459 if (d
->config_fd
>= 0) {
465 bool xen_host_pci_device_closed(XenHostPCIDevice
*d
)
467 return d
->config_fd
== -1;
470 void xen_host_pci_device_put(XenHostPCIDevice
*d
)
472 if (d
->config_fd
>= 0) {