kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: kvm@vger.kernel.org
Cc: alex.williamson@redhat.com, jan.kiszka@siemens.com,
	shashidhar.patil@gmail.com
Subject: [PATCH 9/9] pci-assign: Update MSI-X config based on table writes
Date: Sat, 28 Jan 2012 07:22:27 -0700	[thread overview]
Message-ID: <20120128142227.25681.78115.stgit@bling.home> (raw)
In-Reply-To: <20120128142104.25681.93072.stgit@bling.home>

We currently only update MSI-X configuration with the enable bit
in PCI config space is toggled.  This is pretty sketchy and part
of the reason for the odd checks for vector data is to guess
whether the guest is going to use the vector so we can pre-enable
it.

Two key things missed by doing this is that we don't catch vector
changes after enabling (ex. setting smp_affinity on an irq) and
we don't support guests that don't touch the vector table prior
to enabling the MSI-X capability (ex. freebsd).  This patch
fixes both of these problems.

I'm not able to get good behavior attempting to disable masked
vectors, so we don't actually do anything on mask yet.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---

 hw/device-assignment.c |   93 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 82 insertions(+), 11 deletions(-)

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 7e52615..8b05fa3 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -971,6 +971,11 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev)
     }
 }
 
+static inline bool msix_masked(MSIXTableEntry *entry)
+{
+    return !!(entry->ctrl & 0x1);
+}
+
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
@@ -982,17 +987,19 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     /* Get the usable entry number for allocating */
     for (i = 0; i < adev->msix_max; i++, entry++) {
-        /* Ignore unused entry even it's unmasked */
-        if (entry->data == 0) {
+        if (msix_masked(entry)) {
             continue;
         }
         entries_nr++;
     }
 
-    if (entries_nr == 0) {
-        fprintf(stderr, "MSI-X entry number is zero!\n");
-        return -EINVAL;
+    DEBUG("MSI-X entries: %d\n", entries_nr);
+
+    /* It's valid to enable MSI-X with all entries masked */
+    if (!entries_nr) {
+        return 0;
     }
+
     msix_nr.assigned_dev_id = calc_assigned_dev_id(adev);
     msix_nr.entry_nr = entries_nr;
     r = kvm_assign_set_msix_nr(kvm_state, &msix_nr);
@@ -1010,7 +1017,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entry = adev->msix_table;
     for (i = 0; i < adev->msix_max; i++, entry++) {
-        if (entry->data == 0) {
+        if (msix_masked(entry)) {
             continue;
         }
 
@@ -1082,9 +1089,12 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev)
             perror("assigned_dev_update_msix_mmio");
             return;
         }
-        if (kvm_assign_irq(kvm_state, &assigned_irq_data) < 0) {
-            perror("assigned_dev_enable_msix: assign irq");
-            return;
+
+        if (assigned_dev->irq_entries_nr) {
+            if (kvm_assign_irq(kvm_state, &assigned_irq_data) < 0) {
+                perror("assigned_dev_enable_msix: assign irq");
+                return;
+            }
         }
         assigned_dev->girq = -1;
         assigned_dev->irq_requested_type = assigned_irq_data.flags;
@@ -1445,11 +1455,72 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
                             uint64_t val, unsigned size)
 {
     AssignedDevice *adev = opaque;
+    PCIDevice *pdev = &adev->dev;
+    uint16_t ctrl;
+    MSIXTableEntry orig;
+    int i = addr >> 4;
+
+    if (i >= adev->msix_max) {
+        return; /* Drop write */
+    }
 
-    DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
-          addr, val);
+    ctrl = pci_get_word(pdev->config + pdev->msix_cap + PCI_MSIX_FLAGS);
+
+    DEBUG("write to MSI-X table offset 0x%lx, val 0x%lx\n", addr, val);
+
+    if (ctrl & PCI_MSIX_FLAGS_ENABLE) {
+        orig = adev->msix_table[i];
+    }
 
     memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
+
+    if (ctrl & PCI_MSIX_FLAGS_ENABLE) {
+        MSIXTableEntry *entry = &adev->msix_table[i];
+
+        if (!msix_masked(&orig) && msix_masked(entry)) {
+            /*
+             * Vector masked, disable it
+             *
+             * XXX theoretically we'd call kvm_assign_set_msix_entry
+             * with a gsi of 0 here as the API indicates that should
+             * disable the interrupt.  However, when we do that with
+             * devices with lots of vectors and irqbalance running,
+             * we seem to make kvm confused and get an ENOSPC from
+             * kvm_assign_set_msix_entry when we try to restore it.
+             * So for now we don't actually disable, but we'll update
+             * the entry when it's re-enabled below.
+             */
+        } else if (msix_masked(&orig) && !msix_masked(entry)) {
+            /* Vector unmasked */
+            if (i >= adev->irq_entries_nr || !adev->entry[i].type) {
+                /* Previously unassigned vector, start from scratch */
+                assigned_dev_update_msix(pdev);
+                return;
+            } else {
+                /* Update an existing, previously masked vector */
+                struct kvm_irq_routing_entry orig = adev->entry[i];
+                int ret;
+
+                adev->entry[i].u.msi.address_lo = entry->addr_lo;
+                adev->entry[i].u.msi.address_hi = entry->addr_hi;
+                adev->entry[i].u.msi.data = entry->data;
+
+                ret = kvm_update_routing_entry(&orig, &adev->entry[i]);
+                if (ret) {
+                    fprintf(stderr,
+                            "Error updating irq routing entry (%d)\n", ret);
+                    return;
+                }
+
+                ret = kvm_commit_irq_routes();
+                if (ret) {
+                    fprintf(stderr,
+                            "Error committing irq routes (%d)\n", ret);
+                    return;
+                }
+            }
+        }
+    }
 }
 
 static const MemoryRegionOps msix_mmio_ops = {


  parent reply	other threads:[~2012-01-28 14:22 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-28 14:21 [PATCH 0/9] pci-assign: 64bit MMIO + better MSI-X table support Alex Williamson
2012-01-28 14:21 ` [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest Alex Williamson
2012-01-31 12:40   ` Avi Kivity
2012-01-31 12:45     ` Jan Kiszka
2012-01-31 12:51       ` Avi Kivity
2012-01-31 12:57         ` Jan Kiszka
2012-01-31 13:10           ` Avi Kivity
2012-01-31 13:21             ` Jan Kiszka
2012-01-31 13:33               ` Avi Kivity
2012-01-31 21:08                 ` Alex Williamson
2012-01-31 21:14                   ` Michael S. Tsirkin
2012-02-01  9:03                     ` Avi Kivity
2012-02-01 10:03                       ` Michael S. Tsirkin
2012-02-01 13:55                       ` Alex Williamson
2012-02-01 15:18                         ` Michael S. Tsirkin
2012-02-01 15:24                           ` Alex Williamson
2012-01-28 14:21 ` [PATCH 2/9] pci-assign: Fix warnings with DEBUG enabled Alex Williamson
2012-01-28 14:21 ` [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API Alex Williamson
2012-01-31 12:45   ` Avi Kivity
2012-01-31 21:13     ` Alex Williamson
2012-02-01  4:22       ` Alex Williamson
2012-02-01  9:04         ` Avi Kivity
2012-02-01 13:56           ` Alex Williamson
2012-01-28 14:21 ` [PATCH 4/9] pci-assign: Use struct for MSI-X table Alex Williamson
2012-01-31 17:40   ` Michael S. Tsirkin
2012-01-31 19:05     ` Alex Williamson
2012-01-31 20:00       ` Michael S. Tsirkin
2012-01-31 21:17         ` Alex Williamson
2012-01-31 21:24           ` Michael S. Tsirkin
2012-01-31 21:30             ` Alex Williamson
2012-01-28 14:22 ` [PATCH 5/9] pci-assign: Only calculate maximum MSI-X vector entries once Alex Williamson
2012-01-31 20:18   ` Michael S. Tsirkin
2012-01-31 20:31     ` Alex Williamson
2012-01-31 20:56       ` Michael S. Tsirkin
2012-01-28 14:22 ` [PATCH 6/9] pci-assign: Proper initialization for MSI-X table Alex Williamson
2012-01-31 17:40   ` Michael S. Tsirkin
2012-01-31 19:07     ` Alex Williamson
2012-01-31 19:12       ` Michael S. Tsirkin
2012-01-31 19:16         ` Jan Kiszka
2012-01-31 20:19           ` Michael S. Tsirkin
2012-01-31 21:06             ` Alex Williamson
2012-01-28 14:22 ` [PATCH 7/9] pci-assign: Allocate entries for all MSI-X vectors Alex Williamson
2012-01-28 14:22 ` [PATCH 8/9] pci-assign: Use MSIX_PAGE_SIZE Alex Williamson
2012-01-28 14:22 ` Alex Williamson [this message]
2012-01-31 12:50   ` [PATCH 9/9] pci-assign: Update MSI-X config based on table writes Avi Kivity
2012-01-30 10:11 ` [PATCH 0/9] pci-assign: 64bit MMIO + better MSI-X table support Jan Kiszka
2012-01-30 13:44   ` Alex Williamson
2012-01-31 12:52     ` Avi Kivity
2012-01-31 12:56       ` Jan Kiszka
2012-02-06 15:55 ` Shashidhar Patil
2012-02-06 17:29   ` Alex Williamson
2012-02-09 16:23     ` Shashidhar Patil
2012-02-09 17:23       ` Alex Williamson
2012-02-12 16:30         ` Shashidhar Patil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120128142227.25681.78115.stgit@bling.home \
    --to=alex.williamson@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=kvm@vger.kernel.org \
    --cc=shashidhar.patil@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).