Skip to contentSkip to conversation

Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned device resume failure problem"

PATCH In review Simon Horman 4 participants 18 messages May 2, 2009 – May 8, 2009

lore
Share & tools
lore
#0 lore raw

Changeset "passthrough: Fix older kernel assigned device resume failure problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

[ This problem was previously reported as being in the previous
changeset, "Fix hvm guest cirrus_vga hvm s3 resume failure". Apologies for any confusion that may cause ]

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

In the case of an Intel e100 82557/8/9/0/1 the problem manifests as the EEPROM being declared invalid, although the device seems to be usable by domU.

In the case of an Intel igb 82576 the card is initialised by the guest operating system but is unable to establish a link and ethtool returning invalid values for the supported ports - it tells me the card has fibre when in fact it has copper.

domU is running x86_32 2.6.30-rc4. dom0 is x86_64.

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
#1 lore raw

[ Added Shimada-san to CC list ]

Quoted 24 lines · Simon Horman

Changeset "passthrough: Fix older kernel assigned device resume failure problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

[ This problem was previously reported as being in the previous
changeset, "Fix hvm guest cirrus_vga hvm s3 resume failure". Apologies for any confusion that may cause ]

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

In the case of an Intel e100 82557/8/9/0/1 the problem manifests as the EEPROM being declared invalid, although the device seems to be usable by domU.

In the case of an Intel igb 82576 the card is initialised by the guest operating system but is unable to establish a link and ethtool returning invalid values for the supported ports - it tells me the card has fibre when in fact it has copper.

domU is running x86_32 2.6.30-rc4. dom0 is x86_64.

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
#2 lore raw

Simon Horman writes ("Re: [Xen-devel] Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned device resume failure problem""):

Quoted 5 lines · Simon Horman

Changeset "passthrough: Fix older kernel assigned device resume failure problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

...

Quoted 3 lines

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

Thanks ...

Cui, Dexuan writes ("[PATCH][ioemu] fix PCI bar mapping"):

Quoted 3 lines

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

... Cui, do you think this is likely to fix Simon's problem ?

I'm about to apply it, in any case, but I was wondering if we have another problem too.

Thanks,
Ian.

#3 lore raw
Quoted 19 lines · Ian Jackson

Simon Horman writes ("Re: [Xen-devel] Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned
device resume failure problem""): On Sat, May 02, 2009 at 12:10:15PM

Quoted 7 lines · Simon Horman

Changeset "passthrough: Fix older kernel assigned device resume failure
problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was
applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

...

Quoted 3 lines

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

Thanks ...

Cui, Dexuan writes ("[PATCH][ioemu] fix PCI bar mapping"):

Quoted 8 lines
Quoted 6 lines

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

... Cui, do you think this is likely to fix Simon's problem ?

Yes, I think this should fix Simon's problem since actually I meet with the same symptom and I made the patch to fix it.

Quoted 3 lines

I'm about to apply it, in any case, but I was wondering if we have another problem too.

I don't see another problem.
Yes, please apply it. Thanks!

Thanks,
-- Dexuan

#4 lore raw
Quoted 27 lines · Dexuan
Quoted 26 lines · Ian Jackson

Simon Horman writes ("Re: [Xen-devel] Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned
device resume failure problem""): On Sat, May 02, 2009 at 12:10:15PM

Quoted 7 lines · Simon Horman

Changeset "passthrough: Fix older kernel assigned device resume failure
problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was
applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

...

Quoted 3 lines

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

Thanks ...

Cui, Dexuan writes ("[PATCH][ioemu] fix PCI bar mapping"):

Quoted 6 lines

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

Quoted 2 lines
Quoted 1 line

... Cui, do you think this is likely to fix Simon's problem ?

Yes, I think this should fix Simon's problem since actually I meet with the same symptom and I made the patch to fix it.

Hi,

I did some testing this morning and Dexuan's patch appears to solve the problem that I was seeing.

Thanks

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
#5 lore raw

Simon, thanks a lot for the testing!
BTW, the patch has been checked into staging ioemu-remote tree(http://xenbits.xensource.com/git-http/staging/qemu-xen-unstable.git).

Thanks,
-- Dexuan

-----Original Message-----
From: Simon Horman [mailto:[email protected]]
Sent: 2009年5月6日 7:18
To: Cui, Dexuan
Cc: Ian Jackson; [email protected]; Zhao, Yu; Keir Fraser; Ke, Liping; Yuji Shimada
Subject: Re: [Xen-devel] Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned device resume failure problem"

Quoted 27 lines · Dexuan
Quoted 26 lines · Ian Jackson

Simon Horman writes ("Re: [Xen-devel] Regression in PCI pass-through from qemu-xen changeset "passthrough: Fix older kernel assigned
device resume failure problem""): On Sat, May 02, 2009 at 12:10:15PM

Quoted 7 lines · Simon Horman

Changeset "passthrough: Fix older kernel assigned device resume failure
problem" (dbb8aafa702b8b4f5568e08641d98471fd04e0f8) in qemu-xen-unstable.git appears to cause a regression. This changeset was
applied between the xen-3.4.0-rc2 and xen-3.4.0-rc3 tags of qemu-xen-unstable.git.

...

Quoted 3 lines

The problem that I am seeing is that with this changeset applied I am not able to correctly initialise Intel network cards. Though I assume the problem is not specific to this hardware.

Thanks ...

Cui, Dexuan writes ("[PATCH][ioemu] fix PCI bar mapping"):

Quoted 6 lines

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

Quoted 2 lines
Quoted 1 line

... Cui, do you think this is likely to fix Simon's problem ?

Yes, I think this should fix Simon's problem since actually I meet with the same symptom and I made the patch to fix it.

Hi,

I did some testing this morning and Dexuan's patch appears to solve the problem that I was seeing.

Thanks

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
#6 lore raw
Quoted 2 lines · Dexuan

Simon, thanks a lot for the testing!
BTW, the patch has been checked into staging ioemu-remote tree(http://xenbits.xensource.com/git-http/staging/qemu-xen-unstable.git).

Thanks, I have also confirmed that the staging tree is working for me.

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
#7 lore raw

PATCH 1/1 fix PCI bar mapping

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

hw/pass-through.c +63 -50
index 6a53137..d2bed51 100644
@@ -1791,64 +1791,74 @@ out:
 }
 
 /* mapping BAR */
-static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
+    int mem_enable)
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
-    int i;
 
-    for (i=0; i<PCI_NUM_REGIONS; i++)
-    {
-        r = &dev->io_regions[i];
+    r = &dev->io_regions[bar];
 
-        /* check valid region */
-        if (!r->size)
-            continue;
+    /* check valid region */
+    if (!r->size)
+        return;
 
-        base = &ptdev->bases[i];
-        /* skip unused BAR or upper 64bit BAR */
-        if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
-           (base->bar_flag == PT_BAR_FLAG_UPPER))
-               continue;
+    base = &ptdev->bases[bar];
+    /* skip unused BAR or upper 64bit BAR */
+    if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
+       (base->bar_flag == PT_BAR_FLAG_UPPER))
+           return;
 
