Skip to contentSkip to conversation

pci-assign: 64bit MMIO + better MSI-X table support

PATCH Superseded Alex Williamson 5 participants 54 messages Jan 28, 2012 – Feb 12, 2012 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover pci-assign: 64bit MMIO + better MSI-X table support

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts. The first patch enables exposing MMIO BARs as their native width to the guest. I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there. Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning. We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD. I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series. Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

I wasn't able to get masking to work reliably, so I left that as is for now. Perhaps someone has suggestions on getting that to work. Thanks,

Alex

---

Alex Williamson (9):
pci-assign: Update MSI-X config based on table writes
pci-assign: Use MSIX_PAGE_SIZE
pci-assign: Allocate entries for all MSI-X vectors
pci-assign: Proper initialization for MSI-X table
pci-assign: Only calculate maximum MSI-X vector entries once
pci-assign: Use struct for MSI-X table
pci-assign: Update MSI-X MMIO to Memory API
pci-assign: Fix warnings with DEBUG enabled
pci-assign: Optionally enable 64bit BARs in guest

 hw/device-assignment.c |  272 ++++++++++++++++++++++++++++++------------------
 hw/device-assignment.h |   12 ++
 2 files changed, 179 insertions(+), 105 deletions(-)
#1 lore raw

PATCH 1/9 pci-assign: Optionally enable 64bit BARs in guest

To date we've only exposed BARs as 32bit even if the device physically supports 64bit BARs. Enable 64bit BARs to be exposed as such in the guest, which may free up MMIO below 4G should the guest choose to use it.

This adds a new mem64= option to pci-assign, with the default being off for testing and enablement. The goal is to eventually make this enabled by default.

---

 hw/device-assignment.c |   13 +++++++++++--
 hw/device-assignment.h |    2 ++
 2 files changed, 13 insertions(+), 2 deletions(-)
hw/device-assignment.c +11 -2
index 7f4a5ec..07ae0a0 100644
@@ -49,6 +49,7 @@
 #define IORESOURCE_IRQ      0x00000400
 #define IORESOURCE_DMA      0x00000800
 #define IORESOURCE_PREFETCH 0x00002000  /* No side effects */
+#define IORESOURCE_MEM_64   0x00100000
 
 /* #define DEVICE_ASSIGNMENT_DEBUG 1 */
 
