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 "qemu-common.h"
11 #include "xen-host-pci-device.h"
13 #define XEN_HOST_PCI_MAX_EXT_CAP \
14 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
16 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
17 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
19 # define XEN_HOST_PCI_LOG(f, a...) (void)0
24 * IO resources have these defined flags.
26 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
28 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
29 #define IORESOURCE_IO 0x00000100
30 #define IORESOURCE_MEM 0x00000200
32 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
33 #define IORESOURCE_MEM_64 0x00100000
35 static void xen_host_pci_sysfs_path(const XenHostPCIDevice
*d
,
36 const char *name
, char *buf
, ssize_t size
)
40 rc
= snprintf(buf
, size
, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
41 d
->domain
, d
->bus
, d
->dev
, d
->func
, name
);
42 assert(rc
>= 0 && rc
< size
);
46 /* This size should be enough to read the first 7 lines of a resource file */
47 #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
48 static void xen_host_pci_get_resource(XenHostPCIDevice
*d
, Error
**errp
)
52 char buf
[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE
];
53 unsigned long long start
, end
, flags
, size
;
57 xen_host_pci_sysfs_path(d
, "resource", path
, sizeof(path
));
59 fd
= open(path
, O_RDONLY
);
61 error_setg_file_open(errp
, errno
, path
);
66 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
67 if (rc
< 0 && errno
!= EINTR
) {
68 error_setg_errno(errp
, errno
, "read err");
75 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
78 start
= strtoll(s
, &endptr
, 16);
79 if (*endptr
!= ' ' || s
== endptr
) {
83 end
= strtoll(s
, &endptr
, 16);
84 if (*endptr
!= ' ' || s
== endptr
) {
88 flags
= strtoll(s
, &endptr
, 16);
89 if (*endptr
!= '\n' || s
== endptr
) {
95 size
= end
- start
+ 1;
100 if (flags
& IORESOURCE_IO
) {
101 type
|= XEN_HOST_PCI_REGION_TYPE_IO
;
103 if (flags
& IORESOURCE_MEM
) {
104 type
|= XEN_HOST_PCI_REGION_TYPE_MEM
;
106 if (flags
& IORESOURCE_PREFETCH
) {
107 type
|= XEN_HOST_PCI_REGION_TYPE_PREFETCH
;
109 if (flags
& IORESOURCE_MEM_64
) {
110 type
|= XEN_HOST_PCI_REGION_TYPE_MEM_64
;
113 if (i
< PCI_ROM_SLOT
) {
114 d
->io_regions
[i
].base_addr
= start
;
115 d
->io_regions
[i
].size
= size
;
116 d
->io_regions
[i
].type
= type
;
117 d
->io_regions
[i
].bus_flags
= flags
& IORESOURCE_BITS
;
119 d
->rom
.base_addr
= start
;
122 d
->rom
.bus_flags
= flags
& IORESOURCE_BITS
;
126 if (i
!= PCI_NUM_REGIONS
) {
127 error_setg(errp
, "Invalid format or input too short: %s", buf
);
134 /* This size should be enough to read a long from a file */
135 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
136 static void xen_host_pci_get_value(XenHostPCIDevice
*d
, const char *name
,
137 unsigned int *pvalue
, int base
, Error
**errp
)
140 char buf
[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE
];
145 xen_host_pci_sysfs_path(d
, name
, path
, sizeof(path
));
147 fd
= open(path
, O_RDONLY
);
149 error_setg_file_open(errp
, errno
, path
);
154 rc
= read(fd
, &buf
, sizeof(buf
) - 1);
155 if (rc
< 0 && errno
!= EINTR
) {
156 error_setg_errno(errp
, errno
, "read err");
162 rc
= qemu_strtoul(buf
, &endptr
, base
, &value
);
164 assert(value
<= UINT_MAX
);
167 error_setg_errno(errp
, -rc
, "failed to parse value '%s'", buf
);
174 static inline void xen_host_pci_get_hex_value(XenHostPCIDevice
*d
,
176 unsigned int *pvalue
,
179 xen_host_pci_get_value(d
, name
, pvalue
, 16, errp
);
182 static inline void xen_host_pci_get_dec_value(XenHostPCIDevice
*d
,
184 unsigned int *pvalue
,
187 xen_host_pci_get_value(d
, name
, pvalue
, 10, errp
);
190 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice
*d
)
195 xen_host_pci_sysfs_path(d
, "physfn", path
, sizeof(path
));
197 return !stat(path
, &buf
);
200 static void xen_host_pci_config_open(XenHostPCIDevice
*d
, Error
**errp
)
204 xen_host_pci_sysfs_path(d
, "config", path
, sizeof(path
));
206 d
->config_fd
= open(path
, O_RDWR
);
207 if (d
->config_fd
== -1) {
208 error_setg_file_open(errp
, errno
, path
);
212 static int xen_host_pci_config_read(XenHostPCIDevice
*d
,
213 int pos
, void *buf
, int len
)
218 rc
= pread(d
->config_fd
, buf
, len
, pos
);
219 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
226 static int xen_host_pci_config_write(XenHostPCIDevice
*d
,
227 int pos
, const void *buf
, int len
)
232 rc
= pwrite(d
->config_fd
, buf
, len
, pos
);
233 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
241 int xen_host_pci_get_byte(XenHostPCIDevice
*d
, int pos
, uint8_t *p
)
244 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 1);
251 int xen_host_pci_get_word(XenHostPCIDevice
*d
, int pos
, uint16_t *p
)
254 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 2);
256 *p
= le16_to_cpu(buf
);
261 int xen_host_pci_get_long(XenHostPCIDevice
*d
, int pos
, uint32_t *p
)
264 int rc
= xen_host_pci_config_read(d
, pos
, &buf
, 4);
266 *p
= le32_to_cpu(buf
);
271 int xen_host_pci_get_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
273 return xen_host_pci_config_read(d
, pos
, buf
, len
);
276 int xen_host_pci_set_byte(XenHostPCIDevice
*d
, int pos
, uint8_t data
)
278 return xen_host_pci_config_write(d
, pos
, &data
, 1);
281 int xen_host_pci_set_word(XenHostPCIDevice
*d
, int pos
, uint16_t data
)
283 data
= cpu_to_le16(data
);
284 return xen_host_pci_config_write(d
, pos
, &data
, 2);
287 int xen_host_pci_set_long(XenHostPCIDevice
*d
, int pos
, uint32_t data
)
289 data
= cpu_to_le32(data
);
290 return xen_host_pci_config_write(d
, pos
, &data
, 4);
293 int xen_host_pci_set_block(XenHostPCIDevice
*d
, int pos
, uint8_t *buf
, int len
)
295 return xen_host_pci_config_write(d
, pos
, buf
, len
);
298 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice
*d
, uint32_t cap
)
301 int max_cap
= XEN_HOST_PCI_MAX_EXT_CAP
;
302 int pos
= PCI_CONFIG_SPACE_SIZE
;
305 if (xen_host_pci_get_long(d
, pos
, &header
)) {
309 * If we have no capabilities, this is indicated by cap ID,
310 * cap version and next pointer all being 0.
316 if (PCI_EXT_CAP_ID(header
) == cap
) {
320 pos
= PCI_EXT_CAP_NEXT(header
);
321 if (pos
< PCI_CONFIG_SPACE_SIZE
) {
326 } while (max_cap
> 0);
331 void xen_host_pci_device_get(XenHostPCIDevice
*d
, uint16_t domain
,
332 uint8_t bus
, uint8_t dev
, uint8_t func
,
344 xen_host_pci_config_open(d
, &err
);
349 xen_host_pci_get_resource(d
, &err
);
354 xen_host_pci_get_hex_value(d
, "vendor", &v
, &err
);
360 xen_host_pci_get_hex_value(d
, "device", &v
, &err
);
366 xen_host_pci_get_dec_value(d
, "irq", &v
, &err
);
372 xen_host_pci_get_hex_value(d
, "class", &v
, &err
);
378 d
->is_virtfn
= xen_host_pci_dev_is_virtfn(d
);
383 error_propagate(errp
, err
);
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) {