[PATCH 26/28] kvm tools: Add pci__config_{rd,wr}(), pci__find_dev()

Matt Evans
2011-12-06T03:42:51Z

This allows config space access in a more natural manner than clunky x86 IO ports,
and is useful for other architectures.

Furthermore, the actual registers were only accessed in 32bit chunks; other
systems (e.g. PPC) allow smaller accesses so that, for example, the 16-bit
config field can be read directly. This patch allows this sort of addressing.

Signed-off-by: Matt Evans <matt@ozlabs.org>
---
tools/kvm/include/kvm/pci.h | 5 +++
tools/kvm/pci.c | 63 +++++++++++++++++++++++++++---------------
2 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/tools/kvm/include/kvm/pci.h b/tools/kvm/include/kvm/pci.h
index 88e92dc..be2b0bc 100644
--- a/tools/kvm/include/kvm/pci.h
+++ b/tools/kvm/include/kvm/pci.h
@@ -7,6 +7,8 @@
#include <linux/msi.h>
#include <endian.h>

+#include "kvm/kvm.h"
+
#define PCI_MAX_DEVICES 256
/*
* PCI Configuration Mechanism #1 I/O ports. See Section 3.7.4.1.
@@ -82,6 +84,9 @@ struct pci_device_header {

void pci__init(void);
void pci__register(struct pci_device_header *dev, u8 dev_num);
+struct pci_device_header *pci__find_dev(u8 dev_num);
u32 pci_get_io_space_block(u32 size);
+void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size);
+void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size);

#endif /* KVM__PCI_H */
diff --git a/tools/kvm/pci.c b/tools/kvm/pci.c
index 5bbcbc7..8282e23 100644
--- a/tools/kvm/pci.c
+++ b/tools/kvm/pci.c
@@ -77,7 +77,6 @@ static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_numbe
static bool pci_config_data_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
{
unsigned long start;
- u8 dev_num;

/*
* If someone accesses PCI configuration space offsets that are not
@@ -85,12 +84,41 @@ static bool pci_config_data_out(struct ioport *ioport, struct kvm *kvm, u16 port
*/
start = port - PCI_CONFIG_DATA;

- dev_num = pci_config_address.device_number;
+ pci__config_wr(kvm, pci_config_address, data, size);
+
+ return true;
+}
+
+static bool pci_config_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
+{
+ unsigned long start;
+
+ /*
+ * If someone accesses PCI configuration space offsets that are not
+ * aligned to 4 bytes, it uses ioports to signify that.
+ */
+ start = port - PCI_CONFIG_DATA;
+
+ pci__config_rd(kvm, pci_config_address, data, size);
+
+ return true;
+}
+
+static struct ioport_operations pci_config_data_ops = {
+ .io_in = pci_config_data_in,
+ .io_out = pci_config_data_out,
+};
+
+void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size)
+{
+ u8 dev_num;
+
+ dev_num = addr.device_number;

if (pci_device_exists(0, dev_num, 0)) {
unsigned long offset;

- offset = start + (pci_config_address.register_number << 2);
+ offset = addr.w & 0xff;
if (offset < sizeof(struct pci_device_header)) {
void *p = pci_devices[dev_num];
u8 bar = (offset - PCI_BAR_OFFSET(0)) / (sizeof(u32));
@@ -116,27 +144,18 @@ static bool pci_config_data_out(struct ioport *ioport, struct kvm *kvm, u16 port
}
}
}
-
- return true;
}

-static bool pci_config_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
+void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size)
{
- unsigned long start;
u8 dev_num;

- /*
- * If someone accesses PCI configuration space offsets that are not
- * aligned to 4 bytes, it uses ioports to signify that.
- */
- start = port - PCI_CONFIG_DATA;
-
- dev_num = pci_config_address.device_number;
+ dev_num = addr.device_number;

if (pci_device_exists(0, dev_num, 0)) {
unsigned long offset;

- offset = start + (pci_config_address.register_number << 2);
+ offset = addr.w & 0xff;
if (offset < sizeof(struct pci_device_header)) {
void *p = pci_devices[dev_num];

@@ -145,22 +164,20 @@ static bool pci_config_data_in(struct ioport *ioport, struct kvm *kvm, u16 port,
memset(data, 0x00, size);
} else
memset(data, 0xff, size);
-
- return true;
}

-static struct ioport_operations pci_config_data_ops = {
- .io_in = pci_config_data_in,
- .io_out = pci_config_data_out,
-};
-
void pci__register(struct pci_device_header *dev, u8 dev_num)
{
assert(dev_num < PCI_MAX_DEVICES);
-
pci_devices[dev_num] = dev;
}

+struct pci_device_header *pci__find_dev(u8 dev_num)
+{
+ assert(dev_num < PCI_MAX_DEVICES);
+ return pci_devices[dev_num];
+}
+
void pci__init(void)
{
ioport__register(PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);