@@ -377,6 +378,11 @@ static int assigned_dev_register_regions(PCIRegion *io_regions,
                 ? PCI_BASE_ADDRESS_MEM_PREFETCH
                 : PCI_BASE_ADDRESS_SPACE_MEMORY;
 
+            if ((pci_dev->features & ASSIGNED_DEVICE_USE_MEM64_MASK) &&
+                (cur_region->type & IORESOURCE_MEM_64)) {
+                t |= PCI_BASE_ADDRESS_MEM_TYPE_64;
+            }
+
             /* map physical memory */
             pci_dev->v_addrs[i].u.r_virtbase = mmap(NULL, cur_region->size,
                                                     PROT_WRITE | PROT_READ,
@@ -569,8 +575,9 @@ again:
         rp->valid = 0;
         rp->resource_fd = -1;
         size = end - start + 1;
-        flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
-        if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
+        flags &= IORESOURCE_IO | IORESOURCE_MEM |
+                 IORESOURCE_PREFETCH | IORESOURCE_MEM_64;
+        if (size == 0 || !(flags & (IORESOURCE_IO | IORESOURCE_MEM)))
             continue;
         if (flags & IORESOURCE_MEM) {
             flags &= ~IORESOURCE_IO;
@@ -1701,6 +1708,8 @@ static PCIDeviceInfo assign_info = {
                         ASSIGNED_DEVICE_USE_IOMMU_BIT, true),
         DEFINE_PROP_BIT("prefer_msi", AssignedDevice, features,
                         ASSIGNED_DEVICE_PREFER_MSI_BIT, true),
+        DEFINE_PROP_BIT("mem64", AssignedDevice, features,
+                        ASSIGNED_DEVICE_USE_MEM64_BIT, false),
         DEFINE_PROP_INT32("bootindex", AssignedDevice, bootindex, -1),
         DEFINE_PROP_STRING("configfd", AssignedDevice, configfd_name),
         DEFINE_PROP_END_OF_LIST(),
hw/device-assignment.h +2 -0
index 1b4aecc..8ee2274 100644
@@ -76,9 +76,11 @@ typedef struct {
 
 #define ASSIGNED_DEVICE_USE_IOMMU_BIT	0
 #define ASSIGNED_DEVICE_PREFER_MSI_BIT	1
+#define ASSIGNED_DEVICE_USE_MEM64_BIT	2
 
 #define ASSIGNED_DEVICE_USE_IOMMU_MASK	(1 << ASSIGNED_DEVICE_USE_IOMMU_BIT)
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
+#define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
 typedef struct AssignedDevice {
     PCIDevice dev;
#2 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 9 lines · Alex Williamson

To date we've only exposed BARs as 32bit even if the device physically supports 64bit BARs. Enable 64bit BARs to be exposed as such in the guest, which may free up MMIO below 4G should the guest choose to use it.

This adds a new mem64= option to pci-assign, with the default being off for testing and enablement. The goal is to eventually make this enabled by default.

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 6 lines · Avi Kivity
Quoted 5 lines · Alex Williamson

To date we've only exposed BARs as 32bit even if the device physically supports 64bit BARs. Enable 64bit BARs to be exposed as such in the guest, which may free up MMIO below 4G should the guest choose to use it.

Quoted 7 lines
Quoted 4 lines

This adds a new mem64= option to pci-assign, with the default being off for testing and enablement. The goal is to eventually make this enabled by default.

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#4 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 12 lines · Jan Kiszka
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Alex Williamson

To date we've only exposed BARs as 32bit even if the device physically supports 64bit BARs. Enable 64bit BARs to be exposed as such in the guest, which may free up MMIO below 4G should the guest choose to use it.

This adds a new mem64= option to pci-assign, with the default being off for testing and enablement. The goal is to eventually make this enabled by default.

Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 13 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Alex Williamson

To date we've only exposed BARs as 32bit even if the device physically supports 64bit BARs. Enable 64bit BARs to be exposed as such in the guest, which may free up MMIO below 4G should the guest choose to use it.

This adds a new mem64= option to pci-assign, with the default being off for testing and enablement. The goal is to eventually make this enabled by default.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#6 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 8 lines · Avi Kivity
Quoted 7 lines · Jan Kiszka
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#8 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Alex

#10 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 26 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

Quoted 6 lines

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Alex

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

#11 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 32 lines · Michael S. Tsirkin
Quoted 31 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Quoted 5 lines
Quoted 1 line

Alex

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

The bugs are unrelated to 64-bit BARs.

Alex, why drop the patch? We're far from a release, if there are indeed bugs there, we'll find out.

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 33 lines · Avi Kivity
Quoted 32 lines · Michael S. Tsirkin
Quoted 31 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Alex

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

The bugs are unrelated to 64-bit BARs.

This is my understanding as well.
However, qemu pci has this work around for 32 bit BARs:

    /* Now pcibus_t is 64bit.
     * Check if 32 bit BAR wraps around explicitly.
     * Without this, PC ide doesn't work well.
     * TODO: remove this work around.
     */
    if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
        return PCI_BAR_UNMAPPED;
    }

this helps work around the bugs on condition that guests write UINT32_MAX into BARs to size them which happens to be true.

And for this reason, the bugs trigger with 64 bit BARs but not 32 bit ones.

Quoted 2 lines

Alex, why drop the patch? We're far from a release, if there are indeed bugs there, we'll find out.

I think it will expose the 64 bit handling bug so I suggested applying this patch after you fix the bugs you found in memory - it's better for bisectability.

Quoted 7 lines
Signature · 6 lines
error compiling committee.c: too many arguments to function

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#13 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

In reply to Avi Kivity
Quoted 35 lines · Avi Kivity
Quoted 34 lines · Michael S. Tsirkin
Quoted 32 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Alex

Quoted 8 lines
Quoted 3 lines

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

The bugs are unrelated to 64-bit BARs.

Alex, why drop the patch? We're far from a release, if there are indeed bugs there, we'll find out.

I think maybe this should be a core pci device option so we don't have pci-assign.mem64 vs virtio-net-pci.64bit. I'd like to always set the MEM64 bit to match hardware when registering a BAR and let PCI core decide whether to expose it that way. Besides, it was unrelated to the bulk of the series. Thanks,

Alex

#14 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 42 lines · Alex Williamson
Quoted 41 lines · Avi Kivity
Quoted 32 lines · Michael S. Tsirkin
Quoted 31 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Quoted 5 lines
Quoted 1 line

Alex

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

The bugs are unrelated to 64-bit BARs.

Quoted 7 lines
Quoted 2 lines

Alex, why drop the patch? We're far from a release, if there are indeed bugs there, we'll find out.

I think maybe this should be a core pci device option so we don't have pci-assign.mem64 vs virtio-net-pci.64bit. I'd like to always set the MEM64 bit to match hardware when registering a BAR and let PCI core decide whether to expose it that way.

Besides the bugs, there aren't any reasons not to expose it that way, right?

Quoted 6 lines

Besides, it was unrelated to the
bulk of the series. Thanks,

Alex

#15 lore raw

Re: [PATCH 1/9] pci-assign: Optionally enable 64bit BARs in guest

Quoted 46 lines · Michael S. Tsirkin
Quoted 45 lines · Alex Williamson
Quoted 43 lines · Avi Kivity
Quoted 32 lines · Michael S. Tsirkin
Quoted 31 lines · Alex Williamson
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Jan Kiszka
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Seems fine, but do we really need the option? If it doesn't work we should treat it as an ordinary but and fix it.

So far it's against the architecture of the emulated system: our current chipset predates 64 bit PCI.

Then it should be enabled/disabled at the chipset level.

Makes me wonder if we already do some filtering if the device supports 64 bit but the next bridge does not.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Our 440fx does support 64-bit bars, so the question doesn't arise for x86. Instead we violate the spec.

If you mean by "our" the 440fx-qemu, not the real 440fx. That one does not even support >1GB RAM.

Yes, that's what I meant. It also supports pci hotplug, more slots, cpu hotplug, etc.

I'll drop this patch for now, it was just something I enabled based on a query from MST and didn't want to lose it. Maybe we need the option in PCI core, but I'm just turn it on and hope for the best w/o giving users a way to disable it. Thanks,

Quoted 5 lines
Quoted 1 line

Alex

The patch itself makes sense to me.
Avi found some bugs in 64 bit BAR handling, let's wait till that is fixed then merge this patch.

The bugs are unrelated to 64-bit BARs.

Alex, why drop the patch? We're far from a release, if there are indeed bugs there, we'll find out.

Quoted 7 lines
Quoted 4 lines

I think maybe this should be a core pci device option so we don't have pci-assign.mem64 vs virtio-net-pci.64bit. I'd like to always set the MEM64 bit to match hardware when registering a BAR and let PCI core decide whether to expose it that way.

Besides the bugs, there aren't any reasons not to expose it that way, right?

Yep, mainly just a compatibility/debugging thing.

Alex

#16 lore raw

PATCH 2/9 pci-assign: Fix warnings with DEBUG enabled

In reply to Alex Williamson

---

 hw/device-assignment.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
hw/device-assignment.c +4 -4
index 07ae0a0..b176375 100644
@@ -75,7 +75,7 @@ static uint64_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region,
 
     if (fd >= 0) {
         if (data) {
-            DEBUG("pwrite data=%x, size=%d, e_phys=%x, addr=%x\n",
+            DEBUG("pwrite data=%lx, size=%d, e_phys=%lx, addr=%lx\n",
                   *data, size, addr, addr);
             if (pwrite(fd, data, size, addr) != size) {
                 fprintf(stderr, "%s - pwrite failed %s\n",
@@ -87,14 +87,14 @@ static uint64_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region,
                         __func__, strerror(errno));
                 val = (1UL << (size * 8)) - 1;
             }
-            DEBUG("pread val=%x, size=%d, e_phys=%x, addr=%x\n",
+            DEBUG("pread val=%lx, size=%d, e_phys=%lx, addr=%lx\n",
                   val, size, addr, addr);
         }
     } else {
         uint32_t port = addr + dev_region->u.r_baseport;
 
         if (data) {
-            DEBUG("out data=%x, size=%d, e_phys=%x, host=%x\n",
+            DEBUG("out data=%lx, size=%d, e_phys=%lx, host=%x\n",
                   *data, size, addr, port);
             switch (size) {
                 case 1:
@@ -119,7 +119,7 @@ static uint64_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region,
                     val = inl(port);
                     break;
             }
-            DEBUG("in data=%x, size=%d, e_phys=%x, host=%x\n",
+            DEBUG("in data=%lx, size=%d, e_phys=%lx, host=%x\n",
                   val, size, addr, port);
         }
     }
#17 lore raw

PATCH 3/9 pci-assign: Update MSI-X MMIO to Memory API

In reply to Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

---

 hw/device-assignment.c |   56 +++++++++++++-----------------------------------
 1 files changed, 15 insertions(+), 41 deletions(-)
hw/device-assignment.c +15 -41
index b176375..1fc7ffa 100644
@@ -1437,62 +1437,36 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     return 0;
 }
 
-static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
+static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
+                               unsigned size)
 {
     AssignedDevice *adev = opaque;
-    unsigned int offset = addr & 0xfff;
-    void *page = adev->msix_table_page;
-    uint32_t val = 0;
+    uint64_t val;
 
-    memcpy(&val, (void *)((char *)page + offset), 4);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
 
     return val;
 }
 
-static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
-{
-    return ((msix_mmio_readl(opaque, addr & ~3)) >>
-            (8 * (addr & 3))) & 0xff;
-}
-
-static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
-{
-    return ((msix_mmio_readl(opaque, addr & ~3)) >>
-            (8 * (addr & 3))) & 0xffff;
-}
-
-static void msix_mmio_writel(void *opaque,
-                             target_phys_addr_t addr, uint32_t val)
+static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
+                            uint64_t val, unsigned size)
 {
     AssignedDevice *adev = opaque;
-    unsigned int offset = addr & 0xfff;
-    void *page = adev->msix_table_page;
-
-    DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%x\n",
-		    addr, val);
-    memcpy((void *)((char *)page + offset), &val, 4);
-}
 
-static void msix_mmio_writew(void *opaque,
-                             target_phys_addr_t addr, uint32_t val)
-{
-    msix_mmio_writel(opaque, addr & ~3,
-                     (val & 0xffff) << (8*(addr & 3)));
-}
+    DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
+          addr, val);
 
-static void msix_mmio_writeb(void *opaque,
-                             target_phys_addr_t addr, uint32_t val)
-{
-    msix_mmio_writel(opaque, addr & ~3,
-                     (val & 0xff) << (8*(addr & 3)));
+    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
#18 lore raw

Re: [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API

Quoted 9 lines · Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

static const MemoryRegionOps msix_mmio_ops = {

hw/device-assignment.c +0 -2
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1470

Quoted 10 lines
hw/device-assignment.c +6 -2
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };

.impl.min_access_size = 4 means the core will convert 1-byte I/O to 4-byte I/O (using rmw if needed). That's not what we want, I think you can leave it at 1 and explicitly ignore small accesses in the callbacks.

Have you tested 8-byte I/O? This is the first user. Don't you need to set .valid.max_access_size?

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw

Re: [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API

Quoted 21 lines · Avi Kivity
Quoted 9 lines · Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

static const MemoryRegionOps msix_mmio_ops = {

hw/device-assignment.c +0 -2
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1470

Quoted 10 lines
hw/device-assignment.c +6 -2
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };
Quoted 6 lines

.impl.min_access_size = 4 means the core will convert 1-byte I/O to 4-byte I/O (using rmw if needed). That's not what we want, I think you can leave it at 1 and explicitly ignore small accesses in the callbacks.

Have you tested 8-byte I/O? This is the first user. Don't you need to set .valid.max_access_size?

I have not explicitly tested 8-byte I/O, figured it might just work. Hmm, I wonder if we really need to be strict enough to reject byte and word access. It doesn't follow the spec, but I don't know that it buys us anything to be strict about it. Anyway, I'll look at .valid and respin. Thanks,

Alex

#20 lore raw

Re: [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API

Quoted 22 lines · Alex Williamson
Quoted 21 lines · Avi Kivity
Quoted 19 lines · Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

static const MemoryRegionOps msix_mmio_ops = {

hw/device-assignment.c +6 -4
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };
Quoted 8 lines
Quoted 6 lines

.impl.min_access_size = 4 means the core will convert 1-byte I/O to 4-byte I/O (using rmw if needed). That's not what we want, I think you can leave it at 1 and explicitly ignore small accesses in the callbacks.

Have you tested 8-byte I/O? This is the first user. Don't you need to set .valid.max_access_size?

I have not explicitly tested 8-byte I/O, figured it might just work.

Tested, it gets split into 4-byte accesses by cpu_physical_memory_rw(). It's trivial to add 8-byte access there and they get all the way through the memory API intact. I expect there's a lot more than that to make exec.c quad word access clean though. I'll leave the 8-byte access in place in case it's a good test case. Thanks,

Alex

#21 lore raw

Re: [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API

Quoted 30 lines · Alex Williamson
Quoted 29 lines · Alex Williamson
Quoted 27 lines · Avi Kivity
Quoted 9 lines · Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

static const MemoryRegionOps msix_mmio_ops = {

hw/device-assignment.c +0 -2
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1470

Quoted 10 lines
hw/device-assignment.c +6 -2
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };

.impl.min_access_size = 4 means the core will convert 1-byte I/O to 4-byte I/O (using rmw if needed). That's not what we want, I think you can leave it at 1 and explicitly ignore small accesses in the callbacks.

Have you tested 8-byte I/O? This is the first user. Don't you need to set .valid.max_access_size?

Quoted 8 lines
Quoted 1 line

I have not explicitly tested 8-byte I/O, figured it might just work.

Tested, it gets split into 4-byte accesses by cpu_physical_memory_rw(). It's trivial to add 8-byte access there and they get all the way through the memory API intact. I expect there's a lot more than that to make exec.c quad word access clean though. I'll leave the 8-byte access in place in case it's a good test case. Thanks,

It doesn't make sense without .valid.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw

Re: [PATCH 3/9] pci-assign: Update MSI-X MMIO to Memory API

Quoted 33 lines · Avi Kivity
Quoted 32 lines · Alex Williamson
Quoted 30 lines · Alex Williamson
Quoted 21 lines · Avi Kivity
Quoted 9 lines · Alex Williamson

Stop using compatibility mode and at the same time fix available access sizes. The PCI spec indicates that the MSI-X table may only be accessed as DWORD or QWORD.

static const MemoryRegionOps msix_mmio_ops = {

hw/device-assignment.c +0 -2
-    .old_mmio = {
-        .read = { msix_mmio_readb, msix_mmio_readw, msix_mmio_readl, },

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1470

Quoted 10 lines
hw/device-assignment.c +6 -2
-        .write = { msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel, },
-    },
+    .read = msix_mmio_read,
+    .write = msix_mmio_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 8,
+    },
 };
Quoted 6 lines

.impl.min_access_size = 4 means the core will convert 1-byte I/O to 4-byte I/O (using rmw if needed). That's not what we want, I think you can leave it at 1 and explicitly ignore small accesses in the callbacks.

Have you tested 8-byte I/O? This is the first user. Don't you need to set .valid.max_access_size?

I have not explicitly tested 8-byte I/O, figured it might just work.

Quoted 8 lines
Quoted 6 lines

Tested, it gets split into 4-byte accesses by cpu_physical_memory_rw(). It's trivial to add 8-byte access there and they get all the way through the memory API intact. I expect there's a lot more than that to make exec.c quad word access clean though. I'll leave the 8-byte access in place in case it's a good test case. Thanks,

It doesn't make sense without .valid.

Yep, v2 sets both .valid & .impl, which is what I used to test. Thanks,

Alex

#23 lore raw

PATCH 4/9 pci-assign: Use struct for MSI-X table

In reply to Alex Williamson

This makes access much easier.

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -1
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
#24 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 3 lines · Alex Williamson

This makes access much easier.

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Quoted 168 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -2
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#25 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 8 lines · Michael S. Tsirkin
Quoted 3 lines · Alex Williamson

This makes access much easier.

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Do we really think existing device assignment is ever going to work on anything but x86/x86?

Alex

Quoted 168 lines
Quoted 168 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -2
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#26 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 6 lines · Alex Williamson
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Alex Williamson

This makes access much easier.

Quoted 8 lines
Quoted 3 lines

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Do we really think existing device assignment is ever going to work on anything but x86/x86?

Alex

It's a question of whether anyone bothers to fix all portability bugs. Besides endian-ness, and msix vector decoding, there's not much that is x86 specific there.

Quoted 175 lines
Quoted 164 lines
Quoted 164 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -1
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#27 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson
Quoted 8 lines · Michael S. Tsirkin
Quoted 3 lines · Alex Williamson

This makes access much easier.

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Do we really think existing device assignment is ever going to work on anything but x86/x86?

Quoted 6 lines
Quoted 1 line

Alex

It's a question of whether anyone bothers to fix all portability bugs. Besides endian-ness, and msix vector decoding, there's not much that is x86 specific there.

The way interrupt routing is updated from the chipset is disgustingly x86 specific. The entire kernel side implementation is only enabled for x86 (well, not counting ia64). I expect the kernel side interrupt injection is very x86, or at least ioapic, specific. Beyond that, yeah, it may be trivial ;) Thanks,

Alex

Quoted 175 lines
Quoted 175 lines
Quoted 164 lines
Quoted 164 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -1
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#28 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 17 lines · Alex Williamson
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Alex Williamson
Quoted 8 lines · Michael S. Tsirkin
Quoted 3 lines · Alex Williamson

This makes access much easier.

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Do we really think existing device assignment is ever going to work on anything but x86/x86?

Alex

Quoted 7 lines
Quoted 4 lines

It's a question of whether anyone bothers to fix all portability bugs. Besides endian-ness, and msix vector decoding, there's not much that is x86 specific there.

The way interrupt routing is updated from the chipset is disgustingly x86 specific.

Right, that's what I mean - the decoding of msix vector to destination.

Quoted 184 lines

The entire kernel side implementation is only enabled for x86 (well, not counting ia64). I expect the kernel side interrupt injection is very x86, or at least ioapic, specific. Beyond that, yeah, it may be trivial ;) Thanks,

Alex

Quoted 175 lines
Quoted 175 lines
Quoted 164 lines
Quoted 164 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -1
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#29 lore raw

Re: [PATCH 4/9] pci-assign: Use struct for MSI-X table

Quoted 23 lines · Michael S. Tsirkin
Quoted 22 lines · Alex Williamson
Quoted 20 lines · Michael S. Tsirkin
Quoted 6 lines · Alex Williamson
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Alex Williamson

This makes access much easier.

Quoted 8 lines
Quoted 3 lines

Yes but this also makes it easier to forget
to handle endian-ness.
How about using pci_get/pci_set instead?

Do we really think existing device assignment is ever going to work on anything but x86/x86?

Alex

It's a question of whether anyone bothers to fix all portability bugs. Besides endian-ness, and msix vector decoding, there's not much that is x86 specific there.

Quoted 4 lines
Quoted 2 lines

The way interrupt routing is updated from the chipset is disgustingly x86 specific.

Right, that's what I mean - the decoding of msix vector to destination.

Yes, that, but I was referring more to INTx and the ugly hard coded hook to get notified when the 440fx changes PCI interrupt link setup. Thanks,

Alex

Quoted 185 lines
Quoted 185 lines

The entire kernel side implementation is only enabled for x86 (well, not counting ia64). I expect the kernel side interrupt injection is very x86, or at least ioapic, specific. Beyond that, yeah, it may be trivial ;) Thanks,

Alex

Quoted 175 lines
Quoted 175 lines
Quoted 164 lines
Quoted 164 lines

---

 hw/device-assignment.c |   55 ++++++++++++++++++++++--------------------------
 hw/device-assignment.h |    9 +++++++-
 2 files changed, 33 insertions(+), 31 deletions(-)
hw/device-assignment.c +25 -30
index 1fc7ffa..422ee00 100644
@@ -973,10 +973,9 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
     uint16_t entries_nr = 0, entries_max_nr;
     int pos = 0, i, r = 0;
-    uint32_t msg_addr, msg_upper_addr, msg_data, msg_ctrl;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
-    void *va = adev->msix_table_page;
+    MSIXTableEntry *entry = adev->msix_table;
 
     pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
 
@@ -985,12 +984,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     entries_max_nr += 1;
 
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++) {
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
+        }
         entries_nr ++;
     }
 
@@ -1013,16 +1011,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
-    for (i = 0; i < entries_max_nr; i++) {
+    entry = adev->msix_table;
+    for (i = 0; i < entries_max_nr; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
-        memcpy(&msg_ctrl, va + i * 16 + 12, 4);
-        memcpy(&msg_data, va + i * 16 + 8, 4);
-        if (msg_data == 0)
+        if (entry->data == 0) {
             continue;
-
-        memcpy(&msg_addr, va + i * 16, 4);
-        memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
+        }
 
         r = kvm_get_irq_route_gsi();
         if (r < 0)
@@ -1031,10 +1026,11 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         adev->entry[entries_nr].gsi = r;
         adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
         adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = msg_addr;
-        adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
-        adev->entry[entries_nr].u.msi.data = msg_data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
+        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
+        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
+        adev->entry[entries_nr].u.msi.data = entry->data;
+        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
+              entry->data, entry->addr_lo);
 	kvm_add_routing_entry(&adev->entry[entries_nr]);
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
@@ -1443,7 +1439,7 @@ static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
     AssignedDevice *adev = opaque;
     uint64_t val;
 
-    memcpy(&val, (void *)((uint8_t *)adev->msix_table_page + addr), size);
+    memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
 
     return val;
 }
@@ -1456,7 +1452,7 @@ static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
     DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
           addr, val);
 
-    memcpy((void *)((uint8_t *)adev->msix_table_page + addr), &val, size);
+    memcpy((void *)((uint8_t *)adev->msix_table + addr), &val, size);
 }
 
 static const MemoryRegionOps msix_mmio_ops = {
@@ -1471,15 +1467,13 @@ static const MemoryRegionOps msix_mmio_ops = {
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table_page = mmap(NULL, 0x1000,
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
-    if (dev->msix_table_page == MAP_FAILED) {
-        fprintf(stderr, "fail allocate msix_table_page! %s\n",
-                strerror(errno));
+    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+                           MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
+    if (dev->msix_table == MAP_FAILED) {
+        fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table_page, 0, 0x1000);
+    memset(dev->msix_table, 0, 0x1000);
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
@@ -1487,16 +1481,17 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 {
-    if (!dev->msix_table_page)
+    if (!dev->msix_table) {
         return;
+    }
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table_page, 0x1000) == -1) {
-        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+    if (munmap(dev->msix_table, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
-    dev->msix_table_page = NULL;
+    dev->msix_table = NULL;
 }
 
 static const VMStateDescription vmstate_assigned_device = {
hw/device-assignment.h +8 -1
index 8ee2274..e229303 100644
@@ -82,6 +82,13 @@ typedef struct {
 #define ASSIGNED_DEVICE_PREFER_MSI_MASK	(1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
 #define ASSIGNED_DEVICE_USE_MEM64_MASK	(1 << ASSIGNED_DEVICE_USE_MEM64_BIT)
 
+typedef struct {
+    uint32_t addr_lo;
+    uint32_t addr_hi;
+    uint32_t data;
+    uint32_t ctrl;
+} MSIXTableEntry;
+
 typedef struct AssignedDevice {
     PCIDevice dev;
     PCIHostDevice host;
@@ -110,7 +117,7 @@ typedef struct AssignedDevice {
     uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
     int irq_entries_nr;
     struct kvm_irq_routing_entry *entry;
-    void *msix_table_page;
+    MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
     MemoryRegion mmio;
     char *configfd_name;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#30 lore raw

PATCH 5/9 pci-assign: Only calculate maximum MSI-X vector entries once

In reply to Alex Williamson

---

 hw/device-assignment.c |   17 +++++++----------
 hw/device-assignment.h |    1 +
 2 files changed, 8 insertions(+), 10 deletions(-)
hw/device-assignment.c +7 -10
index 422ee00..af614d3 100644
@@ -971,20 +971,14 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev)
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
-    uint16_t entries_nr = 0, entries_max_nr;
-    int pos = 0, i, r = 0;
+    uint16_t entries_nr = 0;
+    int i, r = 0;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
     MSIXTableEntry *entry = adev->msix_table;
 
-    pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
-
-    entries_max_nr = *(uint16_t *)(pci_dev->config + pos + 2);
-    entries_max_nr &= PCI_MSIX_FLAGS_QSIZE;
-    entries_max_nr += 1;
-
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
         if (entry->data == 0) {
             continue;
@@ -1012,7 +1006,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
     entry = adev->msix_table;
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
         if (entry->data == 0) {
@@ -1225,6 +1219,9 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
         bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
         msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
         dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
+        dev->msix_max = pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS);
+        dev->msix_max &= PCI_MSIX_FLAGS_QSIZE;
+        dev->msix_max += 1;
     }
 
     /* Minimal PM support, nothing writable, device appears to NAK changes */
hw/device-assignment.h +1 -0
index e229303..a909fb2 100644
@@ -119,6 +119,7 @@ typedef struct AssignedDevice {
     struct kvm_irq_routing_entry *entry;
     MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
+    uint16_t msix_max;
     MemoryRegion mmio;
     char *configfd_name;
     int32_t bootindex;
#31 lore raw

Re: [PATCH 5/9] pci-assign: Only calculate maximum MSI-X vector entries once

Quoted 2 lines · Alex Williamson

---

Why? Optimization?

Quoted 68 lines
 hw/device-assignment.c |   17 +++++++----------
 hw/device-assignment.h |    1 +
 2 files changed, 8 insertions(+), 10 deletions(-)
hw/device-assignment.c +7 -10
index 422ee00..af614d3 100644
@@ -971,20 +971,14 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev)
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
-    uint16_t entries_nr = 0, entries_max_nr;
-    int pos = 0, i, r = 0;
+    uint16_t entries_nr = 0;
+    int i, r = 0;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
     MSIXTableEntry *entry = adev->msix_table;
 
-    pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
-
-    entries_max_nr = *(uint16_t *)(pci_dev->config + pos + 2);
-    entries_max_nr &= PCI_MSIX_FLAGS_QSIZE;
-    entries_max_nr += 1;
-
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
         if (entry->data == 0) {
             continue;
@@ -1012,7 +1006,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
     entry = adev->msix_table;
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
         if (entry->data == 0) {
@@ -1225,6 +1219,9 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
         bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
         msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
         dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
+        dev->msix_max = pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS);
+        dev->msix_max &= PCI_MSIX_FLAGS_QSIZE;
+        dev->msix_max += 1;
     }
 
     /* Minimal PM support, nothing writable, device appears to NAK changes */
hw/device-assignment.h +1 -1
index e229303..a909fb2 100644
@@ -119,6 +119,7 @@ typedef struct AssignedDevice {
     struct kvm_irq_routing_entry *entry;
     MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
+    uint16_t msix_max;
     MemoryRegion mmio;
     char *configfd_name;
     int32_t bootindex;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#32 lore raw

Re: [PATCH 5/9] pci-assign: Only calculate maximum MSI-X vector entries once

Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Alex Williamson

---

Why? Optimization?

Because in 6/9 we'd have to calculate it again for resetting the msix table and in 9/9 we use it to drop writes outside of the valid vector range. I suspect we might actually want to call msix_reset on device reset, so that means we'd be recalculating it every time we reset the device, write to the msix table, and again if we're actually updating msix entries. Redundancy exceeded my cost of two bytes metric. Thanks,

Alex

Quoted 68 lines
Quoted 68 lines
 hw/device-assignment.c |   17 +++++++----------
 hw/device-assignment.h |    1 +
 2 files changed, 8 insertions(+), 10 deletions(-)
hw/device-assignment.c +7 -10
index 422ee00..af614d3 100644
@@ -971,20 +971,14 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev)
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
-    uint16_t entries_nr = 0, entries_max_nr;
-    int pos = 0, i, r = 0;
+    uint16_t entries_nr = 0;
+    int i, r = 0;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
     MSIXTableEntry *entry = adev->msix_table;
 
-    pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
-
-    entries_max_nr = *(uint16_t *)(pci_dev->config + pos + 2);
-    entries_max_nr &= PCI_MSIX_FLAGS_QSIZE;
-    entries_max_nr += 1;
-
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
         if (entry->data == 0) {
             continue;
@@ -1012,7 +1006,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
     entry = adev->msix_table;
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
         if (entry->data == 0) {
@@ -1225,6 +1219,9 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
         bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
         msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
         dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
+        dev->msix_max = pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS);
+        dev->msix_max &= PCI_MSIX_FLAGS_QSIZE;
+        dev->msix_max += 1;
     }
 
     /* Minimal PM support, nothing writable, device appears to NAK changes */
hw/device-assignment.h +1 -1
index e229303..a909fb2 100644
@@ -119,6 +119,7 @@ typedef struct AssignedDevice {
     struct kvm_irq_routing_entry *entry;
     MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
+    uint16_t msix_max;
     MemoryRegion mmio;
     char *configfd_name;
     int32_t bootindex;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#33 lore raw

Re: [PATCH 5/9] pci-assign: Only calculate maximum MSI-X vector entries once

Quoted 7 lines · Alex Williamson
Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Alex Williamson

---

Why? Optimization?

Quoted 8 lines

Because in 6/9 we'd have to calculate it again for resetting the msix table and in 9/9 we use it to drop writes outside of the valid vector range. I suspect we might actually want to call msix_reset on device reset, so that means we'd be recalculating it every time we reset the device, write to the msix table, and again if we're actually updating msix entries. Redundancy exceeded my cost of two bytes metric. Thanks,

Alex

No problem but motivation should probably go into commit log.

Quoted 75 lines
Quoted 64 lines
Quoted 64 lines
 hw/device-assignment.c |   17 +++++++----------
 hw/device-assignment.h |    1 +
 2 files changed, 8 insertions(+), 10 deletions(-)
hw/device-assignment.c +7 -10
index 422ee00..af614d3 100644
@@ -971,20 +971,14 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev)
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
-    uint16_t entries_nr = 0, entries_max_nr;
-    int pos = 0, i, r = 0;
+    uint16_t entries_nr = 0;
+    int i, r = 0;
     struct kvm_assigned_msix_nr msix_nr;
     struct kvm_assigned_msix_entry msix_entry;
     MSIXTableEntry *entry = adev->msix_table;
 
-    pos = pci_find_capability(pci_dev, PCI_CAP_ID_MSIX);
-
-    entries_max_nr = *(uint16_t *)(pci_dev->config + pos + 2);
-    entries_max_nr &= PCI_MSIX_FLAGS_QSIZE;
-    entries_max_nr += 1;
-
     /* Get the usable entry number for allocating */
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         /* Ignore unused entry even it's unmasked */
         if (entry->data == 0) {
             continue;
@@ -1012,7 +1006,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entries_nr = 0;
     entry = adev->msix_table;
-    for (i = 0; i < entries_max_nr; i++, entry++) {
+    for (i = 0; i < adev->msix_max; i++, entry++) {
         if (entries_nr >= msix_nr.entry_nr)
             break;
         if (entry->data == 0) {
@@ -1225,6 +1219,9 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
         bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
         msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
         dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
+        dev->msix_max = pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS);
+        dev->msix_max &= PCI_MSIX_FLAGS_QSIZE;
+        dev->msix_max += 1;
     }
 
     /* Minimal PM support, nothing writable, device appears to NAK changes */
hw/device-assignment.h +1 -0
index e229303..a909fb2 100644
@@ -119,6 +119,7 @@ typedef struct AssignedDevice {
     struct kvm_irq_routing_entry *entry;
     MSIXTableEntry *msix_table;
     target_phys_addr_t msix_table_addr;
+    uint16_t msix_max;
     MemoryRegion mmio;
     char *configfd_name;
     int32_t bootindex;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#34 lore raw

PATCH 6/9 pci-assign: Proper initialization for MSI-X table

In reply to Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +19 -1
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */
+    }
+}
+
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
     dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
@@ -1470,7 +1486,9 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
         fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table, 0, 0x1000);
+
+    msix_reset(dev);
+
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
#35 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 19 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +3 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 10 lines
hw/device-assignment.c +10 -0
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Quoted 22 lines
hw/device-assignment.c +6 -2
+    }
+}
+
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
     dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
@@ -1470,7 +1486,9 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
         fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table, 0, 0x1000);
+
+    msix_reset(dev);
+
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#36 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +0 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
Quoted 15 lines

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 13 lines
hw/device-assignment.c +13 -0
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Show me a BE host that even remotely works with this device assignment implementation. Thanks,

Alex

Quoted 22 lines
Quoted 22 lines
hw/device-assignment.c +6 -2
+    }
+}
+
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
     dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
@@ -1470,7 +1486,9 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
         fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table, 0, 0x1000);
+
+    msix_reset(dev);
+
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#37 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 34 lines · Alex Williamson
Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +0 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
Quoted 15 lines

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 13 lines
hw/device-assignment.c +13 -0
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Quoted 4 lines

Show me a BE host that even remotely works with this device assignment implementation. Thanks,

Alex

I don't get it. Yes lots of cleanup is needed but why add more broken code?

Quoted 29 lines
Quoted 18 lines
Quoted 18 lines
hw/device-assignment.c +6 -1
+    }
+}
+
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
     dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
