Skip to contentSkip to conversation

Handle wrap around in limit calculation

PATCH Superseded Shlomo Pongratz 4 participants 5 messages Jan 21, 2024 – Jan 22, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Handle wrap around in limit calculation

From: Shlomo Pongratz <[email protected]>

Hanlde wrap around when calculating the viewport size
caused by the fact that perior to version 460A the limit variable was 32bit quantity and not 64 bits quantity.
In the i.MX 6Dual/6Quad Applications Processor Reference Manual document on which the original code was based upon in the description of the iATU Region Upper Base Address Register it is written:
Forms bits [63:32] of the start (and end) address of the address region to be translated.
That is in this register is the upper of both base and the limit. In the current implementation this value was ignored for the limit which caused a wrap around of the size calculaiton.
Using the documnet example:
Base HI: 0x80000000 Base LO: 0xd0000000 Limit LO: 0xd000ffff
The correct size is 0x80000000d000ffff - 0x80000000d0000000 + 1 =
0x010000
The wrong result is 0xd000ffff - 0x80000000d0000000 + 1 = 0x8000000000010000

----

Changes since v2:
* Don't try to fix the calculation.
* Change the limit variable from 32bit to 64 bit
* Set the limit bits [63:32] same as the base according to the specification on which the original code was base upon.

Changes since v1:
* Seperate subject and description

---
 hw/pci-host/designware.c         | 19 ++++++++++++++-----
 include/hw/pci-host/designware.h |  2 +-
 2 files changed, 15 insertions(+), 6 deletions(-)
hw/pci-host/designware.c +14 -5
index dd9e389c07..43cba9432f 100644
@@ -269,7 +269,7 @@ static void designware_pcie_update_viewport(DesignwarePCIERoot *root,
 {
     const uint64_t target = viewport->target;
     const uint64_t base   = viewport->base;
-    const uint64_t size   = (uint64_t)viewport->limit - base + 1;
+    const uint64_t size   = viewport->limit - base + 1;
     const bool enabled    = viewport->cr[1] & DESIGNWARE_PCIE_ATU_ENABLE;
 
     MemoryRegion *current, *other;
@@ -351,6 +351,14 @@ static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
     case DESIGNWARE_PCIE_ATU_UPPER_BASE:
         viewport->base &= 0x00000000FFFFFFFFULL;
         viewport->base |= (uint64_t)val << 32;
+        /* The documentatoin states that the value of this register
+         * "Forms bits [63:32] of the start (and end) address
+         * of the address region to be translated.
+         * Note that from version 406A there is a sperate
+         * register fot the upper end address
+         */
+        viewport->limit &= 0x00000000FFFFFFFFULL;
+        viewport->limit |= (uint64_t)val << 32;
         break;
 
     case DESIGNWARE_PCIE_ATU_LOWER_TARGET:
@@ -364,7 +372,8 @@ static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
         break;
 
     case DESIGNWARE_PCIE_ATU_LIMIT:
-        viewport->limit = val;
+        viewport->limit &= 0xFFFFFFFF00000000ULL;
+        viewport->limit |= val;
         break;
 
     case DESIGNWARE_PCIE_ATU_CR1:
@@ -429,7 +438,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp)
         viewport->inbound = true;
         viewport->base    = 0x0000000000000000ULL;
         viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;
         viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
 
         source      = &host->pci.address_space_root;
@@ -453,7 +462,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp)
         viewport->inbound = false;
         viewport->base    = 0x0000000000000000ULL;
         viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;
         viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
 
         destination = &host->pci.memory;
