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-common.h"
12 #include "qemu/cutils.h"
13 #include "xen-host-pci-device.h"
15 #define XEN_HOST_PCI_MAX_EXT_CAP \
16 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
18 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
19 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
21 # define XEN_HOST_PCI_LOG(f, a...) (void)0
26 * IO resources have these defined flags.
28 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
30 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
31 #define IORESOURCE_IO 0x00000100
32 #define IORESOURCE_MEM 0x00000200
34 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
35 #define IORESOURCE_MEM_64 0x00100000
37 static void xen_host_pci_sysfs_path(const XenHostPCIDevice
*d
,
38 const char *name
, char *buf
, ssize_t size
)
42 rc
= snprintf(buf
, size
, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
43 d
->domain
, d
->bus
, d
->dev
, d
->func
, name
);
44 assert(rc
>= 0 && rc
< size
);
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
50 static void xen_host_pci_get_resource(XenHostPCIDevice
*d
, Error
**errp
)
54 char buf
[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE
];
55 unsigned long long start
, end
, flags
, size
;
59 xen_host_pci_sysfs_path(d
, "resource", path
, sizeof(path
));
61 fd
= open(path
, O_RDONLY
);
63 error_setg_file_open(errp
, errno
, path
);
68 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
69 if (rc
< 0 && errno
!= EINTR
) {
70 error_setg_errno(errp
, errno
, "read err");
77 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
80 start
= strtoll(s
, &endptr
, 16);
81 if (*endptr
!= ' ' || s
== endptr
) {
85 end
= strtoll(s
, &endptr
, 16);
86 if (*endptr
!= ' ' || s
== endptr
) {
90 flags
= strtoll(s
, &endptr
, 16);
91 if (*endptr
!= '\n' || s
== endptr
) {
97 size
= end
- start
+ 1;
102 if (flags
& IORESOURCE_IO
) {
103 type
|= XEN_HOST_PCI_REGION_TYPE_IO
;
105 if (flags
& IORESOURCE_MEM
) {
106 type
|= XEN_HOST_PCI_REGION_TYPE_MEM
;
108 if (flags
& IORESOURCE_PREFETCH
) {
109 type
|= XEN_HOST_PCI_REGION_TYPE_PREFETCH
;
111 if (flags
& IORESOURCE_MEM_64
) {
112 type
|= XEN_HOST_PCI_REGION_TYPE_MEM_64
;
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
;
121 d
->rom
.base_addr
= start
;
124 d
->rom
.bus_flags
= flags
& IORESOURCE_BITS
;
128 if (i
!= PCI_NUM_REGIONS
) {
129 error_setg(errp
, "Invalid format or input too short: %s", buf
);
136 /* This size should be enough to read a long from a file */
137 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
138 static void xen_host_pci_get_value(XenHostPCIDevice
*d
, const char *name
,
139 unsigned int *pvalue
, int base
, Error
**errp
)
142 char buf
[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE
];
147 xen_host_pci_sysfs_path(d
, name
, path
, sizeof(path
));
149 fd
= open(path
, O_RDONLY
);
151 error_setg_file_open(errp
, errno
, path
);
156 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
157 if (rc
< 0 && errno
!= EINTR
) {
158 error_setg_errno(errp
, errno
, "read err");
164 rc
= qemu_strtoul(buf
, &endptr
, base
, &value
);
166 assert(value
<= UINT_MAX
);
169 error_setg_errno(errp
, -rc
, "failed to parse value '%s'", buf
);
176 static inline void xen_host_pci_get_hex_value(XenHostPCIDevice
*d
,
178 unsigned int *pvalue
,
181 xen_host_pci_get_value(d
, name
, pvalue
, 16, errp
);
184 static inline void xen_host_pci_get_dec_value(XenHostPCIDevice
*d
,
186 unsigned int *pvalue
,
189 xen_host_pci_get_value(d
, name
, pvalue
, 10, errp
);
192 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice
*d
)
197 xen_host_pci_sysfs_path(d
, "physfn", path
, sizeof(path
));
199 return !stat(path
, &buf
);
202 static void xen_host_pci_config_open(XenHostPCIDevice
*d
, Error
**errp
)
206 xen_host_pci_sysfs_path(d
, "config", path
, sizeof(path
));
208 d
->config_fd
= open(path
, O_RDWR
);
209 if (d
->config_fd
== -1) {
210 error_setg_file_open(errp
, errno
, path
);
214 static int xen_host_pci_config_read(XenHostPCIDevice
*d
,
215 int pos
, void *buf
, int len
)
220 rc
= pread(d
->config_fd
, buf
, len
, pos
);
221 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
228 static int xen_host_pci_config_write(XenHostPCIDevice
*d
,
229 int pos
, const void *buf
, int len
)
234 rc
= pwrite(d
->config_fd
, buf
, len
, pos
);
235 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
243 int xen_host_pci_get_byte(XenHostPCIDevice
*d
, int pos
, uint8_t *p
)
246 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 1);
253 int xen_host_pci_get_word(XenHostPCIDevice
*d
, int pos
, uint16_t *p
)
256 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 2);
258 *p
= le16_to_cpu(buf
);
263 int xen_host_pci_get_long(XenHostPCIDevice
*d
, int pos
, uint32_t *p
)
266 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 4);
268 *p
= le32_to_cpu(buf
);
273 int xen_host_pci_get_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
275 return xen_host_pci_config_read(d
, pos
, buf
, len
);
278 int xen_host_pci_set_byte(XenHostPCIDevice
*d
, int pos
, uint8_t data
)
280 return xen_host_pci_config_write(d
, pos
, &data
, 1);
283 int xen_host_pci_set_word(XenHostPCIDevice
*d
, int pos
, uint16_t data
)
285 data
= cpu_to_le16(data
);
286 return xen_host_pci_config_write(d
, pos
, &data
, 2);
289 int xen_host_pci_set_long(XenHostPCIDevice
*d
, int pos
, uint32_t data
)
291 data
= cpu_to_le32(data
);
292 return xen_host_pci_config_write(d
, pos
, &data
, 4);
295 int xen_host_pci_set_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
297 return xen_host_pci_config_write(d
, pos
, buf
, len
);
300 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice
*d
, uint32_t cap
)
303 int max_cap
= XEN_HOST_PCI_MAX_EXT_CAP
;
304 int pos
= PCI_CONFIG_SPACE_SIZE
;
307 if (xen_host_pci_get_long(d
, pos
, &header
)) {
311 * If we have no capabilities, this is indicated by cap ID,
312 * cap version and next pointer all being 0.
318 if (PCI_EXT_CAP_ID(header
) == cap
) {
322 pos
= PCI_EXT_CAP_NEXT(header
);
323 if (pos
< PCI_CONFIG_SPACE_SIZE
) {
328 } while (max_cap
> 0);
333 void xen_host_pci_device_get(XenHostPCIDevice
*d
, uint16_t domain
,
334 uint8_t bus
, uint8_t dev
, uint8_t func
,
346 xen_host_pci_config_open(d
, &err
);
351 xen_host_pci_get_resource(d
, &err
);
356 xen_host_pci_get_hex_value(d
, "vendor", &v
, &err
);
362 xen_host_pci_get_hex_value(d
, "device", &v
, &err
);
368 xen_host_pci_get_dec_value(d
, "irq", &v
, &err
);
374 xen_host_pci_get_hex_value(d
, "class", &v
, &err
);
380 d
->is_virtfn
= xen_host_pci_dev_is_virtfn(d
);
385 error_propagate(errp
, err
);
387 if (d
->config_fd
>= 0) {
393 bool xen_host_pci_device_closed(XenHostPCIDevice
*d
)
395 return d
->config_fd
== -1;
398 void xen_host_pci_device_put(XenHostPCIDevice
*d
)
400 if (d
->config_fd
>= 0) {