@@ -1470,7 +1486,9 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
         fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
         return -EFAULT;
     }
-    memset(dev->msix_table, 0, 0x1000);
+
+    msix_reset(dev);
+
     memory_region_init_io(&dev->mmio, &msix_mmio_ops, dev,
                           "assigned-dev-msix", MSIX_PAGE_SIZE);
     return 0;
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#38 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 19 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +3 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 10 lines
hw/device-assignment.c +10 -0
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Quoted 7 lines
Quoted 4 lines

Show me a BE host that even remotely works with this device assignment implementation. Thanks,

Alex

I don't get it. Yes lots of cleanup is needed but why add more broken code?

At some point, we will just do this via the PCI core, and that will have to do it correctly anyway. This code here is supposed to be removed again.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#39 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 41 lines · Jan Kiszka
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Alex Williamson
Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +0 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
Quoted 15 lines

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 13 lines
hw/device-assignment.c +13 -0
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Show me a BE host that even remotely works with this device assignment implementation. Thanks,

Alex

Quoted 7 lines
Quoted 2 lines

I don't get it. Yes lots of cleanup is needed but why add more broken code?

At some point, we will just do this via the PCI core, and that will have to do it correctly anyway. This code here is supposed to be removed again.

Jan

Either we say this is all dead code, then let's just fix bugs until it is rewritten.
To fix the bug, all we need is patch 9, maybe 6 and 7.

