2012-06-21 17:34:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Citrix Ltd.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:06 +01:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2013-03-18 17:36:02 +01:00
|
|
|
#include "xen-host-pci-device.h"
|
2012-06-21 17:34:24 +02:00
|
|
|
|
|
|
|
#define XEN_HOST_PCI_MAX_EXT_CAP \
|
|
|
|
((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
|
|
|
|
|
|
|
|
#ifdef XEN_HOST_PCI_DEVICE_DEBUG
|
|
|
|
# define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
|
|
|
|
#else
|
|
|
|
# define XEN_HOST_PCI_LOG(f, a...) (void)0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* from linux/ioport.h
|
|
|
|
* IO resources have these defined flags.
|
|
|
|
*/
|
|
|
|
#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
|
|
|
|
|
|
|
|
#define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
|
|
|
|
#define IORESOURCE_IO 0x00000100
|
|
|
|
#define IORESOURCE_MEM 0x00000200
|
|
|
|
|
|
|
|
#define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
|
|
|
|
#define IORESOURCE_MEM_64 0x00100000
|
|
|
|
|
2016-01-17 13:13:10 +01:00
|
|
|
static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
|
|
|
|
const char *name, char *buf, ssize_t size)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
|
|
|
|
d->domain, d->bus, d->dev, d->func, name);
|
2016-01-17 13:13:10 +01:00
|
|
|
assert(rc >= 0 && rc < size);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-16 15:12:21 +02:00
|
|
|
/* This size should be enough to read the first 7 lines of a resource file */
|
|
|
|
#define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
|
2016-01-17 13:13:12 +01:00
|
|
|
static void xen_host_pci_get_resource(XenHostPCIDevice *d, Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
|
|
|
int i, rc, fd;
|
|
|
|
char path[PATH_MAX];
|
2012-08-16 15:12:21 +02:00
|
|
|
char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
|
2012-06-21 17:34:24 +02:00
|
|
|
unsigned long long start, end, flags, size;
|
|
|
|
char *endptr, *s;
|
|
|
|
uint8_t type;
|
|
|
|
|
2016-01-17 13:13:10 +01:00
|
|
|
xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2016-01-17 13:13:12 +01:00
|
|
|
error_setg_file_open(errp, errno, path);
|
|
|
|
return;
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2016-01-17 13:13:12 +01:00
|
|
|
rc = read(fd, &buf, sizeof(buf) - 1);
|
2012-06-21 17:34:24 +02:00
|
|
|
if (rc < 0 && errno != EINTR) {
|
2016-01-17 13:13:12 +01:00
|
|
|
error_setg_errno(errp, errno, "read err");
|
2012-06-21 17:34:24 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} while (rc < 0);
|
|
|
|
buf[rc] = 0;
|
|
|
|
|
|
|
|
s = buf;
|
|
|
|
for (i = 0; i < PCI_NUM_REGIONS; i++) {
|
|
|
|
type = 0;
|
|
|
|
|
|
|
|
start = strtoll(s, &endptr, 16);
|
|
|
|
if (*endptr != ' ' || s == endptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = endptr + 1;
|
|
|
|
end = strtoll(s, &endptr, 16);
|
|
|
|
if (*endptr != ' ' || s == endptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = endptr + 1;
|
|
|
|
flags = strtoll(s, &endptr, 16);
|
|
|
|
if (*endptr != '\n' || s == endptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = endptr + 1;
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
size = end - start + 1;
|
|
|
|
} else {
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & IORESOURCE_IO) {
|
|
|
|
type |= XEN_HOST_PCI_REGION_TYPE_IO;
|
|
|
|
}
|
|
|
|
if (flags & IORESOURCE_MEM) {
|
|
|
|
type |= XEN_HOST_PCI_REGION_TYPE_MEM;
|
|
|
|
}
|
|
|
|
if (flags & IORESOURCE_PREFETCH) {
|
|
|
|
type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
|
|
|
|
}
|
|
|
|
if (flags & IORESOURCE_MEM_64) {
|
|
|
|
type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < PCI_ROM_SLOT) {
|
|
|
|
d->io_regions[i].base_addr = start;
|
|
|
|
d->io_regions[i].size = size;
|
|
|
|
d->io_regions[i].type = type;
|
|
|
|
d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
|
|
|
|
} else {
|
|
|
|
d->rom.base_addr = start;
|
|
|
|
d->rom.size = size;
|
|
|
|
d->rom.type = type;
|
|
|
|
d->rom.bus_flags = flags & IORESOURCE_BITS;
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 13:13:12 +01:00
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
if (i != PCI_NUM_REGIONS) {
|
2016-01-17 13:13:12 +01:00
|
|
|
error_setg(errp, "Invalid format or input too short: %s", buf);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This size should be enough to read a long from a file */
|
|
|
|
#define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
|
2016-01-17 13:13:12 +01:00
|
|
|
static void xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
|
|
|
|
unsigned int *pvalue, int base, Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
|
|
|
|
int fd, rc;
|
|
|
|
unsigned long value;
|
2016-01-17 13:13:11 +01:00
|
|
|
const char *endptr;
|
2012-06-21 17:34:24 +02:00
|
|
|
|
2016-01-17 13:13:10 +01:00
|
|
|
xen_host_pci_sysfs_path(d, name, path, sizeof(path));
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2016-01-17 13:13:12 +01:00
|
|
|
error_setg_file_open(errp, errno, path);
|
|
|
|
return;
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
2016-01-17 13:13:12 +01:00
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
do {
|
2016-01-17 13:13:12 +01:00
|
|
|
rc = read(fd, &buf, sizeof(buf) - 1);
|
2012-06-21 17:34:24 +02:00
|
|
|
if (rc < 0 && errno != EINTR) {
|
2016-01-17 13:13:12 +01:00
|
|
|
error_setg_errno(errp, errno, "read err");
|
2012-06-21 17:34:24 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} while (rc < 0);
|
2016-01-17 13:13:12 +01:00
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
buf[rc] = 0;
|
2016-01-17 13:13:11 +01:00
|
|
|
rc = qemu_strtoul(buf, &endptr, base, &value);
|
|
|
|
if (!rc) {
|
|
|
|
assert(value <= UINT_MAX);
|
2012-06-21 17:34:24 +02:00
|
|
|
*pvalue = value;
|
2016-01-17 13:13:12 +01:00
|
|
|
} else {
|
|
|
|
error_setg_errno(errp, -rc, "failed to parse value '%s'", buf);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
2016-01-17 13:13:12 +01:00
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
static inline void xen_host_pci_get_hex_value(XenHostPCIDevice *d,
|
|
|
|
const char *name,
|
|
|
|
unsigned int *pvalue,
|
|
|
|
Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
2016-01-17 13:13:12 +01:00
|
|
|
xen_host_pci_get_value(d, name, pvalue, 16, errp);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
static inline void xen_host_pci_get_dec_value(XenHostPCIDevice *d,
|
|
|
|
const char *name,
|
|
|
|
unsigned int *pvalue,
|
|
|
|
Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
2016-01-17 13:13:12 +01:00
|
|
|
xen_host_pci_get_value(d, name, pvalue, 10, errp);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
struct stat buf;
|
|
|
|
|
2016-01-17 13:13:10 +01:00
|
|
|
xen_host_pci_sysfs_path(d, "physfn", path, sizeof(path));
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
return !stat(path, &buf);
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
static void xen_host_pci_config_open(XenHostPCIDevice *d, Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2016-01-17 13:13:10 +01:00
|
|
|
xen_host_pci_sysfs_path(d, "config", path, sizeof(path));
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
d->config_fd = open(path, O_RDWR);
|
2016-01-17 13:13:12 +01:00
|
|
|
if (d->config_fd == -1) {
|
|
|
|
error_setg_file_open(errp, errno, path);
|
2012-06-21 17:34:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xen_host_pci_config_read(XenHostPCIDevice *d,
|
|
|
|
int pos, void *buf, int len)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rc = pread(d->config_fd, buf, len, pos);
|
|
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
if (rc != len) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xen_host_pci_config_write(XenHostPCIDevice *d,
|
|
|
|
int pos, const void *buf, int len)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
do {
|
|
|
|
rc = pwrite(d->config_fd, buf, len, pos);
|
|
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
if (rc != len) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
|
|
|
|
{
|
|
|
|
uint8_t buf;
|
|
|
|
int rc = xen_host_pci_config_read(d, pos, &buf, 1);
|
|
|
|
if (!rc) {
|
|
|
|
*p = buf;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
|
|
|
|
{
|
|
|
|
uint16_t buf;
|
|
|
|
int rc = xen_host_pci_config_read(d, pos, &buf, 2);
|
|
|
|
if (!rc) {
|
|
|
|
*p = le16_to_cpu(buf);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
|
|
|
|
{
|
|
|
|
uint32_t buf;
|
|
|
|
int rc = xen_host_pci_config_read(d, pos, &buf, 4);
|
|
|
|
if (!rc) {
|
|
|
|
*p = le32_to_cpu(buf);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
return xen_host_pci_config_read(d, pos, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
|
|
|
|
{
|
|
|
|
return xen_host_pci_config_write(d, pos, &data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
|
|
|
|
{
|
|
|
|
data = cpu_to_le16(data);
|
|
|
|
return xen_host_pci_config_write(d, pos, &data, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
|
|
|
|
{
|
|
|
|
data = cpu_to_le32(data);
|
|
|
|
return xen_host_pci_config_write(d, pos, &data, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
return xen_host_pci_config_write(d, pos, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
|
|
|
|
{
|
|
|
|
uint32_t header = 0;
|
|
|
|
int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
|
|
|
|
int pos = PCI_CONFIG_SPACE_SIZE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (xen_host_pci_get_long(d, pos, &header)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we have no capabilities, this is indicated by cap ID,
|
|
|
|
* cap version and next pointer all being 0.
|
|
|
|
*/
|
|
|
|
if (header == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PCI_EXT_CAP_ID(header) == cap) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = PCI_EXT_CAP_NEXT(header);
|
|
|
|
if (pos < PCI_CONFIG_SPACE_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_cap--;
|
|
|
|
} while (max_cap > 0);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
|
|
|
|
uint8_t bus, uint8_t dev, uint8_t func,
|
|
|
|
Error **errp)
|
2012-06-21 17:34:24 +02:00
|
|
|
{
|
|
|
|
unsigned int v;
|
2016-01-17 13:13:12 +01:00
|
|
|
Error *err = NULL;
|
2012-06-21 17:34:24 +02:00
|
|
|
|
|
|
|
d->config_fd = -1;
|
|
|
|
d->domain = domain;
|
|
|
|
d->bus = bus;
|
|
|
|
d->dev = dev;
|
|
|
|
d->func = func;
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
xen_host_pci_config_open(d, &err);
|
|
|
|
if (err) {
|
2012-06-21 17:34:24 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2016-01-17 13:13:12 +01:00
|
|
|
|
|
|
|
xen_host_pci_get_resource(d, &err);
|
|
|
|
if (err) {
|
2012-06-21 17:34:24 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2016-01-17 13:13:12 +01:00
|
|
|
|
|
|
|
xen_host_pci_get_hex_value(d, "vendor", &v, &err);
|
|
|
|
if (err) {
|
2012-06-21 17:34:24 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
d->vendor_id = v;
|
2016-01-17 13:13:12 +01:00
|
|
|
|
|
|
|
xen_host_pci_get_hex_value(d, "device", &v, &err);
|
|
|
|
if (err) {
|
2012-06-21 17:34:24 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
d->device_id = v;
|
2016-01-17 13:13:12 +01:00
|
|
|
|
|
|
|
xen_host_pci_get_dec_value(d, "irq", &v, &err);
|
|
|
|
if (err) {
|
2012-06-21 17:34:24 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
d->irq = v;
|
2016-01-17 13:13:12 +01:00
|
|
|
|
|
|
|
xen_host_pci_get_hex_value(d, "class", &v, &err);
|
|
|
|
if (err) {
|
2015-07-15 07:37:45 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
d->class_code = v;
|
2016-01-17 13:13:12 +01:00
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
|
|
|
|
|
2016-01-17 13:13:12 +01:00
|
|
|
return;
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
error:
|
2016-01-17 13:13:12 +01:00
|
|
|
error_propagate(errp, err);
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
if (d->config_fd >= 0) {
|
|
|
|
close(d->config_fd);
|
|
|
|
d->config_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:21:29 +02:00
|
|
|
bool xen_host_pci_device_closed(XenHostPCIDevice *d)
|
|
|
|
{
|
|
|
|
return d->config_fd == -1;
|
|
|
|
}
|
|
|
|
|
2012-06-21 17:34:24 +02:00
|
|
|
void xen_host_pci_device_put(XenHostPCIDevice *d)
|
|
|
|
{
|
|
|
|
if (d->config_fd >= 0) {
|
|
|
|
close(d->config_fd);
|
|
|
|
d->config_fd = -1;
|
|
|
|
}
|
|
|
|
}
|