-        /* copy region address to temporary */
-        r_addr = r->addr;
+    /* copy region address to temporary */
+    r_addr = r->addr;
 
-        /* need unmapping in case I/O Space or Memory Space disable */
-        if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
-            ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+    /* need unmapping in case I/O Space or Memory Space disable */
+    if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
+        ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+        r_addr = -1;
+    if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
+    {
+        uint32_t rom_reg;
+        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
+        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
             r_addr = -1;
+    }
 
-        /* prevent guest software mapping memory resource to 00000000h */
-        if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
-            r_addr = -1;
+    /* prevent guest software mapping memory resource to 00000000h */
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+        r_addr = -1;
 
-        /* align resource size (memory type only) */
-        r_size = r->size;
-        PT_GET_EMUL_SIZE(base->bar_flag, r_size);
-
-        /* check overlapped address */
-        ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
-                        r_addr, r_size, r->type);
-        if (ret > 0)
-            PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
-                "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
-                (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
-                i, r_addr, r_size);
-
-        /* check whether we need to update the mapping or not */
-        if (r_addr != ptdev->bases[i].e_physbase)
-        {
-            /* mapping BAR */
-            r->map_func((PCIDevice *)ptdev, i, r_addr,
-                         r_size, r->type);
-        }
+    /* align resource size (memory type only) */
+    r_size = r->size;
+    PT_GET_EMUL_SIZE(base->bar_flag, r_size);
+
+    /* check overlapped address */
+    ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
+                    r_addr, r_size, r->type);
+    if (ret > 0)
+        PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
+            "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
+            (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
+            bar, r_addr, r_size);
+
+    /* check whether we need to update the mapping or not */
+    if (r_addr != ptdev->bases[bar].e_physbase)
+    {
+        /* mapping BAR */
+        r->map_func((PCIDevice *)ptdev, bar, r_addr,
+                     r_size, r->type);
     }
+}
 
-    return;
+static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+{
+    int i;
+
+    for (i=0; i<PCI_NUM_REGIONS; i++)
+        pt_bar_mapping_one(ptdev, i, io_enable, mem_enable);
 }
 
 /* check power state transition */