@@ -560,7 +569,7 @@ static const VMStateDescription vmstate_designware_pcie_viewport = {
     .fields = (const VMStateField[]) {
         VMSTATE_UINT64(base, DesignwarePCIEViewport),
         VMSTATE_UINT64(target, DesignwarePCIEViewport),
-        VMSTATE_UINT32(limit, DesignwarePCIEViewport),
+        VMSTATE_UINT64(limit, DesignwarePCIEViewport),
         VMSTATE_UINT32_ARRAY(cr, DesignwarePCIEViewport, 2),
         VMSTATE_END_OF_LIST()
     }
include/hw/pci-host/designware.h +1 -1
index 908f3d946b..51052683b7 100644
@@ -41,7 +41,7 @@ typedef struct DesignwarePCIEViewport {
 
     uint64_t base;
     uint64_t target;
-    uint32_t limit;
+    uint64_t limit;
     uint32_t cr[2];
 
     bool inbound;
Signature · 1 line
2.25.1
#1 lore raw

Hi Shlomo,

Quoted 52 lines · Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

Hanlde wrap around when calculating the viewport size
caused by the fact that perior to version 460A the limit variable was 32bit quantity and not 64 bits quantity.
In the i.MX 6Dual/6Quad Applications Processor Reference Manual document on which the original code was based upon in the description of the iATU Region Upper Base Address Register it is written:
Forms bits [63:32] of the start (and end) address of the address region to be translated.
That is in this register is the upper of both base and the limit. In the current implementation this value was ignored for the limit which caused a wrap around of the size calculaiton.
Using the documnet example:
Base HI: 0x80000000 Base LO: 0xd0000000 Limit LO: 0xd000ffff
The correct size is 0x80000000d000ffff - 0x80000000d0000000 + 1 =
0x010000
The wrong result is 0xd000ffff - 0x80000000d0000000 + 1 = 0x8000000000010000

----

Changes since v2:
* Don't try to fix the calculation.
* Change the limit variable from 32bit to 64 bit
* Set the limit bits [63:32] same as the base according to the specification on which the original code was base upon.

Changes since v1:
* Seperate subject and description

---
  hw/pci-host/designware.c         | 19 ++++++++++++++-----
  include/hw/pci-host/designware.h |  2 +-
  2 files changed, 15 insertions(+), 6 deletions(-)
hw/pci-host/designware.c +1 -1
index dd9e389c07..43cba9432f 100644
@@ -269,7 +269,7 @@ static void designware_pcie_update_viewport(DesignwarePCIERoot *root,
  {
      const uint64_t target = viewport->target;
      const uint64_t base   = viewport->base;
-    const uint64_t size   = (uint64_t)viewport->limit - base + 1;
+    const uint64_t size   = viewport->limit - base + 1;
      const bool enabled    = viewport->cr[1] & DESIGNWARE_PCIE_ATU_ENABLE;
 
      MemoryRegion *current, *other;
@@ -351,6 +351,14 @@ static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
      case DESIGNWARE_PCIE_ATU_UPPER_BASE:

hw/pci-host/designware.c static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address, · line 361

Quoted 10 lines
hw/pci-host/designware.c +8 -0
          viewport->base &= 0x00000000FFFFFFFFULL;
          viewport->base |= (uint64_t)val << 32;
+        /* The documentatoin states that the value of this register
+         * "Forms bits [63:32] of the start (and end) address
+         * of the address region to be translated.
+         * Note that from version 406A there is a sperate
+         * register fot the upper end address
+         */
+        viewport->limit &= 0x00000000FFFFFFFFULL;
+        viewport->limit |= (uint64_t)val << 32;

This code is easier to review using:

          viewport->limit = deposit64(viewport->limit, 32, 32, val);

hw/pci-host/designware.c static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address, · line 376

Quoted 10 lines
hw/pci-host/designware.c +2 -1
          break;
 
      case DESIGNWARE_PCIE_ATU_LOWER_TARGET:
@@ -364,7 +372,8 @@ static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
          break;
 
      case DESIGNWARE_PCIE_ATU_LIMIT:
-        viewport->limit = val;
+        viewport->limit &= 0xFFFFFFFF00000000ULL;
+        viewport->limit |= val;

Here:

          viewport->limit = deposit64(viewport->limit, 0, 32, val);

hw/pci-host/designware.c static void designware_pcie_root_realize(PCIDevice *dev, Error **errp) · line 441

Quoted 9 lines
hw/pci-host/designware.c +1 -1
          break;
 
      case DESIGNWARE_PCIE_ATU_CR1:
@@ -429,7 +438,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp)
          viewport->inbound = true;
          viewport->base    = 0x0000000000000000ULL;
          viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Previous code is easier to review IMHO.

hw/pci-host/designware.c static void designware_pcie_root_realize(PCIDevice *dev, Error **errp) · line 465

Quoted 9 lines
hw/pci-host/designware.c +1 -1
          viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
 
          source      = &host->pci.address_space_root;
@@ -453,7 +462,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp)
          viewport->inbound = false;
          viewport->base    = 0x0000000000000000ULL;
          viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Ditto.

hw/pci-host/designware.c static const VMStateDescription vmstate_designware_pcie_viewport = { · line 572

Quoted 9 lines
hw/pci-host/designware.c +1 -1
          viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
 
          destination = &host->pci.memory;
@@ -560,7 +569,7 @@ static const VMStateDescription vmstate_designware_pcie_viewport = {
      .fields = (const VMStateField[]) {
          VMSTATE_UINT64(base, DesignwarePCIEViewport),
          VMSTATE_UINT64(target, DesignwarePCIEViewport),
-        VMSTATE_UINT32(limit, DesignwarePCIEViewport),
+        VMSTATE_UINT64(limit, DesignwarePCIEViewport),

Unfortunately this breaks the migration stream. I'm not sure what is the best way to deal with it (Cc'ing migration maintainers).

hw/pci-host/designware.c static const VMStateDescription vmstate_designware_pcie_viewport = { · line 575

Quoted 3 lines
hw/pci-host/designware.c +0 -0
          VMSTATE_UINT32_ARRAY(cr, DesignwarePCIEViewport, 2),
          VMSTATE_END_OF_LIST()
      }

Regards,

Phil.

#2 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé

hw/pci-host/designware.c static const VMStateDescription vmstate_designware_pcie_viewport = { · line 572

Quoted 6 lines
hw/pci-host/designware.c +1 -1
@@ -560,7 +569,7 @@ static const VMStateDescription vmstate_designware_pcie_viewport = {
      .fields = (const VMStateField[]) {
          VMSTATE_UINT64(base, DesignwarePCIEViewport),
          VMSTATE_UINT64(target, DesignwarePCIEViewport),
-        VMSTATE_UINT32(limit, DesignwarePCIEViewport),
+        VMSTATE_UINT64(limit, DesignwarePCIEViewport),

Unfortunately this breaks the migration stream. I'm not sure what is the best way to deal with it (Cc'ing migration maintainers).

My understanding is that we can have at least two ways to do this, one relying on machine type properties, the other one can be VMSD versioning. Frankly I don't have a solid mind either on which is the best approach.

I never had a talk with either Juan / Dave before on this, but my understanding is that VMSD versioning is just less-flexible, because it doesn't support backward migrations (only forward). While machine-type property based solution can support both (forward + backward).

I decided to draft a doc update for this, to put my thoughts here:

https://lore.kernel.org/qemu-devel/[email protected]

It can be seen as a reference, or review comments also welcomed.

This device seems to be only supported by CONFIG_FSL_IMX7. Maybe vmsd versioning would be good enough here, then? If so, instead of VMSTATE_UINT64(), we may want a new VMSTATE_UINT32_V() with a boosted version.

Thanks,

Signature · 1 line
Peter Xu
#3 lore raw
In reply to Philippe Mathieu-Daudé

Please see inline

Quoted 69 lines · Philippe Mathieu-Daudé

Hi Shlomo,

Quoted 55 lines · Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

     Hanlde wrap around when calculating the viewport size
     caused by the fact that perior to version 460A the limit variable was 32bit quantity and not 64 bits quantity.
     In the i.MX 6Dual/6Quad Applications Processor Reference Manual document on which the original code was based upon in the description of the iATU Region Upper Base Address Register it is written:
     Forms bits [63:32] of the start (and end) address of the address region to be translated.
     That is in this register is the upper of both base and the limit. In the current implementation this value was ignored for the limit which caused a wrap around of the size calculaiton.
     Using the documnet example:
     Base HI: 0x80000000 Base LO: 0xd0000000 Limit LO: 0xd000ffff
     The correct size is 0x80000000d000ffff - 0x80000000d0000000 + 1 = 0x010000
     The wrong result is 0xd000ffff - 0x80000000d0000000 + 1 = 0x8000000000010000

     ----

     Changes since v2:
      * Don't try to fix the calculation.
      * Change the limit variable from 32bit to 64 bit
      * Set the limit bits [63:32] same as the base according to the specification on which the original code was base upon.

     Changes since v1:
      * Seperate subject and description
---
  hw/pci-host/designware.c         | 19 ++++++++++++++----- include/hw/pci-host/designware.h |  2 +-

  2 files changed, 15 insertions(+), 6 deletions(-)
hw/pci-host/designware.c +1 -1
index dd9e389c07..43cba9432f 100644
@@ -269,7 +269,7 @@ static void 
 designware_pcie_update_viewport(DesignwarePCIERoot *root,
   {
       const uint64_t target = viewport->target;
       const uint64_t base   = viewport->base;
-    const uint64_t size   = (uint64_t)viewport->limit - base + 1;
+    const uint64_t size   = viewport->limit - base + 1;
       const bool enabled    = viewport->cr[1] & DESIGNWARE_PCIE_ATU_ENABLE;
         MemoryRegion *current, *other;
@@ -351,6 +351,14 @@ static void 
 designware_pcie_root_config_write(PCIDevice *d, uint32_t address,

      case DESIGNWARE_PCIE_ATU_UPPER_BASE:
          viewport->base &= 0x00000000FFFFFFFFULL; viewport->base |= (uint64_t)val << 32;

Quoted 8 lines

+        /* The documentatoin states that the value of this register +         * "Forms bits [63:32] of the start (and end) address +         * of the address region to be translated.
+         * Note that from version 406A there is a sperate +         * register fot the upper end address
+         */
+        viewport->limit &= 0x00000000FFFFFFFFULL;
+        viewport->limit |= (uint64_t)val << 32;

This code is easier to review using:

Quoted 2 lines

          viewport->limit = deposit64(viewport->limit, 32, 32, val);

It will be strange to have
viewport->base &= 0x00000000FFFFFFFFULL; viewport->base |= (uint64_t)val << 32;
and then
viewport->limit = deposit64(viewport->limit, 32, 32, val); I think that the code for base and limit should be the same. And I don't think the original for base should be change to viewport->base = deposit64(viewport->base, 32, 32, val); SO everything will look the same.

Quoted 12 lines
Quoted 9 lines

          break;
        case DESIGNWARE_PCIE_ATU_LOWER_TARGET:

hw/pci-host/designware.c +0 -0
@@ -364,7 +372,8 @@ static void 
 designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
           break;

        case DESIGNWARE_PCIE_ATU_LIMIT:
-        viewport->limit = val;
+        viewport->limit &= 0xFFFFFFFF00000000ULL; +        viewport->limit |= val;

Here:

Quoted 1 line

          viewport->limit = deposit64(viewport->limit, 0, 32, val);

My opinion is that the code should be identical to case DESIGNWARE_PCIE_ATU_LOWER_BASE:
        viewport->base &= 0xFFFFFFFF00000000ULL; viewport->base |= val;
        break;
I don't think it is good to mix two styles.

Quoted 11 lines

hw/pci-host/designware.c static void · line 442

Quoted 9 lines

          break;
        case DESIGNWARE_PCIE_ATU_CR1:

hw/pci-host/designware.c +1 -1
@@ -429,7 +438,7 @@ static void 
 designware_pcie_root_realize(PCIDevice *dev, Error **errp)
           viewport->inbound = true;
           viewport->base    = 0x0000000000000000ULL;
           viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Previous code is easier to review IMHO.

Just want to make it clear that this is 64 bit values and that the upper value is the same as the base's upper value, and according to spec.

Quoted 11 lines

hw/pci-host/designware.c static void · line 466

Quoted 9 lines
hw/pci-host/designware.c +1 -1
           viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
             source      = &host->pci.address_space_root;
@@ -453,7 +462,7 @@ static void 
 designware_pcie_root_realize(PCIDevice *dev, Error **errp)
           viewport->inbound = false;
           viewport->base    = 0x0000000000000000ULL;
           viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Ditto.

Ditto.

Quoted 14 lines

hw/pci-host/designware.c static const VMStateDescription · line 573

Quoted 10 lines
hw/pci-host/designware.c +1 -1
           viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
             destination = &host->pci.memory;
@@ -560,7 +569,7 @@ static const VMStateDescription 
 vmstate_designware_pcie_viewport = {
       .fields = (const VMStateField[]) {
           VMSTATE_UINT64(base, DesignwarePCIEViewport),
           VMSTATE_UINT64(target, DesignwarePCIEViewport),
-        VMSTATE_UINT32(limit, DesignwarePCIEViewport),
+        VMSTATE_UINT64(limit, DesignwarePCIEViewport),

Unfortunately this breaks the migration stream. I'm not sure what is the best way to deal with it (Cc'ing migration maintainers).

My bad forgot to update version_id and minimum_version_id. I'll consult Peter Xu's document.

Quoted 7 lines
Quoted 4 lines
hw/pci-host/designware.c +0 -0
           VMSTATE_UINT32_ARRAY(cr, DesignwarePCIEViewport, 2),
           VMSTATE_END_OF_LIST()

      }

Regards,

Phil.

------------------------------------------------------------------------
*From:* Philippe Mathieu-Daudé [mailto:[email protected]]
*Sent:* Monday, January 22, 2024, 1:17 AM
*To:* Shlomo Pongratz; [email protected]
*Cc:* [email protected], [email protected], [email protected], Peter Xu; Fabiano Rosas *Subject:* [PATCH v3] Handle wrap around in limit calculation

Quoted 129 lines

Hi Shlomo,

Quoted 55 lines · Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

     Hanlde wrap around when calculating the viewport size
     caused by the fact that perior to version 460A the limit variable was 32bit quantity and not 64 bits quantity.
     In the i.MX 6Dual/6Quad Applications Processor Reference Manual document on which the original code was based upon in the description of the iATU Region Upper Base Address Register it is written:
     Forms bits [63:32] of the start (and end) address of the address region to be translated.
     That is in this register is the upper of both base and the limit. In the current implementation this value was ignored for the limit which caused a wrap around of the size calculaiton.
     Using the documnet example:
     Base HI: 0x80000000 Base LO: 0xd0000000 Limit LO: 0xd000ffff
     The correct size is 0x80000000d000ffff - 0x80000000d0000000 + 1 = 0x010000
     The wrong result is 0xd000ffff - 0x80000000d0000000 + 1 = 0x8000000000010000

     ----

     Changes since v2:
      * Don't try to fix the calculation.
      * Change the limit variable from 32bit to 64 bit
      * Set the limit bits [63:32] same as the base according to the specification on which the original code was base upon.

     Changes since v1:
      * Seperate subject and description
---
  hw/pci-host/designware.c         | 19 ++++++++++++++----- include/hw/pci-host/designware.h |  2 +-

  2 files changed, 15 insertions(+), 6 deletions(-)
hw/pci-host/designware.c +1 -1
index dd9e389c07..43cba9432f 100644
@@ -269,7 +269,7 @@ static void 
 designware_pcie_update_viewport(DesignwarePCIERoot *root,
   {
       const uint64_t target = viewport->target;
       const uint64_t base   = viewport->base;
-    const uint64_t size   = (uint64_t)viewport->limit - base + 1;
+    const uint64_t size   = viewport->limit - base + 1;
       const bool enabled    = viewport->cr[1] & DESIGNWARE_PCIE_ATU_ENABLE;
         MemoryRegion *current, *other;
@@ -351,6 +351,14 @@ static void 
 designware_pcie_root_config_write(PCIDevice *d, uint32_t address,

      case DESIGNWARE_PCIE_ATU_UPPER_BASE:
          viewport->base &= 0x00000000FFFFFFFFULL; viewport->base |= (uint64_t)val << 32;

Quoted 8 lines

+        /* The documentatoin states that the value of this register +         * "Forms bits [63:32] of the start (and end) address +         * of the address region to be translated.
+         * Note that from version 406A there is a sperate +         * register fot the upper end address
+         */
+        viewport->limit &= 0x00000000FFFFFFFFULL;
+        viewport->limit |= (uint64_t)val << 32;

This code is easier to review using:

          viewport->limit = deposit64(viewport->limit, 32, 32, val);

Quoted 9 lines

          break;
        case DESIGNWARE_PCIE_ATU_LOWER_TARGET:

hw/pci-host/designware.c +0 -0
@@ -364,7 +372,8 @@ static void 
 designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
           break;

        case DESIGNWARE_PCIE_ATU_LIMIT:
-        viewport->limit = val;
+        viewport->limit &= 0xFFFFFFFF00000000ULL; +        viewport->limit |= val;

Here:

          viewport->limit = deposit64(viewport->limit, 0, 32, val);

hw/pci-host/designware.c static void · line 442

Quoted 9 lines

          break;
        case DESIGNWARE_PCIE_ATU_CR1:

hw/pci-host/designware.c +1 -1
@@ -429,7 +438,7 @@ static void 
 designware_pcie_root_realize(PCIDevice *dev, Error **errp)
           viewport->inbound = true;
           viewport->base    = 0x0000000000000000ULL;
           viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Previous code is easier to review IMHO.

hw/pci-host/designware.c static void · line 466

Quoted 9 lines
hw/pci-host/designware.c +1 -1
           viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
             source      = &host->pci.address_space_root;
@@ -453,7 +462,7 @@ static void 
 designware_pcie_root_realize(PCIDevice *dev, Error **errp)
           viewport->inbound = false;
           viewport->base    = 0x0000000000000000ULL;
           viewport->target  = 0x0000000000000000ULL;
-        viewport->limit   = UINT32_MAX;
+        viewport->limit   = 0x00000000FFFFFFFFULL;

Ditto.

hw/pci-host/designware.c static const VMStateDescription · line 573

Quoted 9 lines
hw/pci-host/designware.c +1 -1
           viewport->cr[0]   = DESIGNWARE_PCIE_ATU_TYPE_MEM;
             destination = &host->pci.memory;
@@ -560,7 +569,7 @@ static const VMStateDescription 
 vmstate_designware_pcie_viewport = {
       .fields = (const VMStateField[]) {
           VMSTATE_UINT64(base, DesignwarePCIEViewport),
           VMSTATE_UINT64(target, DesignwarePCIEViewport),
-        VMSTATE_UINT32(limit, DesignwarePCIEViewport),
+        VMSTATE_UINT64(limit, DesignwarePCIEViewport),

Unfortunately this breaks the migration stream. I'm not sure what is the best way to deal with it (Cc'ing migration maintainers).

Quoted 3 lines
hw/pci-host/designware.c +0 -0
           VMSTATE_UINT32_ARRAY(cr, DesignwarePCIEViewport, 2),
           VMSTATE_END_OF_LIST()

      }

Regards,

Phil.

#4 lore raw
In reply to Shlomo Pongratz
Quoted 52 lines · Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

Hanlde wrap around when calculating the viewport size
caused by the fact that perior to version 460A the limit variable was 32bit quantity and not 64 bits quantity.
In the i.MX 6Dual/6Quad Applications Processor Reference Manual document on which the original code was based upon in the description of the iATU Region Upper Base Address Register it is written:
Forms bits [63:32] of the start (and end) address of the address region to be translated.
That is in this register is the upper of both base and the limit. In the current implementation this value was ignored for the limit which caused a wrap around of the size calculaiton.
Using the documnet example:
Base HI: 0x80000000 Base LO: 0xd0000000 Limit LO: 0xd000ffff
The correct size is 0x80000000d000ffff - 0x80000000d0000000 + 1 =
0x010000
The wrong result is 0xd000ffff - 0x80000000d0000000 + 1 = 0x8000000000010000

----

Changes since v2:
* Don't try to fix the calculation.
* Change the limit variable from 32bit to 64 bit
* Set the limit bits [63:32] same as the base according to the specification on which the original code was base upon.

Changes since v1:
* Seperate subject and description

---
 hw/pci-host/designware.c         | 19 ++++++++++++++-----
 include/hw/pci-host/designware.h |  2 +-
 2 files changed, 15 insertions(+), 6 deletions(-)
hw/pci-host/designware.c +1 -1
index dd9e389c07..43cba9432f 100644
@@ -269,7 +269,7 @@ static void designware_pcie_update_viewport(DesignwarePCIERoot *root,
 {
     const uint64_t target = viewport->target;
     const uint64_t base   = viewport->base;
-    const uint64_t size   = (uint64_t)viewport->limit - base + 1;
+    const uint64_t size   = viewport->limit - base + 1;
     const bool enabled    = viewport->cr[1] & DESIGNWARE_PCIE_ATU_ENABLE;
 
     MemoryRegion *current, *other;

hw/pci-host/designware.c static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address, · line 355

Quoted 5 lines
hw/pci-host/designware.c +1 -0
@@ -351,6 +351,14 @@ static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address,
     case DESIGNWARE_PCIE_ATU_UPPER_BASE:
         viewport->base &= 0x00000000FFFFFFFFULL;
         viewport->base |= (uint64_t)val << 32;
 
+        /* The documentatoin states that the value of this register

"documentation".

Multiline comments should have the /* on a line of its own.

hw/pci-host/designware.c static void designware_pcie_root_config_write(PCIDevice *d, uint32_t address, · line 362

Quoted 8 lines
hw/pci-host/designware.c +7 -0
+         * "Forms bits [63:32] of the start (and end) address
+         * of the address region to be translated.
+         * Note that from version 406A there is a sperate
+         * register fot the upper end address
+         */
+        viewport->limit &= 0x00000000FFFFFFFFULL;
+        viewport->limit |= (uint64_t)val << 32;
         break;

Better to calculate the effective limit address when we need it, rather than when the register is written. It's not a very expensive calculation.

include/hw/pci-host/designware.h typedef struct DesignwarePCIEViewport { · line 45

Quoted 11 lines
include/hw/pci-host/designware.h +1 -1
index 908f3d946b..51052683b7 100644
@@ -41,7 +41,7 @@ typedef struct DesignwarePCIEViewport {
 
     uint64_t base;
     uint64_t target;
-    uint32_t limit;
+    uint64_t limit;
     uint32_t cr[2];

Making this field 64 bits makes the code to read and write the register more complicated, and introduces a migration compat break that we need to deal with. Why bother? You can fix the problem that you're trying to solve in the way that I suggested to you without introducing this extra complication to this patch.

thanks
-- PMM