Skip to contentSkip to conversation

kvm tools: pci: add PCI support for ARM targets

PATCH New Will Deacon 1 participant 18 messages Feb 4, 2014 KVM

lore
Share & tools
lore
#0 lore raw

cover kvm tools: pci: add PCI support for ARM targets

Hello,

This patch series adds PCI support for ARM targets to lkvm. Note that there are corresponding kernel patches in order for Linux to use the virtual PCI host controller, so the interface is still subject to change.

The main parts of this series are:

- Fixes to the MSI-X bar and virtio-net device initialisation

- An effective rewrite of the IRQ allocation code, so that core code handles the IRQ allocation for a device, based upon its bus

- Introduction of a PCI Memory Space, which is actually a shim around the ioport emulation and simply redirects guest access there

- Generation of a device-tree node describing the PCI emulation, using the properties laid out in ePAPR and associated specs

Tested on x86_64, AArch32 and AArch64. Build-tested only on PPC64.

All feedback welcome!

Will

Will Deacon (17):
kvm tools: pci: register virtio pba structure as mmio region with kvm kvm tools: pci: remove BAR 3 hangover from virtio pci msix code kvm tools: net: don't propagate error codes from tx/rx operations kvm tools: net: allow a mixture of pci and mmio virtio devices kvm tools: pci: register 24-bit configuration space below MMIO region kvm tools: pci: ensure BARs are naturally aligned
kvm tools: pci: add MMIO interface to virtio-pci devices kvm tools: irq: remove pin parameter from irq__register_device kvm tools: irq: replace the x86 irq rbtree with the PCI device tree kvm tools: irq: remove remaining parameters to irq__register_device kvm tools: irq: rename irq__register_device to irq__alloc_line kvm tools: irq: make irq__alloc_line generic
kvm tools: irq: move irq line allocation into device registration kvm tools: ARM: route guest PCI accesses to the emulation layer kvm tools: ARM: generate an fdt node for our PCI emulation kvm tools: powerpc: make use of common of_pci.h header definitions kvm tools: ARM: allow default virtio transport to be passed on cmdline

 tools/kvm/Makefile                                 |   3 +-
 tools/kvm/arm/fdt.c                                |   4 +
 tools/kvm/arm/gic.c                                |  12 --
 tools/kvm/arm/include/arm-common/kvm-arch.h        |  20 +++-
 tools/kvm/arm/include/arm-common/kvm-config-arch.h |  12 +-
 tools/kvm/arm/include/arm-common/pci.h             |   6 +
 tools/kvm/arm/ioport.c                             |   5 +-
 tools/kvm/arm/irq.c                                |   8 --
 tools/kvm/arm/kvm-cpu.c                            |   4 +-
 tools/kvm/arm/kvm.c                                |   5 +
 tools/kvm/arm/pci.c                                | 118 ++++++++++++++++++++
 tools/kvm/devices.c                                |  13 +++
 tools/kvm/hw/pci-shmem.c                           |   9 --
 tools/kvm/hw/vesa.c                                |   7 --
 tools/kvm/include/kvm/irq.h                        |  16 +--
 tools/kvm/include/kvm/of_pci.h                     |  44 ++++++++
 tools/kvm/include/kvm/pci.h                        |   3 +
 tools/kvm/include/kvm/virtio-mmio.h                |   1 +
 tools/kvm/include/kvm/virtio-pci.h                 |   4 +-
 tools/kvm/irq.c                                    |   9 ++
 tools/kvm/net/uip/core.c                           |   2 +-
 tools/kvm/pci.c                                    |  54 +++++++--
 tools/kvm/powerpc/include/kvm/kvm-arch.h           |   9 +-
 tools/kvm/powerpc/irq.c                            |  19 ----
 tools/kvm/powerpc/spapr_pci.c                      | 114 ++++++++-----------
 tools/kvm/powerpc/xics.c                           |  28 +----
 tools/kvm/virtio/9p.c                              |   2 +-
 tools/kvm/virtio/balloon.c                         |   2 +-
 tools/kvm/virtio/blk.c                             |   2 +-
 tools/kvm/virtio/console.c                         |   2 +-
 tools/kvm/virtio/mmio.c                            |  14 ++-
 tools/kvm/virtio/net.c                             |  42 +++++--
 tools/kvm/virtio/pci.c                             | 120 ++++++++++++--------
 tools/kvm/virtio/rng.c                             |   2 +-
 tools/kvm/virtio/scsi.c                            |   2 +-
 tools/kvm/x86/include/kvm/kvm-arch.h               |   9 +-
 tools/kvm/x86/irq.c                                | 121 ---------------------
 tools/kvm/x86/mptable.c                            |  35 +++---
 38 files changed, 480 insertions(+), 402 deletions(-)
 create mode 100644 tools/kvm/arm/include/arm-common/pci.h
 create mode 100644 tools/kvm/arm/pci.c
 create mode 100644 tools/kvm/include/kvm/of_pci.h
 create mode 100644 tools/kvm/irq.c
Signature · 1 line
1.8.2.2
#1 lore raw

PATCH 1/17 kvm tools: pci: register virtio pba structure as mmio region with kvm

Despite allocating and providing code to handle accesses to the pba structure, virtio msix blocks in fact only register the msix table with kvm.

This patch fixes the MMIO region so that it includes the pba structure for msix io blocks of virtio pci devices. The corresponding BAR is also updated to advertise the full size of the io block.

---
 tools/kvm/virtio/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/kvm/virtio/pci.c +2 -2
index bb6e7c45e12a..91010cabbcde 100644
@@ -322,7 +322,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		return r;
 
 	vpci->base_addr = (u16)r;
-	r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE, false,
+	r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE * 2, false,
 			       virtio_pci__mmio_callback, vpci);
 	if (r < 0)
 		goto free_ioport;
@@ -344,7 +344,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.status			= cpu_to_le16(PCI_STATUS_CAP_LIST),
 		.capabilities		= (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr,
 		.bar_size[0]		= IOPORT_SIZE,
-		.bar_size[1]		= PCI_IO_SIZE,
+		.bar_size[1]		= PCI_IO_SIZE * 2,
 		.bar_size[3]		= PCI_IO_SIZE,
 	};
 
Signature · 1 line
1.8.2.2
#2 lore raw

PATCH 2/17 kvm tools: pci: remove BAR 3 hangover from virtio pci msix code

In reply to Will Deacon

Despite not being used anymore, there are still traces of BAR 3 in both the code and comments for the virtio pci msix implementation.

This patch removes the redundant code and fixes up the comments to match what we're actually doing.

---
 tools/kvm/virtio/pci.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/kvm/virtio/pci.c +3 -7
index 91010cabbcde..77c933fd4ab2 100644
@@ -345,7 +345,6 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.capabilities		= (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr,
 		.bar_size[0]		= IOPORT_SIZE,
 		.bar_size[1]		= PCI_IO_SIZE * 2,
-		.bar_size[3]		= PCI_IO_SIZE,
 	};
 
 	vpci->dev_hdr = (struct device_header) {
@@ -368,12 +367,9 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	 */
 	vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG - 1);
 
-	/*
-	 * Both table and PBA could be mapped on the same BAR, but for now
-	 * we're not in short of BARs
-	 */
-	vpci->pci_hdr.msix.table_offset = cpu_to_le32(1); /* Use BAR 1 */
-	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(1 | PCI_IO_SIZE); /* Use BAR 3 */
+	/* Both table and PBA are mapped to the same BAR (1) */
+	vpci->pci_hdr.msix.table_offset = cpu_to_le32(1);
+	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(1 | PCI_IO_SIZE);
 	vpci->config_vector = 0;
 
 	r = irq__register_device(subsys_id, &pin, &line);
Signature · 1 line
1.8.2.2
#3 lore raw

PATCH 3/17 kvm tools: net: don't propagate error codes from tx/rx operations

In reply to Will Deacon

Currently, if a ->tx or ->rx callback into the net_dev_operations encounters an error, it returns -1 to the virtio-net code, which in turn treats this as an unsigned (size_t) size describing the data available. The resulting memcpy operation then quickly explodes with a SEGV.

This patch detects the error code from the low-level callbacks and exits the thread dealing with the erroneous queue.

---
 tools/kvm/net/uip/core.c |  2 +-
 tools/kvm/virtio/net.c   | 20 ++++++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)
tools/kvm/net/uip/core.c +1 -1
index b3cd8c25d4da..e860f3afec97 100644
@@ -36,7 +36,7 @@ int uip_tx(struct iovec *iov, u16 out, struct uip_info *info)
 
 		buf = malloc(eth_len);
 		if (!buf)
-			return -1;
+			return -ENOMEM;
 
 		eth = (struct uip_eth *)buf;
 		for (i = 1; i < out; i++) {
tools/kvm/virtio/net.c +16 -4
index 2f61718a5029..ae4712c3c550 100644
@@ -81,7 +81,7 @@ static void *virtio_net_rx_thread(void *p)
 	struct net_dev *ndev = p;
 	u16 out, in;
 	u16 head;
-	size_t len, copied;
+	int len, copied;
 	u32 id;
 
 	mutex_lock(&ndev->mutex);
@@ -108,11 +108,17 @@ static void *virtio_net_rx_thread(void *p)
 			struct virtio_net_hdr_mrg_rxbuf *hdr;
 
 			len = ndev->ops->rx(&dummy_iov, 1, ndev);
+			if (len < 0) {
+				pr_warning("%s: rx on vq %u failed (%d), exiting thread\n",
+						__func__, id, len);
+				goto out_err;
+			}
+
 			copied = 0;
 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
 			hdr = (void *)iov[0].iov_base;
 			while (copied < len) {
-				size_t iovsize = min(len - copied, iov_size(iov, in));
+				size_t iovsize = min_t(size_t, len - copied, iov_size(iov, in));
 
 				memcpy_toiovec(iov, buffer + copied, iovsize);
 				copied += iovsize;
@@ -131,6 +137,7 @@ static void *virtio_net_rx_thread(void *p)
 		}
 	}
 
+out_err:
 	pthread_exit(NULL);
 	return NULL;
 