@@ -3051,6 +3061,7 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
+    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3170,14 +3181,10 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
-    if (reg_grp_entry)
-    {
-        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
-        if (reg_entry)
-            pt_bar_mapping(ptdev, reg_entry->data & PCI_COMMAND_IO,
-                                  reg_entry->data & PCI_COMMAND_MEMORY);
-    }
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
@@ -3195,6 +3202,7 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
+    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3217,6 +3225,11 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     throughable_mask = ~bar_emu_mask & valid_mask;
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
+    /* After BAR reg update, we need to remap BAR*/
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
hw/pass-through.c +63 -50

159 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#8 lore raw

Re: [PATCH][ioemu] fix PCI bar mapping

Quoted 3 lines · Dexuan

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

Hi Cui,

pt_pci_read_config should not be used to read configuration registers. pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pc_read_condign. So side effects may occur. Instead, you can use pc_read_word of libpci just to read configuration registers.

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_CD_reg_write. Then you can get the proper value from reg_entry->date.

Thanks,
--
Yuji Shimada

Quoted 175 lines

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

hw/pass-through.c +63 -50
index 6a53137..d2bed51 100644
@@ -1791,64 +1791,74 @@ out:
 }
 
 /* mapping BAR */
-static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
+    int mem_enable)
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
-    int i;
 
-    for (i=0; i<PCI_NUM_REGIONS; i++)
-    {
-        r = &dev->io_regions[i];
+    r = &dev->io_regions[bar];
 
-        /* check valid region */
-        if (!r->size)
-            continue;
+    /* check valid region */
+    if (!r->size)
+        return;
 
-        base = &ptdev->bases[i];
-        /* skip unused BAR or upper 64bit BAR */
-        if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
-           (base->bar_flag == PT_BAR_FLAG_UPPER))
-               continue;
+    base = &ptdev->bases[bar];
+    /* skip unused BAR or upper 64bit BAR */
+    if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
+       (base->bar_flag == PT_BAR_FLAG_UPPER))
+           return;
 
-        /* copy region address to temporary */
-        r_addr = r->addr;
+    /* copy region address to temporary */
+    r_addr = r->addr;
 
-        /* need unmapping in case I/O Space or Memory Space disable */
-        if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
-            ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+    /* need unmapping in case I/O Space or Memory Space disable */
+    if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
+        ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+        r_addr = -1;
+    if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
+    {
+        uint32_t rom_reg;
+        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
+        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
             r_addr = -1;
+    }
 
-        /* prevent guest software mapping memory resource to 00000000h */
-        if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
-            r_addr = -1;
+    /* prevent guest software mapping memory resource to 00000000h */
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+        r_addr = -1;
 
-        /* align resource size (memory type only) */
-        r_size = r->size;
-        PT_GET_EMUL_SIZE(base->bar_flag, r_size);
-
-        /* check overlapped address */
-        ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
-                        r_addr, r_size, r->type);
-        if (ret > 0)
-            PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
-                "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
-                (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
-                i, r_addr, r_size);
-
-        /* check whether we need to update the mapping or not */
-        if (r_addr != ptdev->bases[i].e_physbase)
-        {
-            /* mapping BAR */
-            r->map_func((PCIDevice *)ptdev, i, r_addr,
-                         r_size, r->type);
-        }
+    /* align resource size (memory type only) */
+    r_size = r->size;
+    PT_GET_EMUL_SIZE(base->bar_flag, r_size);
+
+    /* check overlapped address */
+    ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
+                    r_addr, r_size, r->type);
+    if (ret > 0)
+        PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
+            "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
+            (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
+            bar, r_addr, r_size);
+
+    /* check whether we need to update the mapping or not */
+    if (r_addr != ptdev->bases[bar].e_physbase)
+    {
+        /* mapping BAR */
+        r->map_func((PCIDevice *)ptdev, bar, r_addr,
+                     r_size, r->type);
     }
+}
 
-    return;
+static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+{
+    int i;
+
+    for (i=0; i<PCI_NUM_REGIONS; i++)
+        pt_bar_mapping_one(ptdev, i, io_enable, mem_enable);
 }
 
 /* check power state transition */
@@ -3051,6 +3061,7 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
+    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3170,14 +3181,10 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
-    if (reg_grp_entry)
-    {
-        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
-        if (reg_entry)
-            pt_bar_mapping(ptdev, reg_entry->data & PCI_COMMAND_IO,
-                                  reg_entry->data & PCI_COMMAND_MEMORY);
-    }
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
@@ -3195,6 +3202,7 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
+    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3217,6 +3225,11 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     throughable_mask = ~bar_emu_mask & valid_mask;
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
+    /* After BAR reg update, we need to remap BAR*/
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
#9 lore raw

Re: [PATCH][ioemu] fix PCI bar mapping

In reply to Cui, Dexuan
Quoted 3 lines · Dexuan

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

* There were some typo in my comment. I resend it.

Hi Cui,

pt_pci_read_config should not be used to read configuration registers. pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pci_read_config. So side effects may occur. Instead, you can use pc_read_word of libpci just to read configuration registers.

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