Or there is intent to maintain it going forward then, methinks, it needs to be brought up to some minimal standards.

Quoted 3 lines
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#40 lore raw

Re: [PATCH 6/9] pci-assign: Proper initialization for MSI-X table

Quoted 50 lines · Michael S. Tsirkin
Quoted 41 lines · Jan Kiszka
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Alex Williamson
Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Alex Williamson

Per the PCI spec, all vectors should be masked at handoff.

---

 hw/device-assignment.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
hw/device-assignment.c +0 -0
index af614d3..6efa219 100644
@@ -1462,6 +1462,22 @@ static const MemoryRegionOps msix_mmio_ops = {
     },
 };
 
Quoted 15 lines

hw/device-assignment.c static const MemoryRegionOps msix_mmio_ops = { · line 1477

Quoted 13 lines
hw/device-assignment.c +13 -0
+static void msix_reset(AssignedDevice *dev)
+{
+    MSIXTableEntry *entry;
+    int i;
+
+    if (!dev->msix_table) {
+        return;
+    }
+
+    memset(dev->msix_table, 0, 0x1000);
+
+    for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
+        entry->ctrl = 0x1; /* Masked */

This is broken for BE hosts.

Show me a BE host that even remotely works with this device assignment implementation. Thanks,

Alex

Quoted 7 lines
Quoted 2 lines

I don't get it. Yes lots of cleanup is needed but why add more broken code?

At some point, we will just do this via the PCI core, and that will have to do it correctly anyway. This code here is supposed to be removed again.

Jan

Quoted 7 lines

Either we say this is all dead code, then let's just fix bugs until it is rewritten.
To fix the bug, all we need is patch 9, maybe 6 and 7.

Or there is intent to maintain it going forward then, methinks, it needs to be brought up to some minimal standards.

There's minimal standards and then there's endian neutrality and non-x86 support for a piece of code that, I think, is probably never going to see qemu.git. There's a big gap between those. I'd love for us to move to vfio, but for now we're stuck with this and I'll try to update it and make it a little better each time. Looking out for big endian hosts isn't high on my priority list for this code though. Thanks,

Alex

#41 lore raw

PATCH 7/9 pci-assign: Allocate entries for all MSI-X vectors

In reply to Alex Williamson

We still only initialize the number used. This lets us avoid address space translations if the guest were to sparsely initialize MSI-X vectors on the device.

---

 hw/device-assignment.c |   44 ++++++++++++++++++++++----------------------
 1 files changed, 22 insertions(+), 22 deletions(-)
hw/device-assignment.c +22 -22
index 6efa219..a6d3a5b 100644
@@ -634,8 +634,11 @@ static void free_dev_irq_entries(AssignedDevice *dev)
 {
     int i;
 
-    for (i = 0; i < dev->irq_entries_nr; i++)
-        kvm_del_routing_entry(&dev->entry[i]);
+    for (i = 0; i < dev->irq_entries_nr; i++) {
+        if (dev->entry[i].type) {
+            kvm_del_routing_entry(&dev->entry[i]);
+        }
+    }
     g_free(dev->entry);
     dev->entry = NULL;
     dev->irq_entries_nr = 0;
@@ -983,7 +986,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         if (entry->data == 0) {
             continue;
         }
-        entries_nr ++;
+        entries_nr++;
     }
 
     if (entries_nr == 0) {
@@ -1000,15 +1003,13 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     }
 
     free_dev_irq_entries(adev);
-    adev->irq_entries_nr = entries_nr;
-    adev->entry = g_malloc0(entries_nr * sizeof(*(adev->entry)));
+
+    adev->irq_entries_nr = adev->msix_max;
+    adev->entry = g_malloc0(adev->msix_max * sizeof(*(adev->entry)));
 
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
-    entries_nr = 0;
     entry = adev->msix_table;
     for (i = 0; i < adev->msix_max; i++, entry++) {
-        if (entries_nr >= msix_nr.entry_nr)
-            break;
         if (entry->data == 0) {
             continue;
         }
@@ -1017,26 +1018,25 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
         if (r < 0)
             return r;
 
-        adev->entry[entries_nr].gsi = r;
-        adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
-        adev->entry[entries_nr].flags = 0;
-        adev->entry[entries_nr].u.msi.address_lo = entry->addr_lo;
-        adev->entry[entries_nr].u.msi.address_hi = entry->addr_hi;
-        adev->entry[entries_nr].u.msi.data = entry->data;
-        DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!",
-              entry->data, entry->addr_lo);
-	kvm_add_routing_entry(&adev->entry[entries_nr]);
-
-        msix_entry.gsi = adev->entry[entries_nr].gsi;
+        adev->entry[i].gsi = r;
+        adev->entry[i].type = KVM_IRQ_ROUTING_MSI;
+        adev->entry[i].flags = 0;
+        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;
+
+        DEBUG("MSI-X vector %d, gsi %d, addr %08x_%08x, data %08x\n", i,
+              r, entry->addr_hi, entry->addr_lo, entry->data);
+
+        kvm_add_routing_entry(&adev->entry[i]);
+
+        msix_entry.gsi = adev->entry[i].gsi;
         msix_entry.entry = i;
         r = kvm_assign_set_msix_entry(kvm_state, &msix_entry);
         if (r) {
             fprintf(stderr, "fail to set MSI-X entry! %s\n", strerror(-r));
             break;
         }
-        DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
-                msix_entry.gsi, msix_entry.entry);
-        entries_nr ++;
     }
 
     if (r == 0 && kvm_commit_irq_routes() < 0) {
#42 lore raw

PATCH 8/9 pci-assign: Use MSIX_PAGE_SIZE

In reply to Alex Williamson

We've already got it defined, use it.

---

 hw/device-assignment.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/device-assignment.c +3 -3
index a6d3a5b..7e52615 100644
@@ -1471,7 +1471,7 @@ static void msix_reset(AssignedDevice *dev)
         return;
     }
 