@@ -165,6 +172,12 @@ static void *virtio_net_tx_thread(void *p)
 		while (virt_queue__available(vq)) {
 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
 			len = ndev->ops->tx(iov, out, ndev);
+			if (len < 0) {
+				pr_warning("%s: tx on vq %u failed (%d)\n",
+						__func__, id, len);
+				goto out_err;
+			}
+
 			virt_queue__set_used_elem(vq, head, len);
 		}
 
@@ -172,10 +185,9 @@ static void *virtio_net_tx_thread(void *p)
 			ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, id);
 	}
 
+out_err:
 	pthread_exit(NULL);
-
 	return NULL;
-
 }
 
 static virtio_net_ctrl_ack virtio_net_handle_mq(struct kvm* kvm, struct net_dev *ndev, struct virtio_net_ctrl_hdr *ctrl)
Signature · 1 line
1.8.2.2
#4 lore raw

PATCH 4/17 kvm tools: net: allow a mixture of pci and mmio virtio devices

In reply to Will Deacon

When attempting to initialise a mixture of pci and mmio virtio devices, we cannot share an ops structure, otherwise the transport-specific fields (init/exit and signal handling) will be globally set to the transport of the last registered device.

This patch dynamically allocates a new ops structure for each instance of a virtio net device.

---
 tools/kvm/virtio/net.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
tools/kvm/virtio/net.c +17 -5
index ae4712c3c550..dbb443124ee1 100644
@@ -713,13 +713,20 @@ done:
 
 static int virtio_net__init_one(struct virtio_net_params *params)
 {
-	int i;
+	int i, err;
 	struct net_dev *ndev;
+	struct virtio_ops *ops;
 
 	ndev = calloc(1, sizeof(struct net_dev));
 	if (ndev == NULL)
 		return -ENOMEM;
 
+	ops = malloc(sizeof(*ops));
+	if (ops == NULL) {
+		err = -ENOMEM;
+		goto err_free_ndev;
+	}
+
 	list_add_tail(&ndev->list, &ndevs);
 
 	ndev->kvm = params->kvm;
@@ -749,12 +756,13 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 		uip_static_init(&ndev->info);
 	}
 
+	*ops = net_dev_virtio_ops;
 	if (params->trans && strcmp(params->trans, "mmio") == 0)
-		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
-			    VIRTIO_MMIO, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
+		virtio_init(params->kvm, ndev, &ndev->vdev, ops, VIRTIO_MMIO,
+			    PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
 	else
-		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
-			    VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
+		virtio_init(params->kvm, ndev, &ndev->vdev, ops, VIRTIO_PCI,
+			    PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
 
 	if (params->vhost)
 		virtio_net__vhost_init(params->kvm, ndev);
@@ -763,6 +771,10 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 		compat_id = virtio_compat_add_message("virtio-net", "CONFIG_VIRTIO_NET");
 
 	return 0;
+
+err_free_ndev:
+	free(ndev);
+	return err;
 }
 
 int virtio_net__init(struct kvm *kvm)
Signature · 1 line
1.8.2.2
#5 lore raw

PATCH 5/17 kvm tools: pci: register 24-bit configuration space below MMIO region

In reply to Will Deacon

Rather than performing all config accesses via ioports, map in a 24-bit memory-mapped configuration space directly below the PCI MMIO region.

This will allow architectures to support PCI without having to support legacy ioports in the guest kernel. Instead, kvm tool can forward the config accesses directly to the relevant ioport config callbacks.

---
 tools/kvm/arm/include/arm-common/kvm-arch.h |  7 +++++--
 tools/kvm/include/kvm/pci.h                 |  1 +
 tools/kvm/pci.c                             | 31 +++++++++++++++++++++++++----
 tools/kvm/powerpc/include/kvm/kvm-arch.h    |  5 +++--
 tools/kvm/x86/include/kvm/kvm-arch.h        |  5 +++--
 5 files changed, 39 insertions(+), 10 deletions(-)
tools/kvm/arm/include/arm-common/kvm-arch.h +5 -2
index 72b204fd45a4..caef590a44ff 100644
@@ -19,10 +19,13 @@
 
 #define ARM_IOPORT_SIZE		(ARM_MMIO_AREA - ARM_IOPORT_AREA)
 #define ARM_VIRTIO_MMIO_SIZE	(ARM_AXI_AREA - (ARM_MMIO_AREA + ARM_GIC_SIZE))
-#define ARM_PCI_MMIO_SIZE	(ARM_MEMORY_AREA - ARM_AXI_AREA)
+#define ARM_PCI_CFG_SIZE	(1ULL << 24)
+#define ARM_PCI_MMIO_SIZE	(ARM_MEMORY_AREA - \
+				(ARM_AXI_AREA + ARM_PCI_CFG_SIZE))
 
 #define KVM_IOPORT_AREA		ARM_IOPORT_AREA
-#define KVM_PCI_MMIO_AREA	ARM_AXI_AREA
+#define KVM_PCI_CFG_AREA	ARM_AXI_AREA
+#define KVM_PCI_MMIO_AREA	(KVM_PCI_CFG_AREA + ARM_PCI_CFG_SIZE)
 #define KVM_VIRTIO_MMIO_AREA	ARM_MMIO_AREA
 
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_MMIO
tools/kvm/include/kvm/pci.h +1 -0
index 3da381175c8d..e1e621d51470 100644
@@ -18,6 +18,7 @@
 #define PCI_CONFIG_DATA		0xcfc
 #define PCI_CONFIG_BUS_FORWARD	0xcfa
 #define PCI_IO_SIZE		0x100
+#define PCI_CFG_SIZE		(1ULL << 24)
 
 union pci_config_address {
 	struct {
tools/kvm/pci.c +27 -4
index 8d3732d35842..e735352eb042 100644
@@ -162,6 +162,20 @@ void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data,
 	}
 }
 