Thanks,
--
Yuji Shimada

Quoted 175 lines

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

hw/pass-through.c +63 -50
index 6a53137..d2bed51 100644
@@ -1791,64 +1791,74 @@ out:
 }
 
 /* mapping BAR */
-static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
+    int mem_enable)
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
-    int i;
 
-    for (i=0; i<PCI_NUM_REGIONS; i++)
-    {
-        r = &dev->io_regions[i];
+    r = &dev->io_regions[bar];
 
-        /* check valid region */
-        if (!r->size)
-            continue;
+    /* check valid region */
+    if (!r->size)
+        return;
 
-        base = &ptdev->bases[i];
-        /* skip unused BAR or upper 64bit BAR */
-        if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
-           (base->bar_flag == PT_BAR_FLAG_UPPER))
-               continue;
+    base = &ptdev->bases[bar];
+    /* skip unused BAR or upper 64bit BAR */
+    if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
+       (base->bar_flag == PT_BAR_FLAG_UPPER))
+           return;
 
-        /* copy region address to temporary */
-        r_addr = r->addr;
+    /* copy region address to temporary */
+    r_addr = r->addr;
 
-        /* need unmapping in case I/O Space or Memory Space disable */
-        if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
-            ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+    /* need unmapping in case I/O Space or Memory Space disable */
+    if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
+        ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+        r_addr = -1;
+    if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
+    {
+        uint32_t rom_reg;
+        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
+        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
             r_addr = -1;
+    }
 
-        /* prevent guest software mapping memory resource to 00000000h */
-        if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
-            r_addr = -1;
+    /* prevent guest software mapping memory resource to 00000000h */
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+        r_addr = -1;
 
-        /* align resource size (memory type only) */
-        r_size = r->size;
-        PT_GET_EMUL_SIZE(base->bar_flag, r_size);
-
-        /* check overlapped address */
-        ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
-                        r_addr, r_size, r->type);
-        if (ret > 0)
-            PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
-                "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
-                (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
-                i, r_addr, r_size);
-
-        /* check whether we need to update the mapping or not */
-        if (r_addr != ptdev->bases[i].e_physbase)
-        {
-            /* mapping BAR */
-            r->map_func((PCIDevice *)ptdev, i, r_addr,
-                         r_size, r->type);
-        }
+    /* align resource size (memory type only) */
+    r_size = r->size;
+    PT_GET_EMUL_SIZE(base->bar_flag, r_size);
+
+    /* check overlapped address */
+    ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
+                    r_addr, r_size, r->type);
+    if (ret > 0)
+        PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
+            "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
+            (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
+            bar, r_addr, r_size);
+
+    /* check whether we need to update the mapping or not */
+    if (r_addr != ptdev->bases[bar].e_physbase)
+    {
+        /* mapping BAR */
+        r->map_func((PCIDevice *)ptdev, bar, r_addr,
+                     r_size, r->type);
     }
+}
 
-    return;
+static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+{
+    int i;
+
+    for (i=0; i<PCI_NUM_REGIONS; i++)
+        pt_bar_mapping_one(ptdev, i, io_enable, mem_enable);
 }
 
 /* check power state transition */
@@ -3051,6 +3061,7 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
+    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3170,14 +3181,10 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
-    if (reg_grp_entry)
-    {
-        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
-        if (reg_entry)
-            pt_bar_mapping(ptdev, reg_entry->data & PCI_COMMAND_IO,
-                                  reg_entry->data & PCI_COMMAND_MEMORY);
-    }
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
@@ -3195,6 +3202,7 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
+    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3217,6 +3225,11 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     throughable_mask = ~bar_emu_mask & valid_mask;
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
+    /* After BAR reg update, we need to remap BAR*/
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
#10 lore raw

Re: [PATCH][ioemu] fix PCI bar mapping

Yuji Shimada writes ("Re: [PATCH][ioemu] fix PCI bar mapping"):

Quoted 5 lines

pt_pci_read_config should not be used to read configuration registers. pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pci_read_config. So side effects may occur. Instead, you can use pc_read_word of libpci just to read configuration registers.

Should we be reverting Cui's patch and fix the problem some other way ?

Quoted 3 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

It would be nice to get this sorted for the 3.4 release, which is imminent ...

Ian.

#11 lore raw

RE: [PATCH][ioemu] fix PCI bar mapping

Quoted 10 lines · Ian Jackson

Yuji Shimada writes ("Re: [PATCH][ioemu] fix PCI bar mapping"):

Quoted 6 lines

pt_pci_read_config should not be used to read configuration registers. pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pci_read_config. So side effects may occur. Instead, you can use pc_read_word of libpci just to read configuration registers.

Should we be reverting Cui's patch and fix the problem some other way ?

I have posted a new patch according to Yuji's comment.