-    memset(dev->msix_table, 0, 0x1000);
+    memset(dev->msix_table, 0, MSIX_PAGE_SIZE);
 
     for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
         entry->ctrl = 0x1; /* Masked */
@@ -1480,7 +1480,7 @@ static void msix_reset(AssignedDevice *dev)
 
 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
 {
-    dev->msix_table = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE,
+    dev->msix_table = mmap(NULL, MSIX_PAGE_SIZE, PROT_READ|PROT_WRITE,
                            MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
     if (dev->msix_table == MAP_FAILED) {
         fprintf(stderr, "fail allocate msix_table! %s\n", strerror(errno));
@@ -1502,7 +1502,7 @@ static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
 
     memory_region_destroy(&dev->mmio);
 
-    if (munmap(dev->msix_table, 0x1000) == -1) {
+    if (munmap(dev->msix_table, MSIX_PAGE_SIZE) == -1) {
         fprintf(stderr, "error unmapping msix_table! %s\n",
                 strerror(errno));
     }
#43 lore raw

PATCH 9/9 pci-assign: Update MSI-X config based on table writes

In reply to Alex Williamson

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.

---

 hw/device-assignment.c |   93 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 82 insertions(+), 11 deletions(-)
hw/device-assignment.c +82 -11
index 7e52615..8b05fa3 100644
@@ -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 = {
#44 lore raw

Re: [PATCH 9/9] pci-assign: Update MSI-X config based on table writes

Quoted 16 lines · Alex Williamson

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.

hw/device-assignment.c static void assigned_dev_update_msi(PCIDevice *pci_dev) · line 977

Quoted 4 lines
hw/device-assignment.c +4 -0
+static inline bool msix_masked(MSIXTableEntry *entry)
+{
+    return !!(entry->ctrl & 0x1);
+}

!! !needed (nor 'inline').

Signature · 1 line
error compiling committee.c: too many arguments to function
#45 lore raw
In reply to Alex Williamson
Quoted 17 lines · Alex Williamson

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts. The first patch enables exposing MMIO BARs as their native width to the guest. I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there. Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning. We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD. I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series. Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

Quoted 3 lines

I wasn't able to get masking to work reliably, so I left that as is for now. Perhaps someone has suggestions on getting that to work. Thanks,

Unless it's urging, let's focus on getting this implemented via the MSI/MSI-X core, not widely duplicated in device-assignment.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#46 lore raw
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Alex Williamson

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts. The first patch enables exposing MMIO BARs as their native width to the guest. I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there. Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning. We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD. I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series. Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

Quoted 6 lines
Quoted 3 lines

I wasn't able to get masking to work reliably, so I left that as is for now. Perhaps someone has suggestions on getting that to work. Thanks,

Unless it's urging, let's focus on getting this implemented via the MSI/MSI-X core, not widely duplicated in device-assignment.

I disagree. This isn't making the code duplication worse and it solves at least two use cases that are currently broken. This won't make it any more difficult to eventually move to msix.c, if it does, the core needs more work. Thanks,

Alex

#47 lore raw
Quoted 23 lines · Alex Williamson
Quoted 22 lines · Jan Kiszka
Quoted 20 lines · Alex Williamson

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts. The first patch enables exposing MMIO BARs as their native width to the guest. I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there. Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning. We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD. I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series. Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

I wasn't able to get masking to work reliably, so I left that as is for now. Perhaps someone has suggestions on getting that to work. Thanks,

Quoted 8 lines
Quoted 2 lines

Unless it's urging, let's focus on getting this implemented via the MSI/MSI-X core, not widely duplicated in device-assignment.

I disagree. This isn't making the code duplication worse and it solves at least two use cases that are currently broken. This won't make it any more difficult to eventually move to msix.c, if it does, the core needs more work. Thanks,

I agree (with Alex), but maybe I missed something? Patch 9 does call kvm directly instead of going through msi services, but I don't think this should hold the patches.

Signature · 1 line
error compiling committee.c: too many arguments to function
#48 lore raw
Quoted 33 lines · Avi Kivity
Quoted 23 lines · Alex Williamson
Quoted 22 lines · Jan Kiszka
Quoted 20 lines · Alex Williamson

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts. The first patch enables exposing MMIO BARs as their native width to the guest. I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there. Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning. We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD. I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series. Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

I wasn't able to get masking to work reliably, so I left that as is for now. Perhaps someone has suggestions on getting that to work. Thanks,

Quoted 8 lines
Quoted 2 lines

Unless it's urging, let's focus on getting this implemented via the MSI/MSI-X core, not widely duplicated in device-assignment.

I disagree. This isn't making the code duplication worse and it solves at least two use cases that are currently broken. This won't make it any more difficult to eventually move to msix.c, if it does, the core needs more work. Thanks,

Quoted 3 lines

I agree (with Alex), but maybe I missed something? Patch 9 does call kvm directly instead of going through msi services, but I don't think this should hold the patches.

If this solves the issues, ok. That all needs quite some refactoring anyway (including the kvm core services).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#49 lore raw
In reply to Alex Williamson

HI Alex,
I can give it a try. Please send me all patches as one file if possible.
I was about to report yet another problem with Guest MSI-X smp affinity not being honoured by KVM while injecting interrupts. Its again in the context of 82599 device (tried with Linux as guest). I had a hunch that the MSI-X vector data which gets modified when SMP affinity is not tracked by the qemu_kvm. So the interrupt injection was done in load balancing fashion. Because of this interrupt LB in guest VCPUs I saw that packets for all the queues were processed by one VCPU instead 2 or 4 available.
Anyway I will test the the MSI-X affinity fix also and get back with results. And I hope 82599 send/receive works. Thanks for your help.

-Shashidhar

Quoted 40 lines · Alex Williamson

Patch 1 & 2 here are independent of the rest, but I include them here to avoid conflicts.  The first patch enables exposing MMIO BARs as their native width to the guest.  I added a config option for this with the default to use the existing behavior as I suspect we may have some latent issues there.  Patch 2 is just some trivial debug build warning fixes.

The rest of the patches work on improving MSI-X table support. Particularly, vectors can now be updated by the guest after MSI-X is enabled to support things like irqbalance for SMP affinity tuning.  We also now update MSI-X configuration as new vectors are unmasked, which enables assignment of MSI-X devices on FreeBSD.  I was able to assign and use an 82576 (PF & VF) on a FreeBSD 9.0 guest with this series.  Hopefully Shashidhar can report whether this improves the behavior he as seeing with an 82599.

I wasn't able to get masking to work reliably, so I left that as is for now.  Perhaps someone has suggestions on getting that to work.  Thanks,

Alex

---

Alex Williamson (9):
     pci-assign: Update MSI-X config based on table writes
     pci-assign: Use MSIX_PAGE_SIZE
     pci-assign: Allocate entries for all MSI-X vectors
     pci-assign: Proper initialization for MSI-X table
     pci-assign: Only calculate maximum MSI-X vector entries once
     pci-assign: Use struct for MSI-X table
     pci-assign: Update MSI-X MMIO to Memory API
     pci-assign: Fix warnings with DEBUG enabled
     pci-assign: Optionally enable 64bit BARs in guest

 hw/device-assignment.c |  272 ++++++++++++++++++++++++++++++------------------ hw/device-assignment.h |   12 ++

 2 files changed, 179 insertions(+), 105 deletions(-)
#50 lore raw
Quoted 11 lines · Shashidhar Patil

HI Alex,
I can give it a try. Please send me all patches as one file if possible.
I was about to report yet another problem with Guest MSI-X smp affinity not being honoured by KVM while injecting interrupts. Its again in the context of 82599 device (tried with Linux as guest). I had a hunch that the MSI-X vector data which gets modified when SMP affinity is not tracked by the qemu_kvm. So the interrupt injection was done in load balancing fashion. Because of this interrupt LB in guest VCPUs I saw that packets for all the queues were processed by one VCPU instead 2 or 4 available.
Anyway I will test the the MSI-X affinity fix also and get back with results. And I hope 82599 send/receive works. Thanks for your help.

I pushed the v2 version to github, so you can grab it from here for testing:

git://github.com/awilliam/qemu-kvm.git

Branch pci-assign-msix. Thanks,

Alex

#51 lore raw

Hi Alex,
I tested your code changes for two problems
1. MSIX vectors allocation for FreeBSD Guest
Tested the allocation of MSI-X allocation happening for both
FreeBSD-9.0 (also tested 8.2) and Linux guest.
Also tried multiple restart of the guests to see if the hosts
still maintains those allocations. With Linux
as guest I could pass traffic. Though interrupt allocation worked
the FreeBSD could to pass traffic. The FreeBSD
driver seems to be broken(atleast when running in guest).

2. Guest MSI-X smp_affinity fix.
The smp_affinity configured in guest is honoured. The traffic
distribution is uniform. When smp affinity is configured
in host and guest properly this fix provides multi queue and
hence multi core traffic distribution in both host and
guest. The result is good packet throughput.

Thanks for the the much needed fixes.

-Shashidhar

Quoted 22 lines · Alex Williamson
Quoted 11 lines · Shashidhar Patil

HI Alex,
     I can give it a try. Please send me all patches as one file if possible. I was about to report yet another problem with Guest MSI-X smp affinity not being honoured by KVM while injecting interrupts. Its again in the context of 82599 device (tried with Linux as guest). I had a hunch that the MSI-X vector data which gets modified when SMP affinity is not tracked by the qemu_kvm. So the interrupt injection was done in load balancing fashion. Because of this interrupt LB in guest VCPUs I saw that packets for all the queues were processed by one VCPU instead 2 or 4 available.
Anyway I will test the the MSI-X affinity fix also and get back with results. And I hope 82599 send/receive works. Thanks for your help.

I pushed the v2 version to github, so you can grab it from here for testing:

git://github.com/awilliam/qemu-kvm.git

Branch pci-assign-msix.  Thanks,

Alex

#52 lore raw
Quoted 10 lines · Shashidhar Patil

Hi Alex,
I tested your code changes for two problems
1. MSIX vectors allocation for FreeBSD Guest
Tested the allocation of MSI-X allocation happening for both
FreeBSD-9.0 (also tested 8.2) and Linux guest.
Also tried multiple restart of the guests to see if the hosts
still maintains those allocations. With Linux
as guest I could pass traffic. Though interrupt allocation worked
the FreeBSD could to pass traffic. The FreeBSD
driver seems to be broken(atleast when running in guest).

Which device/driver? I was able to use an 82576, both physical device and sr-iov virtual function) with the freebsd 9.0 igb driver.

Quoted 8 lines

2. Guest MSI-X smp_affinity fix.
The smp_affinity configured in guest is honoured. The traffic
distribution is uniform. When smp affinity is configured
in host and guest properly this fix provides multi queue and
hence multi core traffic distribution in both host and
guest. The result is good packet throughput.

Thanks for the the much needed fixes.

Thanks for testing!

Alex

Quoted 26 lines

-Shashidhar

Quoted 22 lines · Alex Williamson
Quoted 11 lines · Shashidhar Patil

HI Alex,
I can give it a try. Please send me all patches as one file if possible.
I was about to report yet another problem with Guest MSI-X smp affinity not being honoured by KVM while injecting interrupts. Its again in the context of 82599 device (tried with Linux as guest). I had a hunch that the MSI-X vector data which gets modified when SMP affinity is not tracked by the qemu_kvm. So the interrupt injection was done in load balancing fashion. Because of this interrupt LB in guest VCPUs I saw that packets for all the queues were processed by one VCPU instead 2 or 4 available.
Anyway I will test the the MSI-X affinity fix also and get back with results. And I hope 82599 send/receive works. Thanks for your help.

I pushed the v2 version to github, so you can grab it from here for testing:

git://github.com/awilliam/qemu-kvm.git

Branch pci-assign-msix. Thanks,

Alex

#53 lore raw

HI Alex,
I tried both FreeBSD stock and Intel official freebsd driver for 82599.
I tried both FreeBSD 9.0 and FreeBSD8.2.
No luck with traffic.

-Shashidhar

Quoted 57 lines · Alex Williamson
Quoted 10 lines · Shashidhar Patil

Hi Alex,
    I tested your code changes for two problems
1. MSIX vectors allocation for FreeBSD Guest
    Tested the allocation of MSI-X allocation happening for both FreeBSD-9.0 (also tested 8.2) and Linux guest.
     Also tried multiple restart of the guests to see if the hosts still maintains those allocations. With Linux
     as guest I could pass traffic. Though interrupt allocation worked the FreeBSD could to pass traffic. The FreeBSD
     driver seems to be broken(atleast when running in guest).

Which device/driver?  I was able to use an 82576, both physical device and sr-iov virtual function) with the freebsd 9.0 igb driver.

Quoted 8 lines

2. Guest MSI-X smp_affinity fix.
     The smp_affinity configured in guest is honoured. The traffic distribution is uniform. When smp affinity is configured in host and guest properly this fix provides multi queue and hence multi core traffic distribution in both host and guest. The result is good packet throughput.

Thanks for the the much needed fixes.

Thanks for testing!

Alex

Quoted 26 lines

-Shashidhar

Quoted 22 lines · Alex Williamson
Quoted 11 lines · Shashidhar Patil

HI Alex,
     I can give it a try. Please send me all patches as one file if possible. I was about to report yet another problem with Guest MSI-X smp affinity not being honoured by KVM while injecting interrupts. Its again in the context of 82599 device (tried with Linux as guest). I had a hunch that the MSI-X vector data which gets modified when SMP affinity is not tracked by the qemu_kvm. So the interrupt injection was done in load balancing fashion. Because of this interrupt LB in guest VCPUs I saw that packets for all the queues were processed by one VCPU instead 2 or 4 available.
Anyway I will test the the MSI-X affinity fix also and get back with results. And I hope 82599 send/receive works. Thanks for your help.

I pushed the v2 version to github, so you can grab it from here for testing:

git://github.com/awilliam/qemu-kvm.git

Branch pci-assign-msix.  Thanks,

Alex