+static void pci_config_mmio_access(u64 addr, u8 *data, u32 len, u8 is_write, void *kvm)
+{
+	union pci_config_address cfg_addr;
+
+	addr			-= KVM_PCI_CFG_AREA;
+	cfg_addr.w		= (u32)addr;
+	cfg_addr.enable_bit	= 1;
+
+	if (is_write)
+		pci__config_wr(kvm, cfg_addr, data, len);
+	else
+		pci__config_rd(kvm, cfg_addr, data, len);
+}
+
 struct pci_device_header *pci__find_dev(u8 dev_num)
 {
 	struct device_header *hdr = device__find_dev(DEVICE_BUS_PCI, dev_num);
@@ -181,12 +195,21 @@ int pci__init(struct kvm *kvm)
 		return r;
 
 	r = ioport__register(kvm, PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
-	if (r < 0) {
-		ioport__unregister(kvm, PCI_CONFIG_DATA);
-		return r;
-	}
+	if (r < 0)
+		goto err_unregister_data;
+
+	r = kvm__register_mmio(kvm, KVM_PCI_CFG_AREA, PCI_CFG_SIZE, false,
+			       pci_config_mmio_access, kvm);
+	if (r < 0)
+		goto err_unregister_addr;
 
 	return 0;
+
+err_unregister_addr:
+	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
+err_unregister_data:
+	ioport__unregister(kvm, PCI_CONFIG_DATA);
+	return r;
 }
 dev_base_init(pci__init);
 
tools/kvm/powerpc/include/kvm/kvm-arch.h +3 -2
index 96dea91cbedd..c147c78b71e7 100644
@@ -38,8 +38,9 @@
  * from.  Note that this is a PCI bus address.
  */
 #define KVM_IOPORT_AREA			0x0
-#define KVM_PCI_MMIO_AREA		0x1000000
-#define KVM_VIRTIO_MMIO_AREA		0x2000000
+#define KVM_PCI_CFG_AREA		0x1000000
+#define KVM_PCI_MMIO_AREA		0x2000000
+#define KVM_VIRTIO_MMIO_AREA		0x3000000
 
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
 
tools/kvm/x86/include/kvm/kvm-arch.h +3 -2
index 6d59c8e5de0a..8e8389627354 100644
@@ -21,8 +21,9 @@
  * from.  Note that this is a PCI bus address (though same on x86).
  */
 #define KVM_IOPORT_AREA		0x0
-#define KVM_PCI_MMIO_AREA	(KVM_MMIO_START + 0x1000000)
-#define KVM_VIRTIO_MMIO_AREA	(KVM_MMIO_START + 0x2000000)
+#define KVM_PCI_CFG_AREA	(KVM_MMIO_START + 0x1000000)
+#define KVM_PCI_MMIO_AREA	(KVM_MMIO_START + 0x2000000)
+#define KVM_VIRTIO_MMIO_AREA	(KVM_MMIO_START + 0x3000000)
 
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
 
Signature · 1 line
1.8.2.2
#6 lore raw

PATCH 6/17 kvm tools: pci: ensure BARs are naturally aligned

In reply to Will Deacon

BARs must be naturally aligned, so enforce this in the PCI IO space allocator.

---
 tools/kvm/pci.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/kvm/pci.c +5 -3
index e735352eb042..c4442c85b5cf 100644
@@ -18,11 +18,13 @@ static union pci_config_address		pci_config_address;
  */
 static u32 io_space_blocks		= KVM_PCI_MMIO_AREA;
 
+/*
+ * BARs must be naturally aligned, so enforce this in the allocator.
+ */
 u32 pci_get_io_space_block(u32 size)
 {
-	u32 block = io_space_blocks;
-	io_space_blocks += size;
-
+	u32 block = ALIGN(io_space_blocks, size);
+	io_space_blocks = block + size;
 	return block;
 }
 
Signature · 1 line
1.8.2.2
#7 lore raw

PATCH 7/17 kvm tools: pci: add MMIO interface to virtio-pci devices

In reply to Will Deacon

This patch adds an MMIO interface for each virtio-pci device, so that they can be accessed without having to use an ioport. For each device, a new memory BAR is added which corresponds to an area of MMIO space with a shim trap handler. This handler simply translates the access into an ioport access via kvm__emulate_io. Since guests can generate accesses via either the ioport or MMIO regions, an ioeventfd is registered for both.

---
 tools/kvm/include/kvm/virtio-pci.h |   4 +-
 tools/kvm/virtio/pci.c             | 111 +++++++++++++++++++++++++------------
 2 files changed, 78 insertions(+), 37 deletions(-)
tools/kvm/include/kvm/virtio-pci.h +3 -1
index 9b063924e5da..c795ce71fa88 100644
@@ -22,8 +22,10 @@ struct virtio_pci {
 	struct pci_device_header pci_hdr;
 	struct device_header	dev_hdr;
 	void			*dev;
+	struct kvm		*kvm;
 
-	u16			base_addr;
+	u16			port_addr;
+	u32			mmio_addr;
 	u8			status;
 	u8			isr;
 	u32			features;
tools/kvm/virtio/pci.c +75 -36

213 lines not on this page · raw message

Signature · 1 line
1.8.2.2
#8 lore raw

PATCH 8/17 kvm tools: irq: remove pin parameter from irq__register_device

In reply to Will Deacon

In preparation for moving the irq allocation into generic code, remove the pin parameter from irq__register_device and temporarily place the onus on the emulation driver to allocate the pin (which is always 1 and only used on PCI anyway).

---
 tools/kvm/arm/irq.c         |  2 +-
 tools/kvm/hw/pci-shmem.c    |  6 +++---
 tools/kvm/hw/vesa.c         |  6 +++---
 tools/kvm/include/kvm/irq.h |  2 +-
 tools/kvm/powerpc/irq.c     |  3 +--
 tools/kvm/virtio/mmio.c     |  4 ++--
 tools/kvm/virtio/pci.c      |  6 +++---
 tools/kvm/x86/irq.c         | 10 +---------
 8 files changed, 15 insertions(+), 24 deletions(-)
tools/kvm/arm/irq.c +1 -1
index e173e04f3668..dd53f1216c51 100644
@@ -4,7 +4,7 @@
 
 #include "arm-common/gic.h"
 
-int irq__register_device(u32 dev, u8 *pin, u8 *line)
+int irq__register_device(u32 dev, u8 *line)
 {
 	*line = gic__alloc_irqnum();
 	return 0;
tools/kvm/hw/pci-shmem.c +3 -3
index 701231a6ce1e..d0d88f821aba 100644
@@ -352,7 +352,7 @@ int shmem_parser(const struct option *opt, const char *arg, int unset)
 
 int pci_shmem__init(struct kvm *kvm)
 {
-	u8 line, pin;
+	u8 line;
 	char *mem;
 	int r;
 
@@ -360,11 +360,11 @@ int pci_shmem__init(struct kvm *kvm)
 		return 0;
 
 	/* Register good old INTx */
-	r = irq__register_device(PCI_DEVICE_ID_PCI_SHMEM, &pin, &line);
+	r = irq__register_device(PCI_DEVICE_ID_PCI_SHMEM, &line);
 	if (r < 0)
 		return r;
 
-	pci_shmem_pci_device.irq_pin = pin;
+	pci_shmem_pci_device.irq_pin = 1;
 	pci_shmem_pci_device.irq_line = line;
 
 	/* Register MMIO space for MSI-X */
tools/kvm/hw/vesa.c +3 -3
index 47e3a694d98b..07c3978c785e 100644
@@ -55,14 +55,14 @@ static struct framebuffer vesafb;
 struct framebuffer *vesa__init(struct kvm *kvm)
 {
 	u16 vesa_base_addr;
-	u8 line, pin;
+	u8 line;
 	char *mem;
 	int r;
 
 	if (!kvm->cfg.vnc && !kvm->cfg.sdl && !kvm->cfg.gtk)
 		return NULL;
 
-	r = irq__register_device(PCI_DEVICE_ID_VESA, &pin, &line);
+	r = irq__register_device(PCI_DEVICE_ID_VESA, &line);
 	if (r < 0)
 		return ERR_PTR(r);
 
@@ -70,7 +70,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		return ERR_PTR(r);
 
-	vesa_pci_device.irq_pin		= pin;
+	vesa_pci_device.irq_pin		= 1;
 	vesa_pci_device.irq_line	= line;
 	vesa_base_addr			= (u16)r;
 	vesa_pci_device.bar[0]		= cpu_to_le32(vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO);
tools/kvm/include/kvm/irq.h +1 -1
index 5c1274b98610..20213c064b0b 100644
@@ -22,7 +22,7 @@ struct pci_dev {
 	struct list_head	lines;
 };
 
-int irq__register_device(u32 dev, u8 *pin, u8 *line);
+int irq__register_device(u32 dev, u8 *line);
 
 struct rb_node *irq__get_pci_tree(void);
 
tools/kvm/powerpc/irq.c +1 -2
index ae9da507fb82..d906bb2840bc 100644
@@ -32,9 +32,8 @@
  * generic & cope with multiple PPC platform types.
  */
 
-int irq__register_device(u32 dev, u8 *pin, u8 *line)
+int irq__register_device(u32 dev, u8 *line)
 {
-	*pin = 1;
 	/*
 	 * Have I said how nasty I find this?  Line should be dontcare... PHB
 	 * should determine which CPU/XICS IRQ to fire.
tools/kvm/virtio/mmio.c +2 -2
index afae6a70f006..90f90ede3c1f 100644
@@ -260,7 +260,7 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		     int device_id, int subsys_id, int class)
 {
 	struct virtio_mmio *vmmio = vdev->virtio;
-	u8 pin, line;
+	u8 line;
 
 	vmmio->addr	= virtio_mmio_get_io_space_block(VIRTIO_MMIO_IO_SIZE);
 	vmmio->kvm	= kvm;
@@ -277,7 +277,7 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.queue_num_max	= 256,
 	};
 
-	if (irq__register_device(subsys_id, &pin, &line) < 0)
+	if (irq__register_device(subsys_id, &line) < 0)
 		return -1;
 	vmmio->irq = line;
 	vmmio->dev_hdr = (struct device_header) {
tools/kvm/virtio/pci.c +3 -3
index e1b5be6b036e..77dde3bee741 100644
@@ -335,7 +335,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		     int device_id, int subsys_id, int class)
 {
 	struct virtio_pci *vpci = vdev->virtio;
-	u8 pin, line;
+	u8 line;
 	int r;
 
 	vpci->kvm = kvm;
@@ -406,14 +406,14 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(2 | PCI_IO_SIZE);
 	vpci->config_vector = 0;
 
-	r = irq__register_device(subsys_id, &pin, &line);
+	r = irq__register_device(subsys_id, &line);
 	if (r < 0)
 		goto free_msix_mmio;
 
 	if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI))
 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
 
-	vpci->pci_hdr.irq_pin	= pin;
+	vpci->pci_hdr.irq_pin	= 1;
 	vpci->pci_hdr.irq_line	= line;
 	r = device__register(&vpci->dev_hdr);
 	if (r < 0)
tools/kvm/x86/irq.c +1 -9
index 7447c6b7d7aa..17db2110e96f 100644
@@ -85,7 +85,7 @@ static int insert(struct rb_root *root, struct pci_dev *data)
 	return 0;
 }
 
-int irq__register_device(u32 dev, u8 *pin, u8 *line)
+int irq__register_device(u32 dev, u8 *line)
 {
 	struct pci_dev *node;
 	int r;
@@ -100,13 +100,6 @@ int irq__register_device(u32 dev, u8 *pin, u8 *line)
 
 		*node = (struct pci_dev) {
 			.id	= dev,
-			/*
-			 * PCI supports only INTA#,B#,C#,D# per device.
-			 * A#,B#,C#,D# are allowed for multifunctional
-			 * devices so stick with A# for our single
-			 * function devices.
-			 */
-			.pin	= 1,
 		};
 
 		INIT_LIST_HEAD(&node->lines);
@@ -126,7 +119,6 @@ int irq__register_device(u32 dev, u8 *pin, u8 *line)
 
 		new->line	= next_line++;
 		*line		= new->line;
-		*pin		= node->pin;
 
 		list_add(&new->node, &node->lines);
 
Signature · 1 line
1.8.2.2
#9 lore raw

PATCH 9/17 kvm tools: irq: replace the x86 irq rbtree with the PCI device tree

In reply to Will Deacon

The x86 code keeps its own rbtree of PCI devices in order to allocate interrupts. However, this functionality can be moved into the generic PCI device tree and be reused by other architectures.

This patch removes the x86 tree and reworks the ACPI mptable generation to use the PCI device tree for enumerating the bus.

---
 tools/kvm/include/kvm/irq.h |  14 ------
 tools/kvm/x86/irq.c         | 109 +-------------------------------------------
 tools/kvm/x86/mptable.c     |  35 ++++++--------
 3 files changed, 15 insertions(+), 143 deletions(-)
tools/kvm/include/kvm/irq.h +0 -14
index 20213c064b0b..7652b8587464 100644
@@ -10,22 +10,8 @@
 
 struct kvm;
 
-struct irq_line {
-	u8			line;
-	struct list_head	node;
-};
-
-struct pci_dev {
-	struct rb_node		node;
-	u32			id;
-	u8			pin;
-	struct list_head	lines;
-};
-
 int irq__register_device(u32 dev, u8 *line);
 
-struct rb_node *irq__get_pci_tree(void);
-
 int irq__init(struct kvm *kvm);
 int irq__exit(struct kvm *kvm);
 int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg);
tools/kvm/x86/irq.c +2 -107

133 lines not on this page · raw message

tools/kvm/x86/mptable.c +13 -22
index ea8c6e8c848f..a984de9eca9c 100644
@@ -3,7 +3,8 @@
 #include "kvm/apic.h"
 #include "kvm/mptable.h"
 #include "kvm/util.h"
-#include "kvm/irq.h"
+#include "kvm/devices.h"
+#include "kvm/pci.h"
 
 #include <linux/kernel.h>
 #include <string.h>
@@ -80,7 +81,7 @@ int mptable__init(struct kvm *kvm)
 	struct mpc_bus *mpc_bus;
 	struct mpc_ioapic *mpc_ioapic;
 	struct mpc_intsrc *mpc_intsrc;
-	struct rb_node *pci_tree;
+	struct device_header *dev_hdr;
 
 	const int pcibusid = 0;
 	const int isabusid = 1;
@@ -171,31 +172,21 @@ int mptable__init(struct kvm *kvm)
 
 	/*
 	 * IRQ sources.
-	 *
-	 * FIXME: Same issue as with buses. We definitely
-	 * need kind of collector routine which enumerate
-	 * resources used first and pass them here.
-	 * At moment we know we have only virtio block device
-	 * and virtio console but this is g00berfish.
-	 *
 	 * Also note we use PCI irqs here, no for ISA bus yet.
 	 */
 
-	for (pci_tree = irq__get_pci_tree(); pci_tree; pci_tree = rb_next(pci_tree)) {
-		struct pci_dev *dev = rb_entry(pci_tree, struct pci_dev, node);
-		struct irq_line *irq_line;
+	dev_hdr = device__first_dev(DEVICE_BUS_PCI);
+	while (dev_hdr) {
+		unsigned char srcbusirq;
+		struct pci_device_header *pci_hdr = dev_hdr->data;
 
-		list_for_each_entry(irq_line, &dev->lines, node) {
-			unsigned char srcbusirq;
+		srcbusirq = (pci_hdr->subsys_id << 2) | (pci_hdr->irq_pin - 1);
+		mpc_intsrc = last_addr;
+		mptable_add_irq_src(mpc_intsrc, pcibusid, srcbusirq, ioapicid, pci_hdr->irq_line);
 
-			srcbusirq = (dev->id << 2) | (dev->pin - 1);
-
-			mpc_intsrc = last_addr;
-
-			mptable_add_irq_src(mpc_intsrc, pcibusid, srcbusirq, ioapicid, irq_line->line);
-			last_addr = (void *)&mpc_intsrc[1];
-			nentries++;
-		}
+		last_addr = (void *)&mpc_intsrc[dev_hdr->dev_num];
+		nentries++;
+		dev_hdr = device__next_dev(dev_hdr);
 	}
 
 	/*
Signature · 1 line
1.8.2.2
#10 lore raw

PATCH 10/17 kvm tools: irq: remove remaining parameters to irq__register_device

In reply to Will Deacon

With the removal of the x86 irq rbtree, the only parameter used by irq__register_device is actually used to return the new line.

This patch removes all of the parameters from irq__register_device and returns the allocated line directly.

---
 tools/kvm/arm/irq.c         | 5 ++---
 tools/kvm/hw/pci-shmem.c    | 7 +------
 tools/kvm/hw/vesa.c         | 7 +------
 tools/kvm/include/kvm/irq.h | 2 +-
 tools/kvm/powerpc/irq.c     | 5 ++---
 tools/kvm/virtio/mmio.c     | 7 ++-----
 tools/kvm/virtio/pci.c      | 7 +------
 tools/kvm/x86/irq.c         | 5 ++---
 8 files changed, 12 insertions(+), 33 deletions(-)
tools/kvm/arm/irq.c +2 -3
index dd53f1216c51..39d4fbc23314 100644
@@ -4,10 +4,9 @@
 
 #include "arm-common/gic.h"
 
-int irq__register_device(u32 dev, u8 *line)
+int irq__register_device(void)
 {
-	*line = gic__alloc_irqnum();
-	return 0;
+	return gic__alloc_irqnum();
 }
 
 int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
tools/kvm/hw/pci-shmem.c +1 -6
index d0d88f821aba..c0c63223e6d9 100644
@@ -352,7 +352,6 @@ int shmem_parser(const struct option *opt, const char *arg, int unset)
 
 int pci_shmem__init(struct kvm *kvm)
 {
-	u8 line;
 	char *mem;
 	int r;
 
@@ -360,12 +359,8 @@ int pci_shmem__init(struct kvm *kvm)
 		return 0;
 
 	/* Register good old INTx */
-	r = irq__register_device(PCI_DEVICE_ID_PCI_SHMEM, &line);
-	if (r < 0)
-		return r;
-
 	pci_shmem_pci_device.irq_pin = 1;
-	pci_shmem_pci_device.irq_line = line;
+	pci_shmem_pci_device.irq_line = irq__register_device();
 
 	/* Register MMIO space for MSI-X */
 	r = ioport__register(kvm, IOPORT_EMPTY, &shmem_pci__io_ops, IOPORT_SIZE, NULL);
tools/kvm/hw/vesa.c +1 -6
index 07c3978c785e..f033ebed0790 100644
@@ -55,23 +55,18 @@ static struct framebuffer vesafb;
 struct framebuffer *vesa__init(struct kvm *kvm)
 {
 	u16 vesa_base_addr;
-	u8 line;
 	char *mem;
 	int r;
 
 	if (!kvm->cfg.vnc && !kvm->cfg.sdl && !kvm->cfg.gtk)
 		return NULL;
 
-	r = irq__register_device(PCI_DEVICE_ID_VESA, &line);
-	if (r < 0)
-		return ERR_PTR(r);
-
 	r = ioport__register(kvm, IOPORT_EMPTY, &vesa_io_ops, IOPORT_SIZE, NULL);
 	if (r < 0)
 		return ERR_PTR(r);
 
 	vesa_pci_device.irq_pin		= 1;
-	vesa_pci_device.irq_line	= line;
+	vesa_pci_device.irq_line	= irq__register_device();
 	vesa_base_addr			= (u16)r;
 	vesa_pci_device.bar[0]		= cpu_to_le32(vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO);
 	device__register(&vesa_device);
tools/kvm/include/kvm/irq.h +1 -1
index 7652b8587464..14cf574849b3 100644
@@ -10,7 +10,7 @@
 
 struct kvm;
 
-int irq__register_device(u32 dev, u8 *line);
+int irq__register_device(void);
 
 int irq__init(struct kvm *kvm);
 int irq__exit(struct kvm *kvm);
tools/kvm/powerpc/irq.c +2 -3
index d906bb2840bc..3436ae85027f 100644
@@ -32,14 +32,13 @@
  * generic & cope with multiple PPC platform types.
  */
 
-int irq__register_device(u32 dev, u8 *line)
+int irq__register_device(void)
 {
 	/*
 	 * Have I said how nasty I find this?  Line should be dontcare... PHB
 	 * should determine which CPU/XICS IRQ to fire.
 	 */
-	*line = xics_alloc_irqnum();
-	return 0;
+	return xics_alloc_irqnum();
 }
 
 int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
tools/kvm/virtio/mmio.c +2 -5
index 90f90ede3c1f..a8212bacd599 100644
@@ -260,7 +260,6 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		     int device_id, int subsys_id, int class)
 {
 	struct virtio_mmio *vmmio = vdev->virtio;
-	u8 line;
 
 	vmmio->addr	= virtio_mmio_get_io_space_block(VIRTIO_MMIO_IO_SIZE);
 	vmmio->kvm	= kvm;
@@ -277,9 +276,7 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.queue_num_max	= 256,
 	};
 
-	if (irq__register_device(subsys_id, &line) < 0)
-		return -1;
-	vmmio->irq = line;
+	vmmio->irq = irq__register_device();
 	vmmio->dev_hdr = (struct device_header) {
 		.bus_type	= DEVICE_BUS_MMIO,
 		.data		= generate_virtio_mmio_fdt_node,
@@ -293,7 +290,7 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	 *
 	 * virtio_mmio.devices=0x200@0xd2000000:5,0x200@0xd2000200:6
 	 */
-	pr_info("virtio-mmio.devices=0x%x@0x%x:%d\n", VIRTIO_MMIO_IO_SIZE, vmmio->addr, line);
+	pr_info("virtio-mmio.devices=0x%x@0x%x:%d\n", VIRTIO_MMIO_IO_SIZE, vmmio->addr, vmmio->irq);
 
 	return 0;
 }
tools/kvm/virtio/pci.c +1 -6
index 77dde3bee741..8005e17195d9 100644
@@ -335,7 +335,6 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		     int device_id, int subsys_id, int class)
 {
 	struct virtio_pci *vpci = vdev->virtio;
-	u8 line;
 	int r;
 
 	vpci->kvm = kvm;
@@ -406,15 +405,11 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(2 | PCI_IO_SIZE);
 	vpci->config_vector = 0;
 
-	r = irq__register_device(subsys_id, &line);
-	if (r < 0)
-		goto free_msix_mmio;
-
 	if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI))
 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
 
 	vpci->pci_hdr.irq_pin	= 1;
-	vpci->pci_hdr.irq_line	= line;
+	vpci->pci_hdr.irq_line	= irq__register_device();
 	r = device__register(&vpci->dev_hdr);
 	if (r < 0)
 		goto free_msix_mmio;
tools/kvm/x86/irq.c +2 -3
index ef5ec34aebff..2c7606998926 100644
@@ -39,10 +39,9 @@ static int irq__add_routing(u32 gsi, u32 type, u32 irqchip, u32 pin)
 	return 0;
 }
 
-int irq__register_device(u32 dev, u8 *line)
+int irq__register_device(void)
 {
-	*line = next_line++;
-	return 0;
+	return next_line++;
 }
 
 int irq__init(struct kvm *kvm)
Signature · 1 line
1.8.2.2
#11 lore raw

PATCH 11/17 kvm tools: irq: rename irq__register_device to irq__alloc_line

In reply to Will Deacon

Since irq__register_device no longer registers a device with anything, rename it to irq__alloc_line, which better describes what is actually going on.

---
 tools/kvm/arm/irq.c         | 2 +-
 tools/kvm/hw/pci-shmem.c    | 2 +-
 tools/kvm/hw/vesa.c         | 2 +-
 tools/kvm/include/kvm/irq.h | 2 +-
 tools/kvm/powerpc/irq.c     | 2 +-
 tools/kvm/powerpc/xics.c    | 4 ++--
 tools/kvm/virtio/mmio.c     | 2 +-
 tools/kvm/virtio/pci.c      | 2 +-
 tools/kvm/x86/irq.c         | 2 +-
 9 files changed, 10 insertions(+), 10 deletions(-)
tools/kvm/arm/irq.c +1 -1
index 39d4fbc23314..a39a2c289d27 100644
@@ -4,7 +4,7 @@
 
 #include "arm-common/gic.h"
 
-int irq__register_device(void)
+int irq__alloc_line(void)
 {
 	return gic__alloc_irqnum();
 }
tools/kvm/hw/pci-shmem.c +1 -1
index c0c63223e6d9..057118332981 100644
@@ -360,7 +360,7 @@ int pci_shmem__init(struct kvm *kvm)
 
 	/* Register good old INTx */
 	pci_shmem_pci_device.irq_pin = 1;
-	pci_shmem_pci_device.irq_line = irq__register_device();
+	pci_shmem_pci_device.irq_line = irq__alloc_line();
 
 	/* Register MMIO space for MSI-X */
 	r = ioport__register(kvm, IOPORT_EMPTY, &shmem_pci__io_ops, IOPORT_SIZE, NULL);
tools/kvm/hw/vesa.c +1 -1
index f033ebed0790..c7b8e66cc531 100644
@@ -66,7 +66,7 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 		return ERR_PTR(r);
 
 	vesa_pci_device.irq_pin		= 1;
-	vesa_pci_device.irq_line	= irq__register_device();
+	vesa_pci_device.irq_line	= irq__alloc_line();
 	vesa_base_addr			= (u16)r;
 	vesa_pci_device.bar[0]		= cpu_to_le32(vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO);
 	device__register(&vesa_device);
tools/kvm/include/kvm/irq.h +1 -1
index 14cf574849b3..4cec6f0f8447 100644
@@ -10,7 +10,7 @@
 
 struct kvm;
 
-int irq__register_device(void);
+int irq__alloc_line(void);
 
 int irq__init(struct kvm *kvm);
 int irq__exit(struct kvm *kvm);
tools/kvm/powerpc/irq.c +1 -1
index 3436ae85027f..c187d8f306f2 100644
@@ -32,7 +32,7 @@
  * generic & cope with multiple PPC platform types.
  */
 
-int irq__register_device(void)
+int irq__alloc_line(void)
 {
 	/*
 	 * Have I said how nasty I find this?  Line should be dontcare... PHB
tools/kvm/powerpc/xics.c +2 -2
index c1ef35bbe012..9dc27b8b7752 100644
@@ -278,14 +278,14 @@ static int allocated_irqnum = XICS_IRQ_OFFSET;
 /*
  * xics_alloc_irqnum(): This is hacky.  The problem boils down to the PCI device
  * code which just calls kvm__irq_line( .. pcidev->pci_hdr.irq_line ..) at will.
- * Each PCI device's IRQ line is allocated by irq__register_device() (which
+ * Each PCI device's IRQ line is allocated by irq__alloc_line() (which
  * allocates an IRQ AND allocates a.. PCI device num..).
  *
  * In future I'd like to at least mimic some kind of 'upstream IRQ controller'
  * whereby PCI devices let their PHB know when they want to IRQ, and that
  * percolates up.
  *
- * For now, allocate a REAL xics irq number and (via irq__register_device) push
+ * For now, allocate a REAL xics irq number and (via irq__alloc_line) push
  * that into the config space.	8 bits only though!
  */
 int xics_alloc_irqnum(void)
tools/kvm/virtio/mmio.c +1 -1
index a8212bacd599..1cb186b452e6 100644
@@ -276,7 +276,7 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.queue_num_max	= 256,
 	};
 
-	vmmio->irq = irq__register_device();
+	vmmio->irq = irq__alloc_line();
 	vmmio->dev_hdr = (struct device_header) {
 		.bus_type	= DEVICE_BUS_MMIO,
 		.data		= generate_virtio_mmio_fdt_node,
tools/kvm/virtio/pci.c +1 -1
index 8005e17195d9..dbd67580fb03 100644
@@ -409,7 +409,7 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
 
 	vpci->pci_hdr.irq_pin	= 1;
-	vpci->pci_hdr.irq_line	= irq__register_device();
+	vpci->pci_hdr.irq_line	= irq__alloc_line();
 	r = device__register(&vpci->dev_hdr);
 	if (r < 0)
 		goto free_msix_mmio;
tools/kvm/x86/irq.c +1 -1
index 2c7606998926..5bd6aa42987e 100644
@@ -39,7 +39,7 @@ static int irq__add_routing(u32 gsi, u32 type, u32 irqchip, u32 pin)
 	return 0;
 }
 
-int irq__register_device(void)
+int irq__alloc_line(void)
 {
 	return next_line++;
 }
Signature · 1 line
1.8.2.2
#12 lore raw

PATCH 12/17 kvm tools: irq: make irq__alloc_line generic

In reply to Will Deacon

All architectures are now doing the same thing for irq__alloc_line:

1. Initialise a global counter to some fixed offset
2. Return the current value of the counter and increment it

This is better off in core code, with each architecture specifying the initial offset, which is specific to the interrupt controller being used by the guest.

---
 tools/kvm/Makefile                          |  1 +
 tools/kvm/arm/gic.c                         | 12 ------------
 tools/kvm/arm/include/arm-common/kvm-arch.h |  4 ++++
 tools/kvm/arm/ioport.c                      |  5 ++---
 tools/kvm/arm/irq.c                         |  7 -------
 tools/kvm/irq.c                             |  9 +++++++++
 tools/kvm/powerpc/include/kvm/kvm-arch.h    |  2 ++
 tools/kvm/powerpc/irq.c                     | 17 -----------------
 tools/kvm/powerpc/xics.c                    | 28 ++--------------------------
 tools/kvm/x86/include/kvm/kvm-arch.h        |  2 ++
 tools/kvm/x86/irq.c                         |  7 -------
 11 files changed, 22 insertions(+), 72 deletions(-)
 create mode 100644 tools/kvm/irq.c
tools/kvm/Makefile +1 -0
index 27fb2fbeae8d..29f18220c749 100644
@@ -57,6 +57,7 @@ OBJS	+= guest_compat.o
 OBJS	+= hw/rtc.o
 OBJS	+= hw/serial.o
 OBJS	+= ioport.o
+OBJS	+= irq.o
 OBJS	+= kvm-cpu.o
 OBJS	+= kvm.o
 OBJS	+= main.o
tools/kvm/arm/gic.c +0 -12
index 8d2ff873c2de..5d8cbe64e628 100644
@@ -7,18 +7,6 @@
 #include <linux/byteorder.h>
 #include <linux/kvm.h>
 
-static int irq_ids;
-
-int gic__alloc_irqnum(void)
-{
-	int irq = GIC_SPI_IRQ_BASE + irq_ids++;
-
-	if (irq > GIC_MAX_IRQ)
-		die("GIC IRQ limit %d reached!", GIC_MAX_IRQ);
-
-	return irq;
-}
-
 int gic__init_irqchip(struct kvm *kvm)
 {
 	int err;
tools/kvm/arm/include/arm-common/kvm-arch.h +4 -0
index caef590a44ff..348e88d00b1c 100644
@@ -5,6 +5,8 @@
 #include <linux/const.h>
 #include <linux/types.h>
 
+#include "arm-common/gic.h"
+
 #define ARM_IOPORT_AREA		_AC(0x0000000000000000, UL)
 #define ARM_MMIO_AREA		_AC(0x0000000000010000, UL)
 #define ARM_AXI_AREA		_AC(0x0000000040000000, UL)
@@ -28,6 +30,8 @@
 #define KVM_PCI_MMIO_AREA	(KVM_PCI_CFG_AREA + ARM_PCI_CFG_SIZE)
 #define KVM_VIRTIO_MMIO_AREA	ARM_MMIO_AREA
 
+#define KVM_IRQ_OFFSET		GIC_SPI_IRQ_BASE
+
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_MMIO
 
 static inline bool arm_addr_in_ioport_region(u64 phys_addr)
tools/kvm/arm/ioport.c +2 -3
index 74ee10ec16ca..bdd30b6fe812 100644
@@ -1,6 +1,5 @@
 #include "kvm/ioport.h"
-
-#include "arm-common/gic.h"
+#include "kvm/irq.h"
 
 void ioport__setup_arch(struct kvm *kvm)
 {
@@ -8,5 +7,5 @@ void ioport__setup_arch(struct kvm *kvm)
 
 void ioport__map_irq(u8 *irq)
 {
-	*irq = gic__alloc_irqnum();
+	*irq = irq__alloc_line();
 }
tools/kvm/arm/irq.c +0 -7
index a39a2c289d27..d8f44dfbfec4 100644
@@ -2,13 +2,6 @@
 #include "kvm/kvm.h"
 #include "kvm/util.h"
 
-#include "arm-common/gic.h"
-
-int irq__alloc_line(void)
-{
-	return gic__alloc_irqnum();
-}
-
 int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
 {
 	die(__FUNCTION__);
tools/kvm/irq.c new file +9 -0
new file mode 100644
index 000000000000..33ea8d22e285
@@ -0,0 +1,9 @@
+#include "kvm/irq.h"
+#include "kvm/kvm-arch.h"
+
+static u8 next_line = KVM_IRQ_OFFSET;
+
+int irq__alloc_line(void)
+{
+	return next_line++;
+}
tools/kvm/powerpc/include/kvm/kvm-arch.h +2 -0
index c147c78b71e7..d2b52bf0f617 100644
@@ -42,6 +42,8 @@
 #define KVM_PCI_MMIO_AREA		0x2000000
 #define KVM_VIRTIO_MMIO_AREA		0x3000000
 
+#define KVM_IRQ_OFFSET			16
+
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
 
 struct spapr_phb;
tools/kvm/powerpc/irq.c +0 -17
index c187d8f306f2..03f2fe792365 100644
@@ -24,23 +24,6 @@
 
 #include "kvm/pci.h"
 
-#include "xics.h"
-#include "spapr_pci.h"
-
-/*
- * FIXME: The code in this file assumes an SPAPR guest, using XICS.  Make
- * generic & cope with multiple PPC platform types.
- */
-
-int irq__alloc_line(void)
-{
-	/*
-	 * Have I said how nasty I find this?  Line should be dontcare... PHB
-	 * should determine which CPU/XICS IRQ to fire.
-	 */
-	return xics_alloc_irqnum();
-}
-
 int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
 {
 	die(__FUNCTION__);
tools/kvm/powerpc/xics.c +2 -26
index 9dc27b8b7752..071b928a7ada 100644
@@ -14,6 +14,7 @@
 #include "spapr.h"
 #include "xics.h"
 #include "kvm/util.h"
+#include "kvm/kvm.h"
 
 #include <stdio.h>
 #include <malloc.h>
@@ -41,7 +42,7 @@ struct icp_server_state {
 	struct kvm_cpu *cpu;
 };
 
-#define XICS_IRQ_OFFSET 16
+#define XICS_IRQ_OFFSET KVM_IRQ_OFFSET
 #define XISR_MASK	0x00ffffff
 #define CPPR_MASK	0xff000000
 
@@ -273,31 +274,6 @@ static void ics_eoi(struct ics_state *ics, int nr)
  * Exported functions
  */
 
-static int allocated_irqnum = XICS_IRQ_OFFSET;
-
-/*
- * xics_alloc_irqnum(): This is hacky.  The problem boils down to the PCI device
- * code which just calls kvm__irq_line( .. pcidev->pci_hdr.irq_line ..) at will.
- * Each PCI device's IRQ line is allocated by irq__alloc_line() (which
- * allocates an IRQ AND allocates a.. PCI device num..).
- *
- * In future I'd like to at least mimic some kind of 'upstream IRQ controller'
- * whereby PCI devices let their PHB know when they want to IRQ, and that
- * percolates up.
- *
- * For now, allocate a REAL xics irq number and (via irq__alloc_line) push
- * that into the config space.	8 bits only though!
- */
-int xics_alloc_irqnum(void)
-{
-	int irq = allocated_irqnum++;
-
-	if (irq > 255)
-		die("Huge numbers of IRQs aren't supported with the daft kvmtool IRQ system.");
-
-	return irq;
-}
-
 static target_ulong h_cppr(struct kvm_cpu *vcpu,
 			   target_ulong opcode, target_ulong *args)
 {
tools/kvm/x86/include/kvm/kvm-arch.h +2 -0
index 8e8389627354..29953a761aa4 100644
@@ -25,6 +25,8 @@
 #define KVM_PCI_MMIO_AREA	(KVM_MMIO_START + 0x2000000)
 #define KVM_VIRTIO_MMIO_AREA	(KVM_MMIO_START + 0x3000000)
 
+#define KVM_IRQ_OFFSET		5
+
 #define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
 
 struct kvm_arch {
tools/kvm/x86/irq.c +0 -7
index 5bd6aa42987e..72177e7dfd91 100644
@@ -16,8 +16,6 @@
 #define IRQCHIP_SLAVE			1
 #define IRQCHIP_IOAPIC			2
 
-static u8		next_line	= 5;
-
 /* First 24 GSIs are routed between IRQCHIPs and IOAPICs */
 static u32 gsi = 24;
 
@@ -39,11 +37,6 @@ static int irq__add_routing(u32 gsi, u32 type, u32 irqchip, u32 pin)
 	return 0;
 }
 
-int irq__alloc_line(void)
-{
-	return next_line++;
-}
-
 int irq__init(struct kvm *kvm)
 {
 	int i, r;
Signature · 1 line
1.8.2.2
#13 lore raw

PATCH 13/17 kvm tools: irq: move irq line allocation into device registration

In reply to Will Deacon

For the MMIO and PCI buses, drivers typically allocate an IRQ line for their device before registering the device with the device tree for the relevant bus.

This patch moves the IRQ allocation into the bus code, which is then called directly by the device tree when a new device is registered. IOPORT devices, however, tend to use hardcoded IRQs for legacy reasons, so they are still required to deal with their interrupts (which also require remapping for non-x86 architectures).

---
 tools/kvm/devices.c                 | 13 +++++++++++++
 tools/kvm/hw/pci-shmem.c            |  4 ----
 tools/kvm/hw/vesa.c                 |  2 --
 tools/kvm/include/kvm/pci.h         |  2 ++
 tools/kvm/include/kvm/virtio-mmio.h |  1 +
 tools/kvm/pci.c                     | 15 +++++++++++++++
 tools/kvm/virtio/mmio.c             |  9 ++++++++-
 tools/kvm/virtio/pci.c              |  2 --
 8 files changed, 39 insertions(+), 9 deletions(-)
tools/kvm/devices.c +13 -0
index 5b627e9adee2..b560a59944e0 100644
@@ -1,5 +1,7 @@
 #include "kvm/devices.h"
 #include "kvm/kvm.h"
+#include "kvm/pci.h"
+#include "kvm/virtio-mmio.h"
 
 #include <linux/err.h>
 #include <linux/rbtree.h>
@@ -27,6 +29,17 @@ int device__register(struct device_header *dev)
 	bus = &device_trees[dev->bus_type];
 	dev->dev_num = bus->dev_num++;
 
+	switch (dev->bus_type) {
+	case DEVICE_BUS_PCI:
+		pci__assign_irq(dev);
+		break;
+	case DEVICE_BUS_MMIO:
+		virtio_mmio_assign_irq(dev);
+		break;
+	default:
+		break;
+	}
+
 	node = &bus->root.rb_node;
 	while (*node) {
 		int num = rb_entry(*node, struct device_header, node)->dev_num;
tools/kvm/hw/pci-shmem.c +0 -4
index 057118332981..34de74770ea9 100644
@@ -358,10 +358,6 @@ int pci_shmem__init(struct kvm *kvm)
 	if (shmem_region == NULL)
 		return 0;
 
-	/* Register good old INTx */
-	pci_shmem_pci_device.irq_pin = 1;
-	pci_shmem_pci_device.irq_line = irq__alloc_line();
-
 	/* Register MMIO space for MSI-X */
 	r = ioport__register(kvm, IOPORT_EMPTY, &shmem_pci__io_ops, IOPORT_SIZE, NULL);
 	if (r < 0)
tools/kvm/hw/vesa.c +0 -2
index c7b8e66cc531..a0b15a77998b 100644
@@ -65,8 +65,6 @@ struct framebuffer *vesa__init(struct kvm *kvm)
 	if (r < 0)
 		return ERR_PTR(r);
 
-	vesa_pci_device.irq_pin		= 1;
-	vesa_pci_device.irq_line	= irq__alloc_line();
 	vesa_base_addr			= (u16)r;
 	vesa_pci_device.bar[0]		= cpu_to_le32(vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO);
 	device__register(&vesa_device);
tools/kvm/include/kvm/pci.h +2 -0
index e1e621d51470..b0c28a10af6a 100644
@@ -6,6 +6,7 @@
 #include <linux/pci_regs.h>
 #include <endian.h>
 
+#include "kvm/devices.h"
 #include "kvm/kvm.h"
 #include "kvm/msi.h"
 
@@ -88,6 +89,7 @@ int pci__init(struct kvm *kvm);
 int pci__exit(struct kvm *kvm);
 struct pci_device_header *pci__find_dev(u8 dev_num);
 u32 pci_get_io_space_block(u32 size);
+void pci__assign_irq(struct device_header *dev_hdr);
 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);
 
tools/kvm/include/kvm/virtio-mmio.h +1 -0
index 4d6a671c79c5..835f421bcb8d 100644
@@ -56,4 +56,5 @@ int virtio_mmio_signal_config(struct kvm *kvm, struct virtio_device *vdev);
 int virtio_mmio_exit(struct kvm *kvm, struct virtio_device *vdev);
 int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		      int device_id, int subsys_id, int class);
+void virtio_mmio_assign_irq(struct device_header *dev_hdr);
 #endif
tools/kvm/pci.c +15 -0
index c4442c85b5cf..c2da15252019 100644
@@ -1,6 +1,7 @@
 #include "kvm/devices.h"
 #include "kvm/pci.h"
 #include "kvm/ioport.h"
+#include "kvm/irq.h"
 #include "kvm/util.h"
 #include "kvm/kvm.h"
 
@@ -28,6 +29,20 @@ u32 pci_get_io_space_block(u32 size)
 	return block;
 }
 
+void pci__assign_irq(struct device_header *dev_hdr)
+{
+	struct pci_device_header *pci_hdr = dev_hdr->data;
+
+	/*
+	 * PCI supports only INTA#,B#,C#,D# per device.
+	 *
+	 * A#,B#,C#,D# are allowed for multifunctional devices so stick
+	 * with A# for our single function devices.
+	 */
+	pci_hdr->irq_pin	= 1;
+	pci_hdr->irq_line	= irq__alloc_line();
+}
+
 static void *pci_config_address_ptr(u16 port)
 {
 	unsigned long offset;
tools/kvm/virtio/mmio.c +8 -1
index 1cb186b452e6..e1fca2b05546 100644
@@ -256,6 +256,14 @@ static void generate_virtio_mmio_fdt_node(void *fdt,
 }
 #endif
 
+void virtio_mmio_assign_irq(struct device_header *dev_hdr)
+{
+	struct virtio_mmio *vmmio = container_of(dev_hdr,
+						 struct virtio_mmio,
+						 dev_hdr);
+	vmmio->irq = irq__alloc_line();
+}
+
 int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		     int device_id, int subsys_id, int class)
 {
@@ -276,7 +284,6 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		.queue_num_max	= 256,
 	};
 
-	vmmio->irq = irq__alloc_line();
 	vmmio->dev_hdr = (struct device_header) {
 		.bus_type	= DEVICE_BUS_MMIO,
 		.data		= generate_virtio_mmio_fdt_node,
tools/kvm/virtio/pci.c +0 -2
index dbd67580fb03..fa7aa0021a29 100644
@@ -408,8 +408,6 @@ int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 	if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI))
 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
 
-	vpci->pci_hdr.irq_pin	= 1;
-	vpci->pci_hdr.irq_line	= irq__alloc_line();
 	r = device__register(&vpci->dev_hdr);
 	if (r < 0)
 		goto free_msix_mmio;
Signature · 1 line
1.8.2.2
#14 lore raw

PATCH 14/17 kvm tools: ARM: route guest PCI accesses to the emulation layer

In reply to Will Deacon

This patch routes guest PCI accesses to kvm__emulate_mmio, rather than exiting lkvm via a die invocation. The guest command-line is also updated to prevent the guest from attempting to program the BARs with new addresses (i.e. probe-only).

---
 tools/kvm/arm/include/arm-common/kvm-arch.h | 6 +++---
 tools/kvm/arm/kvm-cpu.c                     | 4 ++--
 tools/kvm/arm/kvm.c                         | 5 +++++
 3 files changed, 10 insertions(+), 5 deletions(-)
tools/kvm/arm/include/arm-common/kvm-arch.h +3 -3
index 348e88d00b1c..8adfcd495c03 100644
@@ -46,10 +46,10 @@ static inline bool arm_addr_in_virtio_mmio_region(u64 phys_addr)
 	return phys_addr >= KVM_VIRTIO_MMIO_AREA && phys_addr < limit;
 }
 
-static inline bool arm_addr_in_pci_mmio_region(u64 phys_addr)
+static inline bool arm_addr_in_pci_region(u64 phys_addr)
 {
-	u64 limit = KVM_PCI_MMIO_AREA + ARM_PCI_MMIO_SIZE;
-	return phys_addr >= KVM_PCI_MMIO_AREA && phys_addr < limit;
+	u64 limit = KVM_PCI_CFG_AREA + ARM_PCI_CFG_SIZE + ARM_PCI_MMIO_SIZE;
+	return phys_addr >= KVM_PCI_CFG_AREA && phys_addr < limit;
 }
 
 struct kvm_arch {
tools/kvm/arm/kvm-cpu.c +2 -2
index d31e7b154ae8..b017994f4014 100644
@@ -107,8 +107,8 @@ bool kvm_cpu__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len,
 		int direction = is_write ? KVM_EXIT_IO_OUT : KVM_EXIT_IO_IN;
 		u16 port = phys_addr & USHRT_MAX;
 		return kvm__emulate_io(kvm, port, data, direction, len, 1);
-	} else if (arm_addr_in_pci_mmio_region(phys_addr)) {
-		die("PCI emulation not supported on ARM!");
+	} else if (arm_addr_in_pci_region(phys_addr)) {
+		return kvm__emulate_mmio(kvm, phys_addr, data, len, is_write);
 	}
 
 	return false;
tools/kvm/arm/kvm.c +5 -0
index 008b7fe75d7d..6db646b4f4fd 100644
@@ -56,6 +56,11 @@ void kvm__arch_read_term(struct kvm *kvm)
 
 void kvm__arch_set_cmdline(char *cmdline, bool video)
 {
+	/*
+	 * We don't support movable BARs, so force the guest to use what
+	 * we tell it.
+	 */
+	strcpy(cmdline, "pci=firmware");
 }
 
 void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
Signature · 1 line
1.8.2.2
#15 lore raw

PATCH 15/17 kvm tools: ARM: generate an fdt node for our PCI emulation

In reply to Will Deacon

This patch factors out some of the openfirmware PCI binding definitions from the powerpc spapr PCI code and uses them to generate appropriate FDT properties for the PCI node on ARM.

---
 tools/kvm/Makefile                     |   2 +-
 tools/kvm/arm/fdt.c                    |   4 ++
 tools/kvm/arm/include/arm-common/pci.h |   6 ++
 tools/kvm/arm/pci.c                    | 118 +++++++++++++++++++++++++++++++++
 tools/kvm/include/kvm/of_pci.h         |  44 ++++++++++++
 5 files changed, 173 insertions(+), 1 deletion(-)
 create mode 100644 tools/kvm/arm/include/arm-common/pci.h
 create mode 100644 tools/kvm/arm/pci.c
 create mode 100644 tools/kvm/include/kvm/of_pci.h
tools/kvm/Makefile +1 -1
index 29f18220c749..b87265189d1f 100644
@@ -158,7 +158,7 @@ endif
 
 # ARM
 OBJS_ARM_COMMON		:= arm/fdt.o arm/gic.o arm/ioport.o arm/irq.o \
-			   arm/kvm.o arm/kvm-cpu.o arm/timer.o
+			   arm/kvm.o arm/kvm-cpu.o arm/pci.o arm/timer.o
 HDRS_ARM_COMMON		:= arm/include
 ifeq ($(ARCH), arm)
 	DEFINES		+= -DCONFIG_ARM
tools/kvm/arm/fdt.c +4 -0
index 9a34d9804930..d90fc9e1734f 100644
@@ -5,6 +5,7 @@
 #include "kvm/virtio-mmio.h"
 
 #include "arm-common/gic.h"
+#include "arm-common/pci.h"
 
 #include <stdbool.h>
 
@@ -155,6 +156,9 @@ static int setup_fdt(struct kvm *kvm)
 		dev_hdr = device__next_dev(dev_hdr);
 	}
 
+	/* PCI host controller */
+	pci__generate_fdt_nodes(fdt, gic_phandle);
+
 	/* PSCI firmware */
 	_FDT(fdt_begin_node(fdt, "psci"));
 	_FDT(fdt_property_string(fdt, "compatible", "arm,psci"));
tools/kvm/arm/include/arm-common/pci.h new file +6 -0
new file mode 100644
index 000000000000..ee87725ee82f
@@ -0,0 +1,6 @@
+#ifndef ARM_COMMON__PCI_H
+#define ARM_COMMON__PCI_H
+
+void pci__generate_fdt_nodes(void *fdt, u32 gic_phandle);
+
+#endif /* ARM_COMMON__PCI_H */
tools/kvm/arm/pci.c new file +118 -0

118 lines not on this page · raw message

tools/kvm/include/kvm/of_pci.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
1.8.2.2
#16 lore raw

PATCH 16/17 kvm tools: powerpc: make use of common of_pci.h header definitions

In reply to Will Deacon

Now that we have some common OF PCI definitions in of_pci.h, make use of them when generating the devicetree for spapr_pci on ppc.

---
 tools/kvm/powerpc/spapr_pci.c | 114 ++++++++++++++++++------------------------
 1 file changed, 48 insertions(+), 66 deletions(-)
tools/kvm/powerpc/spapr_pci.c +48 -66
index ed4b9ab52a7c..768e3f2df648 100644
@@ -18,6 +18,7 @@
 #include "kvm/devices.h"
 #include "kvm/fdt.h"
 #include "kvm/util.h"
+#include "kvm/of_pci.h"
 #include "kvm/pci.h"
 
 #include <linux/pci_regs.h>
@@ -42,26 +43,8 @@ static const uint32_t bars[] = {
 
 #define PCI_NUM_REGIONS		7
 
-/* Macros to operate with address in OF binding to PCI */
-#define b_x(x, p, l)	(((x) & ((1<<(l))-1)) << (p))
-#define b_n(x)		b_x((x), 31, 1) /* 0 if relocatable */
-#define b_p(x)		b_x((x), 30, 1) /* 1 if prefetchable */
-#define b_t(x)		b_x((x), 29, 1) /* 1 if the address is aliased */
-#define b_ss(x)		b_x((x), 24, 2) /* the space code */
-#define b_bbbbbbbb(x)	b_x((x), 16, 8) /* bus number */
-#define b_ddddd(x)	b_x((x), 11, 5) /* device number */
-#define b_fff(x)	b_x((x), 8, 3)	/* function number */
-#define b_rrrrrrrr(x)	b_x((x), 0, 8)	/* register number */
-
-#define SS_M64		3
-#define SS_M32		2
-#define SS_IO		1
-#define SS_CONFIG	0
-
-
 static struct spapr_phb phb;
 
-
 static void rtas_ibm_read_pci_config(struct kvm_cpu *vcpu,
 				     uint32_t token, uint32_t nargs,
 				     target_ulong args,
@@ -228,11 +211,11 @@ static uint32_t bar_to_ss(unsigned long bar)
 {
 	if ((bar & PCI_BASE_ADDRESS_SPACE) ==
 	    PCI_BASE_ADDRESS_SPACE_IO)
-		return SS_IO;
+		return OF_PCI_SS_IO;
 	else if (bar & PCI_BASE_ADDRESS_MEM_TYPE_64)
-		return SS_M64;
+		return OF_PCI_SS_M64;
 	else
-		return SS_M32;
+		return OF_PCI_SS_M32;
 }
 
 static unsigned long bar_to_addr(unsigned long bar)
@@ -251,33 +234,32 @@ int spapr_populate_pci_devices(struct kvm *kvm,
 	int bus_off, node_off = 0, devid, fn, i, n, devices;
 	struct device_header *dev_hdr;
 	char nodename[256];
-	struct {
-		uint32_t hi;
-		uint64_t addr;
-		uint64_t size;
-	} __attribute__((packed)) reg[PCI_NUM_REGIONS + 1],
-		  assigned_addresses[PCI_NUM_REGIONS];
+	struct of_pci_unit_address reg[PCI_NUM_REGIONS + 1],
+				   assigned_addresses[PCI_NUM_REGIONS];
 	uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
-	struct {
-		uint32_t hi;
-		uint64_t child;
-		uint64_t parent;
-		uint64_t size;
-	} __attribute__((packed)) ranges[] = {
+	struct of_pci_ranges_entry ranges[] = {
 		{
-			cpu_to_be32(b_ss(1)), cpu_to_be64(0),
+			{
+				cpu_to_be32(of_pci_b_ss(1)),
+				cpu_to_be32(0),
+				cpu_to_be32(0),
+			},
 			cpu_to_be64(phb.io_addr),
 			cpu_to_be64(phb.io_size),
 		},
 		{
-			cpu_to_be32(b_ss(2)), cpu_to_be64(0),
+			{
+				cpu_to_be32(of_pci_b_ss(2)),
+				cpu_to_be32(0),
+				cpu_to_be32(0),
+			},
 			cpu_to_be64(phb.mem_addr),
 			cpu_to_be64(phb.mem_size),
 		},
 	};
 	uint64_t bus_reg[] = { cpu_to_be64(phb.buid), 0 };
 	uint32_t interrupt_map_mask[] = {
-		cpu_to_be32(b_ddddd(-1)|b_fff(-1)), 0x0, 0x0, 0x0};
+		cpu_to_be32(of_pci_b_ddddd(-1)|of_pci_b_fff(-1)), 0x0, 0x0, 0x0};
 	uint32_t interrupt_map[SPAPR_PCI_NUM_LSI][7];
 
 	/* Start populating the FDT */
@@ -321,7 +303,7 @@ int spapr_populate_pci_devices(struct kvm *kvm,
 			die("Unexpected behaviour in spapr_populate_pci_devices,"
 			    "wrong devid %u\n", devid);
 		}
-		irqmap[0] = cpu_to_be32(b_ddddd(devid)|b_fff(fn));
+		irqmap[0] = cpu_to_be32(of_pci_b_ddddd(devid)|of_pci_b_fff(fn));
 		irqmap[1] = 0;
 		irqmap[2] = 0;
 		irqmap[3] = 0;
@@ -358,15 +340,15 @@ int spapr_populate_pci_devices(struct kvm *kvm,
 
 		/* Config space region comes first */
 		reg[0].hi = cpu_to_be32(
-			b_n(0) |
-			b_p(0) |
-			b_t(0) |
-			b_ss(SS_CONFIG) |
-			b_bbbbbbbb(0) |
-			b_ddddd(devid) |
-			b_fff(fn));
-		reg[0].addr = 0;
-		reg[0].size = 0;
+			of_pci_b_n(0) |
+			of_pci_b_p(0) |
+			of_pci_b_t(0) |
+			of_pci_b_ss(OF_PCI_SS_CONFIG) |
+			of_pci_b_bbbbbbbb(0) |
+			of_pci_b_ddddd(devid) |
+			of_pci_b_fff(fn));
+		reg[0].mid = 0;
+		reg[0].lo = 0;
 
 		n = 0;
 		/* Six BARs, no ROM supported, addresses are 32bit */
@@ -376,33 +358,33 @@ int spapr_populate_pci_devices(struct kvm *kvm,
 			}
 
 			reg[n+1].hi = cpu_to_be32(
-				b_n(0) |
-				b_p(0) |
-				b_t(0) |
-				b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) |
-				b_bbbbbbbb(0) |
-				b_ddddd(devid) |
-				b_fff(fn) |
-				b_rrrrrrrr(bars[i]));
-			reg[n+1].addr = 0;
-			reg[n+1].size = cpu_to_be64(hdr->bar_size[i]);
+				of_pci_b_n(0) |
+				of_pci_b_p(0) |
+				of_pci_b_t(0) |
+				of_pci_b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) |
+				of_pci_b_bbbbbbbb(0) |
+				of_pci_b_ddddd(devid) |
+				of_pci_b_fff(fn) |
+				of_pci_b_rrrrrrrr(bars[i]));
+			reg[n+1].mid = 0;
+			reg[n+1].lo = cpu_to_be64(hdr->bar_size[i]);
 
 			assigned_addresses[n].hi = cpu_to_be32(
-				b_n(1) |
-				b_p(0) |
-				b_t(0) |
-				b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) |
-				b_bbbbbbbb(0) |
-				b_ddddd(devid) |
-				b_fff(fn) |
-				b_rrrrrrrr(bars[i]));
+				of_pci_b_n(1) |
+				of_pci_b_p(0) |
+				of_pci_b_t(0) |
+				of_pci_b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) |
+				of_pci_b_bbbbbbbb(0) |
+				of_pci_b_ddddd(devid) |
+				of_pci_b_fff(fn) |
+				of_pci_b_rrrrrrrr(bars[i]));
 
 			/*
 			 * Writing zeroes to assigned_addresses causes the guest kernel to
 			 * reassign BARs
 			 */