Quoted 4 lines
Quoted 4 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

Please help to review my newly-posted patch for this approach.

Quoted 5 lines

It would be nice to get this sorted for the 3.4 release, which is imminent ...

Ian.

Thanks,
-- Dexuan

#12 lore raw

RE: [PATCH][ioemu] fix PCI bar mapping

In reply to Yuji Shimada

Hi Yuji,
Thanks a lot for the comments!

Quoted 3 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

I prefer this approach.
Attached is the patch. Could you help to have a review?

Thanks,
-- Dexuan

-----Original Message-----
From: Yuji Shimada [mailto:[email protected]]
Sent: 2009年5月7日 15:40
To: Cui, Dexuan; Ian Jackson
Cc: Ke, Liping; Zhao, Yu; xen-devel
Subject: Re: [PATCH][ioemu] fix PCI bar mapping

Quoted 3 lines · Dexuan

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

* There were some typo in my comment. I resend it.

Hi Cui,

pt_pci_read_config should not be used to read configuration registers. pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pci_read_config. So side effects may occur. Instead, you can use pc_read_word of libpci just to read configuration registers.

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

Thanks,
--
Yuji Shimada

Quoted 165 lines

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

hw/pass-through.c +58 -50
index 6a53137..d2bed51 100644
@@ -1791,64 +1791,74 @@ out:
 }
 
 /* mapping BAR */
-static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
+    int mem_enable)
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
-    int i;
 
-    for (i=0; i<PCI_NUM_REGIONS; i++)
-    {
-        r = &dev->io_regions[i];
+    r = &dev->io_regions[bar];
 
-        /* check valid region */
-        if (!r->size)
-            continue;
+    /* check valid region */
+    if (!r->size)
+        return;
 
-        base = &ptdev->bases[i];
-        /* skip unused BAR or upper 64bit BAR */
-        if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
-           (base->bar_flag == PT_BAR_FLAG_UPPER))
-               continue;
+    base = &ptdev->bases[bar];
+    /* skip unused BAR or upper 64bit BAR */
+    if ((base->bar_flag == PT_BAR_FLAG_UNUSED) ||
+       (base->bar_flag == PT_BAR_FLAG_UPPER))
+           return;
 
-        /* copy region address to temporary */
-        r_addr = r->addr;
+    /* copy region address to temporary */
+    r_addr = r->addr;
 
-        /* need unmapping in case I/O Space or Memory Space disable */
-        if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
-            ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+    /* need unmapping in case I/O Space or Memory Space disable */
+    if (((base->bar_flag == PT_BAR_FLAG_IO) && !io_enable ) ||
+        ((base->bar_flag == PT_BAR_FLAG_MEM) && !mem_enable ))
+        r_addr = -1;
+    if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
+    {
+        uint32_t rom_reg;
+        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
+        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
             r_addr = -1;
+    }
 
-        /* prevent guest software mapping memory resource to 00000000h */
-        if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
-            r_addr = -1;
+    /* prevent guest software mapping memory resource to 00000000h */
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+        r_addr = -1;
 
-        /* align resource size (memory type only) */
-        r_size = r->size;
-        PT_GET_EMUL_SIZE(base->bar_flag, r_size);
-
-        /* check overlapped address */
-        ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
-                        r_addr, r_size, r->type);
-        if (ret > 0)
-            PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
-                "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
-                (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
-                i, r_addr, r_size);
-
-        /* check whether we need to update the mapping or not */
-        if (r_addr != ptdev->bases[i].e_physbase)
-        {
-            /* mapping BAR */
-            r->map_func((PCIDevice *)ptdev, i, r_addr,
-                         r_size, r->type);
-        }
+    /* align resource size (memory type only) */
+    r_size = r->size;
+    PT_GET_EMUL_SIZE(base->bar_flag, r_size);
+
+    /* check overlapped address */
+    ret = pt_chk_bar_overlap(dev->bus, dev->devfn,
+                    r_addr, r_size, r->type);
+    if (ret > 0)
+        PT_LOG("Warning: ptdev[%02x:%02x.%x][Region:%d][Address:%08xh]"
+            "[Size:%08xh] is overlapped.\n", pci_bus_num(dev->bus),
+            (dev->devfn >> 3) & 0x1F, (dev->devfn & 0x7),
+            bar, r_addr, r_size);
+
+    /* check whether we need to update the mapping or not */
+    if (r_addr != ptdev->bases[bar].e_physbase)
+    {
+        /* mapping BAR */
+        r->map_func((PCIDevice *)ptdev, bar, r_addr,
+                     r_size, r->type);
     }
+}
 
-    return;
+static void pt_bar_mapping(struct pt_dev *ptdev, int io_enable, int mem_enable)
+{
+    int i;
+
+    for (i=0; i<PCI_NUM_REGIONS; i++)
+        pt_bar_mapping_one(ptdev, i, io_enable, mem_enable);
 }
 
 /* check power state transition */
