[PATCH 11/13] pci-assign: Track MSI/MSI-X capability position, clean up related code

Jan Kiszka
2011-06-27T18:20:42Z

From: Jan Kiszka <jan.kiszka@siemens.com>

Store the MSI and MSI-X capability position in the same fields the QEMU
core uses as well. Although we still open-code MSI support, this does
not cause conflicts. Instead, it allow us to drop config space searches
from assigned_device_pci_cap_write_config. Moreover, we no longer need
to pass the cap position around and can clean up
assigned_dev_update_msi/msix a bit as well.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
hw/device-assignment.c | 37 ++++++++++++++++++++-----------------
1 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 43a048f..6a2ca8c 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -1069,11 +1069,12 @@ void assigned_dev_update_irqs(void)
}
}

-static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos)
+static void assigned_dev_update_msi(PCIDevice *pci_dev)
{
struct kvm_assigned_irq assigned_irq_data;
AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
- uint8_t ctrl_byte = pci_dev->config[ctrl_pos];
+ uint8_t ctrl_byte = pci_get_byte(pci_dev->config + pci_dev->msi_cap +
+ PCI_MSI_FLAGS);
int r;

memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
@@ -1096,13 +1097,13 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos)
}

if (ctrl_byte & PCI_MSI_FLAGS_ENABLE) {
- int pos = ctrl_pos - PCI_MSI_FLAGS;
+ uint8_t *pos = pci_dev->config + pci_dev->msi_cap;
+
assigned_dev->entry = qemu_mallocz(sizeof(*(assigned_dev->entry)));
assigned_dev->entry->u.msi.address_lo =
- pci_get_long(pci_dev->config + pos + PCI_MSI_ADDRESS_LO);
+ pci_get_long(pos + PCI_MSI_ADDRESS_LO);
assigned_dev->entry->u.msi.address_hi = 0;
- assigned_dev->entry->u.msi.data =
- pci_get_word(pci_dev->config + pos + PCI_MSI_DATA_32);
+ assigned_dev->entry->u.msi.data = pci_get_word(pos + PCI_MSI_DATA_32);
assigned_dev->entry->type = KVM_IRQ_ROUTING_MSI;
r = kvm_get_irq_route_gsi();
if (r < 0) {
@@ -1221,11 +1222,12 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
return r;
}

-static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
+static void assigned_dev_update_msix(PCIDevice *pci_dev)
{
struct kvm_assigned_irq assigned_irq_data;
AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
- uint16_t *ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos);
+ uint16_t ctrl_word = pci_get_word(pci_dev->config + pci_dev->msix_cap +
+ PCI_MSIX_FLAGS);
int r;

memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
@@ -1235,7 +1237,7 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
* try to catch this by only deassigning irqs if the guest is using
* MSIX or intends to start. */
if ((assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MSIX) ||
- (*ctrl_word & PCI_MSIX_FLAGS_ENABLE)) {
+ (ctrl_word & PCI_MSIX_FLAGS_ENABLE)) {

assigned_irq_data.flags = assigned_dev->irq_requested_type;
free_dev_irq_entries(assigned_dev);
@@ -1247,7 +1249,7 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
assigned_dev->irq_requested_type = 0;
}

- if (*ctrl_word & PCI_MSIX_FLAGS_ENABLE) {
+ if (ctrl_word & PCI_MSIX_FLAGS_ENABLE) {
assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX |
KVM_DEV_IRQ_GUEST_MSIX;

@@ -1311,21 +1313,20 @@ static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev,
uint32_t val, int len)
{
uint8_t cap_id = pci_dev->config_map[address];
- uint8_t cap;

pci_default_write_config(pci_dev, address, val, len);
switch (cap_id) {
case PCI_CAP_ID_MSI:
- cap = pci_find_capability(pci_dev, cap_id);
- if (ranges_overlap(address - cap, len, PCI_MSI_FLAGS, 1)) {
- assigned_dev_update_msi(pci_dev, cap + PCI_MSI_FLAGS);
+ if (range_covers_byte(address, len,
+ pci_dev->msi_cap + PCI_MSI_FLAGS)) {
+ assigned_dev_update_msi(pci_dev);
}
break;

case PCI_CAP_ID_MSIX:
- cap = pci_find_capability(pci_dev, cap_id);
- if (ranges_overlap(address - cap, len, PCI_MSIX_FLAGS + 1, 1)) {
- assigned_dev_update_msix(pci_dev, cap + PCI_MSIX_FLAGS);
+ if (range_covers_byte(address, len,
+ pci_dev->msix_cap + PCI_MSIX_FLAGS + 1)) {
+ assigned_dev_update_msix(pci_dev);
}
break;

@@ -1356,6 +1357,7 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
if ((ret = pci_add_capability(pci_dev, PCI_CAP_ID_MSI, pos, 10)) < 0) {
return ret;
}
+ pci_dev->msi_cap = pos;

pci_set_word(pci_dev->config + pos + PCI_MSI_FLAGS,
pci_get_word(pci_dev->config + pos + PCI_MSI_FLAGS) &
@@ -1378,6 +1380,7 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
if ((ret = pci_add_capability(pci_dev, PCI_CAP_ID_MSIX, pos, 12)) < 0) {
return ret;
}
+ pci_dev->msix_cap = pos;

pci_set_word(pci_dev->config + pos + PCI_MSIX_FLAGS,
pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS) &
--
1.7.1