-			assigned_addresses[n].addr = cpu_to_be64(bar_to_addr(le32_to_cpu(hdr->bar[i])));
-			assigned_addresses[n].size = reg[n+1].size;
+			assigned_addresses[n].mid = cpu_to_be64(bar_to_addr(le32_to_cpu(hdr->bar[i])));
+			assigned_addresses[n].lo = reg[n+1].lo;
 
 			++n;
 		}
Signature · 1 line
1.8.2.2
#17 lore raw

PATCH 17/17 kvm tools: ARM: allow default virtio transport to be passed on cmdline

In reply to Will Deacon

This patch changes VIRTIO_DEFAULT_TRANS to take a struct kvm parameter, allowing architectures to choose the default transport dynamically.

For ARM, this is driven by an arch-specific cmdline option.

---
 tools/kvm/arm/include/arm-common/kvm-arch.h        |  3 ++-
 tools/kvm/arm/include/arm-common/kvm-config-arch.h | 12 ++++++++----
 tools/kvm/powerpc/include/kvm/kvm-arch.h           |  2 +-
 tools/kvm/virtio/9p.c                              |  2 +-
 tools/kvm/virtio/balloon.c                         |  2 +-
 tools/kvm/virtio/blk.c                             |  2 +-
 tools/kvm/virtio/console.c                         |  2 +-
 tools/kvm/virtio/rng.c                             |  2 +-
 tools/kvm/virtio/scsi.c                            |  2 +-
 tools/kvm/x86/include/kvm/kvm-arch.h               |  2 +-
 10 files changed, 18 insertions(+), 13 deletions(-)