@@ -3051,6 +3061,7 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
+    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3170,14 +3181,10 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
-    if (reg_grp_entry)
-    {
-        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
-        if (reg_entry)
-            pt_bar_mapping(ptdev, reg_entry->data & PCI_COMMAND_IO,
-                                  reg_entry->data & PCI_COMMAND_MEMORY);
-    }
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
@@ -3195,6 +3202,7 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
+    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3217,6 +3225,11 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     throughable_mask = ~bar_emu_mask & valid_mask;

hw/pass-through.c static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev, · line 3235

Quoted 10 lines
hw/pass-through.c +5 -0
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
+    /* After BAR reg update, we need to remap BAR*/
+    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
+    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
+        cmd & PCI_COMMAND_MEMORY);
+
     return 0;
 }
 
hw/pass-through.c +31 -15
index d2bed51..51a39db 100644
@@ -1796,6 +1796,8 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
+    struct pt_reg_grp_tbl *reg_grp_entry = NULL;
+    struct pt_reg_tbl *reg_entry = NULL;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
@@ -1821,10 +1823,13 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
         r_addr = -1;
     if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
     {
-        uint32_t rom_reg;
-        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
-        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
-            r_addr = -1;
+        reg_grp_entry = pt_find_reg_grp(ptdev, PCI_ROM_ADDRESS);
+        if (reg_grp_entry)
+        {
+            reg_entry = pt_find_reg(reg_grp_entry, PCI_ROM_ADDRESS);
+            if (reg_entry && !(reg_entry->data & PCI_ROM_ADDRESS_ENABLE))
+                r_addr = -1;
+        }
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
@@ -3011,7 +3016,7 @@ static int pt_cmd_reg_write(struct pt_dev *ptdev,
         emu_mask |= PCI_COMMAND_MEMORY;
 
     /* modify emulate register */
-    writable_mask = emu_mask & ~reg->ro_mask & valid_mask;
+    writable_mask = ~reg->ro_mask & valid_mask;
     cfg_entry->data = PT_MERGE_VALUE(*value, cfg_entry->data, writable_mask);
 
     /* create value for writing to I/O device register */
@@ -3061,7 +3066,6 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
-    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3181,9 +3185,14 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
-    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
-        cmd & PCI_COMMAND_MEMORY);
+    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
+    if (reg_grp_entry)
+    {
+        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
+        if (reg_entry)
+            pt_bar_mapping_one(ptdev, index, reg_entry->data & PCI_COMMAND_IO,
+                               reg_entry->data & PCI_COMMAND_MEMORY);
+    }
 
     return 0;
 }
@@ -3194,6 +3203,8 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
         uint32_t *value, uint32_t dev_value, uint32_t valid_mask)
 {
     struct pt_reg_info_tbl *reg = cfg_entry->reg;
+    struct pt_reg_grp_tbl *reg_grp_entry = NULL;
+    struct pt_reg_tbl *reg_entry = NULL;
     struct pt_region *base = NULL;
     PCIDevice *d = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
@@ -3202,7 +3213,6 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
-    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3212,10 +3222,10 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
 
     /* set emulate mask and read-only mask */
     bar_emu_mask = reg->emu_mask;
-    bar_ro_mask = reg->ro_mask | (r_size - 1);
+    bar_ro_mask = (reg->ro_mask | (r_size - 1)) & ~PCI_ROM_ADDRESS_ENABLE;
 
     /* modify emulate register */
-    writable_mask = bar_emu_mask & ~bar_ro_mask & valid_mask;
+    writable_mask = ~bar_ro_mask & valid_mask;
     cfg_entry->data = PT_MERGE_VALUE(*value, cfg_entry->data, writable_mask);
 
     /* update the corresponding virtual region address */
@@ -3226,9 +3236,15 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
-    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
-        cmd & PCI_COMMAND_MEMORY);
+    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
+    if (reg_grp_entry)
+    {
+        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
+        if (reg_entry)
+            pt_bar_mapping_one(ptdev, PCI_ROM_SLOT,
+                               reg_entry->data & PCI_COMMAND_IO,
+                               reg_entry->data & PCI_COMMAND_MEMORY);
+    }
 
     return 0;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#13 lore raw

Re: [PATCH][ioemu] fix PCI bar mapping

Quoted 12 lines · Dexuan

Hi Yuji,
Thanks a lot for the comments!

Quoted 3 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

I prefer this approach.
Attached is the patch. Could you help to have a review?

Thanks,
-- Dexuan

Hi Cui,

Thanks for sending the patch.

The patch seems to be good.

Thanks,
--
Yuji Shimada

#14 lore raw