tools/kvm/arm/include/arm-common/kvm-arch.h +2 -1
index 8adfcd495c03..b6c4bf8de364 100644
@@ -32,7 +32,8 @@
 
 #define KVM_IRQ_OFFSET		GIC_SPI_IRQ_BASE
 
-#define VIRTIO_DEFAULT_TRANS	VIRTIO_MMIO
+#define VIRTIO_DEFAULT_TRANS(kvm)	\
+	((kvm)->cfg.arch.virtio_trans_pci ? VIRTIO_PCI : VIRTIO_MMIO)
 
 static inline bool arm_addr_in_ioport_region(u64 phys_addr)
 {
tools/kvm/arm/include/arm-common/kvm-config-arch.h +8 -4
index f3baf392704d..a8ebd94a78f1 100644
@@ -4,9 +4,10 @@
 #include "kvm/parse-options.h"
 
 struct kvm_config_arch {
-	const char *dump_dtb_filename;
-	unsigned int force_cntfrq;
-	bool aarch32_guest;
+	const char	*dump_dtb_filename;
+	unsigned int	force_cntfrq;
+	bool		virtio_trans_pci;
+	bool		aarch32_guest;
 };
 
 #define OPT_ARCH_RUN(pfx, cfg)							\
@@ -17,6 +18,9 @@ struct kvm_config_arch {
 	OPT_UINTEGER('\0', "override-bad-firmware-cntfrq", &(cfg)->force_cntfrq,\
 		     "Specify Generic Timer frequency in guest DT to "		\
 		     "work around buggy secure firmware *Firmware should be "	\
-		     "updated to program CNTFRQ correctly*"),
+		     "updated to program CNTFRQ correctly*"),			\
+	OPT_BOOLEAN('\0', "force-pci", &(cfg)->virtio_trans_pci,		\
+		    "Force virtio devices to use PCI as their default "		\
+		    "transport"),
 
 #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */
tools/kvm/powerpc/include/kvm/kvm-arch.h +1 -1
index d2b52bf0f617..f8627a2a2dd3 100644
@@ -44,7 +44,7 @@
 
 #define KVM_IRQ_OFFSET			16
 
-#define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
+#define VIRTIO_DEFAULT_TRANS(kvm)	VIRTIO_PCI
 
 struct spapr_phb;
 
tools/kvm/virtio/9p.c +1 -1
index 127b13afdf0b..25cdd8c2e70b 100644
@@ -1392,7 +1392,7 @@ int virtio_9p__init(struct kvm *kvm)
 
 	list_for_each_entry(p9dev, &devs, list) {
 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
-			    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_9P,
+			    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_9P,
 			    VIRTIO_ID_9P, PCI_CLASS_9P);
 	}
 