RE: [PATCH][ioemu] fix PCI bar mapping

Thanks a lot for Yuji's review!

Anyway, looks this new patch doesn't handle properly the case of SR/IOV VF. I'm improving this and will send out a new patch ASAP. Sorry.

Thanks,
-- Dexuan

-----Original Message-----
From: Yuji Shimada [mailto:[email protected]]
Sent: 2009年5月8日 8:47
To: Cui, Dexuan; Ian Jackson
Cc: Ke, Liping; Zhao, Yu; xen-devel
Subject: Re: [PATCH][ioemu] fix PCI bar mapping

Quoted 12 lines · Dexuan

Hi Yuji,
Thanks a lot for the comments!

Quoted 3 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

I prefer this approach.
Attached is the patch. Could you help to have a review?

Thanks,
-- Dexuan

Hi Cui,

Thanks for sending the patch.

The patch seems to be good.

Thanks,
--
Yuji Shimada

#15 lore raw

RE: [PATCH][ioemu] fix PCI bar mapping

Quoted 32 lines · Dexuan

Thanks a lot for Yuji's review!

Anyway, looks this new patch doesn't handle properly the case of SR/IOV VF. I'm improving this and will send out a new patch ASAP. Sorry.

Thanks,
-- Dexuan

-----Original Message-----
From: Yuji Shimada [mailto:[email protected]]
Sent: 2009年5月8日 8:47
To: Cui, Dexuan; Ian Jackson
Cc: Ke, Liping; Zhao, Yu; xen-devel
Subject: Re: [PATCH][ioemu] fix PCI bar mapping

Quoted 12 lines · Dexuan

Hi Yuji,
Thanks a lot for the comments!

Quoted 3 lines

Or, there is another approach. It is that you remove emu_mask from writable_mask in pt_cmd_reg_write. Then you can get the proper value from reg_entry->data.

I prefer this approach.
Attached is the patch. Could you help to have a review?

Thanks,
-- Dexuan

Quoted 7 lines

Hi Cui,

Thanks for sending the patch.

The patch seems to be good.

Thanks,

Hi all,
Sorry, my previous mail turns out to be a false alarm. :-)
Actually SR-IOV VF can still work fine with the patch. I also make some other tests and it works fine.

So, Ian, please apply the pach.
Let me paste the patch below for your convenience:

--------------------------------------

passthrough: pt_bar_mapping: use a better way to get the CMD value

The pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) in 5d767b7b3fac52336f59e5b40d8befa6b1909937 is not proper as Yuji Shimada points out: "pt_pci_read_config emulates access to read the registers from guest software. Many functions which are not relevant are executed in pt_pci_read_config. So side effects may occur"; instead, we can "remove emu_mask from writable_mask in pt_cmd_reg_write and then we can get the proper value from reg_entry->data". Thanks for Yuji's review and Simon Horman's test.