tools/kvm/virtio/balloon.c +1 -1
index 486353584904..f7dfb0be2875 100644
@@ -262,7 +262,7 @@ int virtio_bln__init(struct kvm *kvm)
 	memset(&bdev.config, 0, sizeof(struct virtio_balloon_config));
 
 	virtio_init(kvm, &bdev, &bdev.vdev, &bln_dev_virtio_ops,
-		    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_BLN,
+		    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_BLN,
 		    VIRTIO_ID_BALLOON, PCI_CLASS_BLN);
 
 	if (compat_id == -1)
tools/kvm/virtio/blk.c +1 -1
index ab1871673d46..4bed3a975d1d 100644
@@ -260,7 +260,7 @@ static int virtio_blk__init_one(struct kvm *kvm, struct disk_image *disk)
 	};
 
 	virtio_init(kvm, bdev, &bdev->vdev, &blk_dev_virtio_ops,
-		    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_BLK,
+		    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_BLK,
 		    VIRTIO_ID_BLOCK, PCI_CLASS_BLK);
 
 	list_add_tail(&bdev->list, &bdevs);
tools/kvm/virtio/console.c +1 -1
index f982dab702e3..0474e2b0e668 100644
@@ -210,7 +210,7 @@ int virtio_console__init(struct kvm *kvm)
 	pthread_cond_init(&cdev.poll_cond, NULL);
 
 	virtio_init(kvm, &cdev, &cdev.vdev, &con_dev_virtio_ops,
-		    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_CONSOLE,
+		    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_CONSOLE,
 		    VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE);
 	if (compat_id == -1)
 		compat_id = virtio_compat_add_message("virtio-console", "CONFIG_VIRTIO_CONSOLE");