hw/pass-through.c +31 -15
index d2bed51..51a39db 100644
@@ -1796,6 +1796,8 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
 {
     PCIDevice *dev = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
+    struct pt_reg_grp_tbl *reg_grp_entry = NULL;
+    struct pt_reg_tbl *reg_entry = NULL;
     struct pt_region *base = NULL;
     uint32_t r_size = 0, r_addr = -1;
     int ret = 0;
@@ -1821,10 +1823,13 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
         r_addr = -1;
     if ( (bar == PCI_ROM_SLOT) && (r_addr != -1) )
     {
-        uint32_t rom_reg;
-        rom_reg = pt_pci_read_config(&ptdev->dev, PCI_ROM_ADDRESS, 4);
-        if ( !(rom_reg & PCI_ROM_ADDRESS_ENABLE) )
-            r_addr = -1;
+        reg_grp_entry = pt_find_reg_grp(ptdev, PCI_ROM_ADDRESS);
+        if (reg_grp_entry)
+        {
+            reg_entry = pt_find_reg(reg_grp_entry, PCI_ROM_ADDRESS);
+            if (reg_entry && !(reg_entry->data & PCI_ROM_ADDRESS_ENABLE))
+                r_addr = -1;
+        }
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
@@ -3011,7 +3016,7 @@ static int pt_cmd_reg_write(struct pt_dev *ptdev,
         emu_mask |= PCI_COMMAND_MEMORY;
 
     /* modify emulate register */
-    writable_mask = emu_mask & ~reg->ro_mask & valid_mask;
+    writable_mask = ~reg->ro_mask & valid_mask;
     cfg_entry->data = PT_MERGE_VALUE(*value, cfg_entry->data, writable_mask);
 
     /* create value for writing to I/O device register */
@@ -3061,7 +3066,6 @@ static int pt_bar_reg_write(struct pt_dev *ptdev,
     uint32_t prev_offset;
     uint32_t r_size = 0;
     int index = 0;
-    uint16_t cmd;
 
     /* get BAR index */
     index = pt_bar_offset_to_index(reg->offset);
@@ -3181,9 +3185,14 @@ exit:
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
-    pt_bar_mapping_one(ptdev, index, cmd & PCI_COMMAND_IO,
-        cmd & PCI_COMMAND_MEMORY);
+    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
+    if (reg_grp_entry)
+    {
+        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
+        if (reg_entry)
+            pt_bar_mapping_one(ptdev, index, reg_entry->data & PCI_COMMAND_IO,
+                               reg_entry->data & PCI_COMMAND_MEMORY);
+    }
 
     return 0;
 }
@@ -3194,6 +3203,8 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
         uint32_t *value, uint32_t dev_value, uint32_t valid_mask)
 {
     struct pt_reg_info_tbl *reg = cfg_entry->reg;
+    struct pt_reg_grp_tbl *reg_grp_entry = NULL;
+    struct pt_reg_tbl *reg_entry = NULL;
     struct pt_region *base = NULL;
     PCIDevice *d = (PCIDevice *)&ptdev->dev;
     PCIIORegion *r;
@@ -3202,7 +3213,6 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     uint32_t r_size = 0;
     uint32_t bar_emu_mask = 0;
     uint32_t bar_ro_mask = 0;
-    uint16_t cmd;
 
     r = &d->io_regions[PCI_ROM_SLOT];
     r_size = r->size;
@@ -3212,10 +3222,10 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
 
     /* set emulate mask and read-only mask */
     bar_emu_mask = reg->emu_mask;
-    bar_ro_mask = reg->ro_mask | (r_size - 1);
+    bar_ro_mask = (reg->ro_mask | (r_size - 1)) & ~PCI_ROM_ADDRESS_ENABLE;
 
     /* modify emulate register */
-    writable_mask = bar_emu_mask & ~bar_ro_mask & valid_mask;
+    writable_mask = ~bar_ro_mask & valid_mask;
     cfg_entry->data = PT_MERGE_VALUE(*value, cfg_entry->data, writable_mask);
 
     /* update the corresponding virtual region address */
@@ -3226,9 +3236,15 @@ static int pt_exp_rom_bar_reg_write(struct pt_dev *ptdev,
     *value = PT_MERGE_VALUE(*value, dev_value, throughable_mask);
 
     /* After BAR reg update, we need to remap BAR*/
-    cmd = pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2);
-    pt_bar_mapping_one(ptdev, PCI_ROM_SLOT, cmd & PCI_COMMAND_IO,
-        cmd & PCI_COMMAND_MEMORY);
+    reg_grp_entry = pt_find_reg_grp(ptdev, PCI_COMMAND);
+    if (reg_grp_entry)
+    {
+        reg_entry = pt_find_reg(reg_grp_entry, PCI_COMMAND);
+        if (reg_entry)
+            pt_bar_mapping_one(ptdev, PCI_ROM_SLOT,
+                               reg_entry->data & PCI_COMMAND_IO,
+                               reg_entry->data & PCI_COMMAND_MEMORY);
+    }
 
     return 0;
 }
author Tested-by on v1 1/1
#16 lore raw

Re: [PATCH][ioemu] fix PCI bar mapping

In reply to Cui, Dexuan
Quoted 9 lines · Dexuan

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

Hi,

this patch also seems to resolve the problem that I was seeing where Intel NICs were not usable.

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
Tested-by Simon Horman
#17 lore raw

RE: [PATCH][ioemu] fix PCI bar mapping

Hi Simon, thanks for helping testing it!

Thanks,
-- Dexuan

-----Original Message-----
From: Simon Horman [mailto:[email protected]]
Sent: 2009年5月8日 10:06
To: Cui, Dexuan
Cc: Ian Jackson; Yuji Shimada; Zhao, Yu; xen-devel; Ke, Liping
Subject: Re: [Xen-devel] [PATCH][ioemu] fix PCI bar mapping

Quoted 9 lines · Dexuan

dbb8aafa702b8b4f5568e08641d98471fd04e0f8 has a bug:
The virtual CMD value we get from reg_entry->data is not the proper value because reg_entry->data only holds the emulated bits and the PCI_COMMAND_IO/PCI_COMMAND_MEMORY bits are not in it. Instead, we can use pt_pci_read_config(&ptdev->dev, PCI_COMMAND, 2) to get the proper value.

We should only update the mapping of the related BAR, NOT the mappings of ALL BARs.

In pt_exp_rom_bar_reg_write(), we should also update the mapping. And for PCI_ROM_SLOT, when the PCI_ROM_ADDRESS_ENABLE bit is 0, we should not have the mapping.

Hi,

this patch also seems to resolve the problem that I was seeing where Intel NICs were not usable.

Signature · 3 lines
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en