tools/kvm/virtio/rng.c +1 -1
index 394de710838d..8031368a7e73 100644
@@ -171,7 +171,7 @@ int virtio_rng__init(struct kvm *kvm)
 	}
 
 	r = virtio_init(kvm, rdev, &rdev->vdev, &rng_dev_virtio_ops,
-			VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_RNG,
+			VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_RNG,
 			VIRTIO_ID_RNG, PCI_CLASS_RNG);
 	if (r < 0)
 		goto cleanup;
tools/kvm/virtio/scsi.c +1 -1
index 8da24209eb90..be254f30e61c 100644
@@ -252,7 +252,7 @@ static int virtio_scsi_init_one(struct kvm *kvm, struct disk_image *disk)
 	sdev->target.vhost_tpgt = strtol(disk->tpgt, NULL, 0);
 
 	virtio_init(kvm, sdev, &sdev->vdev, &scsi_dev_virtio_ops,
-		    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_SCSI,
+		    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_SCSI,
 		    VIRTIO_ID_SCSI, PCI_CLASS_BLK);
 
 	list_add_tail(&sdev->list, &sdevs);
tools/kvm/x86/include/kvm/kvm-arch.h +1 -1
index 29953a761aa4..a9f23b81f87e 100644
@@ -27,7 +27,7 @@
 
 #define KVM_IRQ_OFFSET		5
 
-#define VIRTIO_DEFAULT_TRANS	VIRTIO_PCI
+#define VIRTIO_DEFAULT_TRANS(kvm)	VIRTIO_PCI
 
 struct kvm_arch {
 	u16			boot_selector;
Signature · 1 line
1.8.2.2