Skip to contentSkip to conversation

hw/arm/virt: Improve address assignment for high memory regions

PATCH Superseded Gavin Shan 4 participants 17 messages Oct 4, 2022 – Oct 11, 2022

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

v4 cover hw/arm/virt: Improve address assignment for high memory regions

There are three high memory regions, which are VIRT_HIGH_REDIST2, VIRT_HIGH_PCIE_ECAM and VIRT_HIGH_PCIE_MMIO. Their base addresses are floating on highest RAM address. However, they can be disabled in several cases.

(1) One specific high memory region is disabled by developer by

    toggling vms->highmem_{redists, ecam, mmio}.

(2) VIRT_HIGH_PCIE_ECAM region is disabled on machine, which is

    'virt-2.12' or ealier than it.

(3) VIRT_HIGH_PCIE_ECAM region is disabled when firmware is loaded on 32-bits system.

(4) One specific high memory region is disabled when it breaks the PA space limit.

The current implementation of virt_set_memmap() isn't comprehensive because the space for one specific high memory region is always reserved from the PA space for case (1), (2) and (3). In the code, 'base' and 'vms->highest_gpa' are always increased for those three cases. It's unnecessary since the assigned space of the disabled high memory region won't be used afterwards.

The series intends to improve the address assignment for these high memory regions.

PATCH[1-4] preparatory work for the improvment
PATCH[5] improve high memory region address assignment
PATCH[6] adds 'compact-highmem' to enable or disable the optimization

History
=======
v3: https://lists.nongnu.org/archive/html/qemu-arm/2022-09/msg00258.html
v2: https://lore.kernel.org/all/[email protected]/T/
v1: https://lists.nongnu.org/archive/html/qemu-arm/2022-08/msg00013.html

Changelog
==========
v4:
* Add virt_get_high_memmap_enabled() helper (Eric)
* Move 'vms->highmem_compact' and related logic from

    PATCH[v4 6/6] to PATCH[v4 5/6] to avoid git-bisect
    breakage                                                   (Eric)

* Document the legacy and optimized high memory region
layout in commit log and source code (Eric)
v3:
* Reorder the patches (Gavin)
* Add 'highmem-compact' property for backwards compatibility (Eric)
v2:

  * Split the patches for easier review                        (Gavin)
  * Improved changelog                                         (Marc)
  * Use 'bool fits' in virt_set_high_memmap()                  (Eric)

Gavin Shan (6):
hw/arm/virt: Introduce virt_set_high_memmap() helper hw/arm/virt: Rename variable size to region_size in
virt_set_high_memmap()
hw/arm/virt: Introduce variable region_base in virt_set_high_memmap() hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper hw/arm/virt: Improve high memory region address hw/arm/virt: Add 'compact-highmem' property

 docs/system/arm/virt.rst |   4 ++
 hw/arm/virt.c            | 131 +++++++++++++++++++++++++++++----------
 include/hw/arm/virt.h    |   2 +
 3 files changed, 104 insertions(+), 33 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH v4 1/6 hw/arm/virt: Introduce virt_set_high_memmap() helper

This introduces virt_set_high_memmap() helper. The logic of high memory region address assignment is moved to the helper. The intention is to make the subsequent optimization for high memory region address assignment easier.

No functional change intended.

---
 hw/arm/virt.c | 74 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 33 deletions(-)
hw/arm/virt.c +41 -33
index 0961e053e5..4dab528b82 100644
@@ -1689,6 +1689,46 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
     return arm_cpu_mp_affinity(idx, clustersz);
 }
 
+static void virt_set_high_memmap(VirtMachineState *vms,
+                                 hwaddr base, int pa_bits)
+{
+    int i;
+
+    for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
+        hwaddr size = extended_memmap[i].size;
+        bool fits;
+
+        base = ROUND_UP(base, size);
+        vms->memmap[i].base = base;
+        vms->memmap[i].size = size;
+
+        /*
+         * Check each device to see if they fit in the PA space,
+         * moving highest_gpa as we go.
+         *
+         * For each device that doesn't fit, disable it.
+         */
+        fits = (base + size) <= BIT_ULL(pa_bits);
+        if (fits) {
+            vms->highest_gpa = base + size - 1;
+        }
+
+        switch (i) {
+        case VIRT_HIGH_GIC_REDIST2:
+            vms->highmem_redists &= fits;
+            break;
+        case VIRT_HIGH_PCIE_ECAM:
+            vms->highmem_ecam &= fits;
+            break;
+        case VIRT_HIGH_PCIE_MMIO:
+            vms->highmem_mmio &= fits;
+            break;
+        }
+
+        base += size;
+    }
+}
+
 static void virt_set_memmap(VirtMachineState *vms, int pa_bits)
 {
     MachineState *ms = MACHINE(vms);
@@ -1744,39 +1784,7 @@ static void virt_set_memmap(VirtMachineState *vms, int pa_bits)
     /* We know for sure that at least the memory fits in the PA space */
     vms->highest_gpa = memtop - 1;
 
-    for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
-        hwaddr size = extended_memmap[i].size;
-        bool fits;
-
-        base = ROUND_UP(base, size);
-        vms->memmap[i].base = base;
-        vms->memmap[i].size = size;
-
-        /*
-         * Check each device to see if they fit in the PA space,
-         * moving highest_gpa as we go.
-         *
-         * For each device that doesn't fit, disable it.
-         */
-        fits = (base + size) <= BIT_ULL(pa_bits);
-        if (fits) {
-            vms->highest_gpa = base + size - 1;
-        }
-
-        switch (i) {
-        case VIRT_HIGH_GIC_REDIST2:
-            vms->highmem_redists &= fits;
-            break;
-        case VIRT_HIGH_PCIE_ECAM:
-            vms->highmem_ecam &= fits;
-            break;
-        case VIRT_HIGH_PCIE_MMIO:
-            vms->highmem_mmio &= fits;
-            break;
-        }
-
-        base += size;
-    }
+    virt_set_high_memmap(vms, base, pa_bits);
 
     if (device_memory_size > 0) {
         ms->device_memory = g_malloc0(sizeof(*ms->device_memory));
Signature · 1 line
2.23.0
author
#2 lore raw

PATCH v4 2/6 hw/arm/virt: Rename variable size to region_size in virt_set_high_memmap()

In reply to Gavin Shan

This renames variable 'size' to 'region_size' in virt_set_high_memmap(). Its counterpart ('region_base') will be introduced in next patch.

No functional change intended.

---
 hw/arm/virt.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
hw/arm/virt.c +8 -7
index 4dab528b82..187b3ee0e2 100644
@@ -1692,15 +1692,16 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
 static void virt_set_high_memmap(VirtMachineState *vms,
                                  hwaddr base, int pa_bits)
 {
+    hwaddr region_size;
+    bool fits;
     int i;
 
     for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
-        hwaddr size = extended_memmap[i].size;
-        bool fits;
+        region_size = extended_memmap[i].size;
 
-        base = ROUND_UP(base, size);
+        base = ROUND_UP(base, region_size);
         vms->memmap[i].base = base;
-        vms->memmap[i].size = size;
+        vms->memmap[i].size = region_size;
 
         /*
          * Check each device to see if they fit in the PA space,
@@ -1708,9 +1709,9 @@ static void virt_set_high_memmap(VirtMachineState *vms,
          *
          * For each device that doesn't fit, disable it.
          */
-        fits = (base + size) <= BIT_ULL(pa_bits);
+        fits = (base + region_size) <= BIT_ULL(pa_bits);
         if (fits) {
-            vms->highest_gpa = base + size - 1;
+            vms->highest_gpa = base + region_size - 1;
         }
 
         switch (i) {
@@ -1725,7 +1726,7 @@ static void virt_set_high_memmap(VirtMachineState *vms,
             break;
         }
 
-        base += size;
+        base += region_size;
     }
 }
 
Signature · 1 line
2.23.0
Reviewed-by on v4 2/6
#3 lore raw

Re: [PATCH v4 2/6] hw/arm/virt: Rename variable size to region_size in virt_set_high_memmap()

Quoted 11 lines · Gavin Shan

This renames variable 'size' to 'region_size' in virt_set_high_memmap(). Its counterpart ('region_base') will be introduced in next patch.

No functional change intended.

---
 hw/arm/virt.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
author
#4 lore raw

PATCH v4 3/6 hw/arm/virt: Introduce variable region_base in virt_set_high_memmap()

In reply to Gavin Shan

This introduces variable 'region_base' for the base address of the specific high memory region. It's the preparatory work to optimize high memory region address assignment.

No functional change intended.

---
 hw/arm/virt.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
hw/arm/virt.c +6 -6
index 187b3ee0e2..b0b679d1f4 100644
@@ -1692,15 +1692,15 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
 static void virt_set_high_memmap(VirtMachineState *vms,
                                  hwaddr base, int pa_bits)
 {
-    hwaddr region_size;
+    hwaddr region_base, region_size;
     bool fits;
     int i;
 
     for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
+        region_base = ROUND_UP(base, extended_memmap[i].size);
         region_size = extended_memmap[i].size;
 
-        base = ROUND_UP(base, region_size);
-        vms->memmap[i].base = base;
+        vms->memmap[i].base = region_base;
         vms->memmap[i].size = region_size;
 
         /*
@@ -1709,9 +1709,9 @@ static void virt_set_high_memmap(VirtMachineState *vms,
          *
          * For each device that doesn't fit, disable it.
          */
-        fits = (base + region_size) <= BIT_ULL(pa_bits);
+        fits = (region_base + region_size) <= BIT_ULL(pa_bits);
         if (fits) {
-            vms->highest_gpa = base + region_size - 1;
+            vms->highest_gpa = region_base + region_size - 1;
         }
 
         switch (i) {
@@ -1726,7 +1726,7 @@ static void virt_set_high_memmap(VirtMachineState *vms,
             break;
         }
 
-        base += region_size;
+        base = region_base + region_size;
     }
 }
 
Signature · 1 line
2.23.0
Reviewed-by on v4 3/6
#5 lore raw

Re: [PATCH v4 3/6] hw/arm/virt: Introduce variable region_base in virt_set_high_memmap()

Quoted 11 lines · Gavin Shan

This introduces variable 'region_base' for the base address of the specific high memory region. It's the preparatory work to optimize high memory region address assignment.

No functional change intended.

---
 hw/arm/virt.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
author
#6 lore raw

PATCH v4 4/6 hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper

In reply to Gavin Shan

This introduces virt_get_high_memmap_enabled() helper, which returns the pointer to vms->highmem_{redists, ecam, mmio}. The pointer will be used in the subsequent patches.

No functional change intended.

---
 hw/arm/virt.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
hw/arm/virt.c +17 -13
index b0b679d1f4..59de7b78b5 100644
@@ -1689,14 +1689,29 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
     return arm_cpu_mp_affinity(idx, clustersz);
 }
 
+static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
+                                                 int index)
+{
+    bool *enabled_array[] = {
+        &vms->highmem_redists,
+        &vms->highmem_ecam,
+        &vms->highmem_mmio,
+    };
+
+    assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));
+
+    return enabled_array[index - VIRT_LOWMEMMAP_LAST];
+}
+
 static void virt_set_high_memmap(VirtMachineState *vms,
                                  hwaddr base, int pa_bits)
 {
     hwaddr region_base, region_size;
-    bool fits;
+    bool *region_enabled, fits;
     int i;
 
     for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
+        region_enabled = virt_get_high_memmap_enabled(vms, i);
         region_base = ROUND_UP(base, extended_memmap[i].size);
         region_size = extended_memmap[i].size;
 
@@ -1714,18 +1729,7 @@ static void virt_set_high_memmap(VirtMachineState *vms,
             vms->highest_gpa = region_base + region_size - 1;
         }
 
-        switch (i) {
-        case VIRT_HIGH_GIC_REDIST2:
-            vms->highmem_redists &= fits;
-            break;
-        case VIRT_HIGH_PCIE_ECAM:
-            vms->highmem_ecam &= fits;
-            break;
-        case VIRT_HIGH_PCIE_MMIO:
-            vms->highmem_mmio &= fits;
-            break;
-        }
-
+        *region_enabled &= fits;
         base = region_base + region_size;
     }
 }
Signature · 1 line
2.23.0
#7 lore raw

Re: [PATCH v4 4/6] hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper

Quoted 19 lines · Gavin Shan

This introduces virt_get_high_memmap_enabled() helper, which returns the pointer to vms->highmem_{redists, ecam, mmio}. The pointer will be used in the subsequent patches.

No functional change intended.

---
 hw/arm/virt.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
hw/arm/virt.c +0 -0
index b0b679d1f4..59de7b78b5 100644
@@ -1689,14 +1689,29 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
     return arm_cpu_mp_affinity(idx, clustersz);
 }
 

hw/arm/virt.c static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) · line 1701

Quoted 10 lines
hw/arm/virt.c +10 -0
+static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
+                                                 int index)
+{
+    bool *enabled_array[] = {
+        &vms->highmem_redists,
+        &vms->highmem_ecam,
+        &vms->highmem_mmio,
+    };
+
+    assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));

I wonder whether we want an assert(ARRAY_SIZE(extended_memmap) == ARRAY_SIZE(enabled_array))? IIUC, we never want those two to get out of sync?

Quoted 4 lines
hw/arm/virt.c +4 -0
+
+    return enabled_array[index - VIRT_LOWMEMMAP_LAST];
+}
+
author
#8 lore raw

Re: [PATCH v4 4/6] hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper

Hi Connie,

Quoted 21 lines · Cornelia Huck
Quoted 19 lines · Gavin Shan

This introduces virt_get_high_memmap_enabled() helper, which returns the pointer to vms->highmem_{redists, ecam, mmio}. The pointer will be used in the subsequent patches.

No functional change intended.

---
  hw/arm/virt.c | 30 +++++++++++++++++-------------
  1 file changed, 17 insertions(+), 13 deletions(-)
hw/arm/virt.c +0 -0
index b0b679d1f4..59de7b78b5 100644
@@ -1689,14 +1689,29 @@ static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
      return arm_cpu_mp_affinity(idx, clustersz);
  }
 
Quoted 15 lines

hw/arm/virt.c static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) · line 1701

Quoted 10 lines
hw/arm/virt.c +10 -0
+static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
+                                                 int index)
+{
+    bool *enabled_array[] = {
+        &vms->highmem_redists,
+        &vms->highmem_ecam,
+        &vms->highmem_mmio,
+    };
+
+    assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));

I wonder whether we want an assert(ARRAY_SIZE(extended_memmap) == ARRAY_SIZE(enabled_array))? IIUC, we never want those two to get out of sync?

Yeah, It makes sense to ensure both arrays synchronized. I will add the extra check in next respin.

Quoted 4 lines

hw/arm/virt.c static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) · line 1705

Quoted 4 lines
hw/arm/virt.c +4 -0
+
+    return enabled_array[index - VIRT_LOWMEMMAP_LAST];
+}
+

Thanks,
Gavin

#9 lore raw

Re: [PATCH v4 4/6] hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper

Quoted 41 lines · Gavin Shan

Hi Connie,

Quoted 28 lines · Cornelia Huck
Quoted 26 lines · Gavin Shan

This introduces virt_get_high_memmap_enabled() helper, which returns the pointer to vms->highmem_{redists, ecam, mmio}. The pointer will be used in the subsequent patches.

No functional change intended.

---
  hw/arm/virt.c | 30 +++++++++++++++++-------------

  1 file changed, 17 insertions(+), 13 deletions(-)
hw/arm/virt.c +0 -0
index b0b679d1f4..59de7b78b5 100644
@@ -1689,14 +1689,29 @@ static uint64_t
 virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
       return arm_cpu_mp_affinity(idx, clustersz);
   }

  +static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
+                                                 int index) +{
+    bool *enabled_array[] = {
+        &vms->highmem_redists,
+        &vms->highmem_ecam,

Quoted 9 lines
Quoted 4 lines

+        &vms->highmem_mmio,
+    };
+
+    assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));

I wonder whether we want an assert(ARRAY_SIZE(extended_memmap) == ARRAY_SIZE(enabled_array))? IIUC, we never want those two to get out of sync?

Quoted 2 lines

Yeah, It makes sense to ensure both arrays synchronized. I will add the extra check in next respin.

With Connie's suggestion this looks good to me.

Thanks

Eric

Quoted 9 lines
Quoted 5 lines

hw/arm/virt.c static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) · line 1705

Quoted 5 lines
hw/arm/virt.c +4 -0
+
+    return enabled_array[index - VIRT_LOWMEMMAP_LAST];
+}
+

Thanks,
Gavin

author
#10 lore raw

Re: [PATCH v4 4/6] hw/arm/virt: Introduce virt_get_high_memmap_enabled() helper

Quoted 40 lines · Eric Auger
Quoted 39 lines · Gavin Shan
Quoted 37 lines · Cornelia Huck
Quoted 20 lines · Gavin Shan

This introduces virt_get_high_memmap_enabled() helper, which returns the pointer to vms->highmem_{redists, ecam, mmio}. The pointer will be used in the subsequent patches.

No functional change intended.

---
   hw/arm/virt.c | 30 +++++++++++++++++-------------
   1 file changed, 17 insertions(+), 13 deletions(-)
hw/arm/virt.c +0 -0
index b0b679d1f4..59de7b78b5 100644
@@ -1689,14 +1689,29 @@ static uint64_t
 virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
       return arm_cpu_mp_affinity(idx, clustersz);
   }
   +static inline bool *virt_get_high_memmap_enabled(VirtMachineState

hw/arm/virt.c static uint64_t · line 1702

Quoted 10 lines
hw/arm/virt.c +9 -0
 *vms,
+                                                 int index)
+{
+    bool *enabled_array[] = {
+        &vms->highmem_redists,
+        &vms->highmem_ecam,
+        &vms->highmem_mmio,
+    };
+
+    assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));

I wonder whether we want an assert(ARRAY_SIZE(extended_memmap) == ARRAY_SIZE(enabled_array))? IIUC, we never want those two to get out of sync?

Quoted 5 lines
Quoted 2 lines

Yeah, It makes sense to ensure both arrays synchronized. I will add the extra check in next respin.

With Connie's suggestion this looks good to me.

What we need is actually like below because the array (extended_memmap) starts from VIRT_LOWMEMMAP_LAST instead of zero. I'm adding the extra check into v5, which will be posted shortly.

   assert(ARRAY_SIZE(extended_memmap) - VIRT_LOWMEMMAP_LAST ==
          ARRAY_SIZE(enabled_array));
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

hw/arm/virt.c static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) · line 1705

Quoted 5 lines
hw/arm/virt.c +4 -0
+
+    return enabled_array[index - VIRT_LOWMEMMAP_LAST];
+}
+

Thanks,
Gavin

author
#11 lore raw

PATCH v4 5/6 hw/arm/virt: Improve high memory region address

In reply to Gavin Shan

There are three high memory regions, which are VIRT_HIGH_REDIST2, VIRT_HIGH_PCIE_ECAM and VIRT_HIGH_PCIE_MMIO. Their base addresses are floating on highest RAM address. However, they can be disabled in several cases.

(1) One specific high memory region is disabled by developer by

    toggling vms->highmem_{redists, ecam, mmio}.

(2) VIRT_HIGH_PCIE_ECAM region is disabled on machine, which is

    'virt-2.12' or ealier than it.

(3) VIRT_HIGH_PCIE_ECAM region is disabled when firmware is loaded on 32-bits system.

(4) One specific high memory region is disabled when it breaks the PA space limit.

The current implementation of virt_set_memmap() isn't comprehensive because the space for one specific high memory region is always reserved from the PA space for case (1), (2) and (3). In the code, 'base' and 'vms->highest_gpa' are always increased for those three cases. It's unnecessary since the assigned space of the disabled high memory region won't be used afterwards.

This improves the address assignment for those three high memory region by skipping the address assignment for one specific high memory region if it has been disabled in case (1), (2) and (3). 'vms->high_compact' is false for now, meaning that we don't have any behavior changes until it becomes configurable through property 'compact-highmem' in next patch.

---
 hw/arm/virt.c         | 19 ++++++++++++-------
 include/hw/arm/virt.h |  1 +
 2 files changed, 13 insertions(+), 7 deletions(-)
hw/arm/virt.c +12 -7
index 59de7b78b5..4164da49e9 100644
@@ -1715,9 +1715,6 @@ static void virt_set_high_memmap(VirtMachineState *vms,
         region_base = ROUND_UP(base, extended_memmap[i].size);
         region_size = extended_memmap[i].size;
 
-        vms->memmap[i].base = region_base;
-        vms->memmap[i].size = region_size;
-
         /*
          * Check each device to see if they fit in the PA space,
          * moving highest_gpa as we go.
@@ -1725,12 +1722,20 @@ static void virt_set_high_memmap(VirtMachineState *vms,
          * For each device that doesn't fit, disable it.
          */
         fits = (region_base + region_size) <= BIT_ULL(pa_bits);
-        if (fits) {
+        if (*region_enabled && fits) {
+            vms->memmap[i].base = region_base;
+            vms->memmap[i].size = region_size;
             vms->highest_gpa = region_base + region_size - 1;
+            base = region_base + region_size;
+        } else {
+            *region_enabled = false;
+            if (!vms->highmem_compact) {
+                base = region_base + region_size;
+                if (fits) {
+                    vms->highest_gpa = region_base + region_size - 1;
+                }
+            }
         }
-
-        *region_enabled &= fits;
-        base = region_base + region_size;
     }
 }
 
include/hw/arm/virt.h +1 -0
index 6ec479ca2b..709f623741 100644
@@ -144,6 +144,7 @@ struct VirtMachineState {
     PFlashCFI01 *flash[2];
     bool secure;
     bool highmem;
+    bool highmem_compact;
     bool highmem_ecam;
     bool highmem_mmio;
     bool highmem_redists;
Signature · 1 line
2.23.0
#12 lore raw

Re: [PATCH v4 5/6] hw/arm/virt: Improve high memory region address

Quoted 42 lines · Gavin Shan

There are three high memory regions, which are VIRT_HIGH_REDIST2, VIRT_HIGH_PCIE_ECAM and VIRT_HIGH_PCIE_MMIO. Their base addresses are floating on highest RAM address. However, they can be disabled in several cases.

(1) One specific high memory region is disabled by developer by

    toggling vms->highmem_{redists, ecam, mmio}.

(2) VIRT_HIGH_PCIE_ECAM region is disabled on machine, which is

    'virt-2.12' or ealier than it.

(3) VIRT_HIGH_PCIE_ECAM region is disabled when firmware is loaded on 32-bits system.

(4) One specific high memory region is disabled when it breaks the PA space limit.

The current implementation of virt_set_memmap() isn't comprehensive because the space for one specific high memory region is always reserved from the PA space for case (1), (2) and (3). In the code, 'base' and 'vms->highest_gpa' are always increased for those three cases. It's unnecessary since the assigned space of the disabled high memory region won't be used afterwards.

This improves the address assignment for those three high memory region by skipping the address assignment for one specific high memory region if it has been disabled in case (1), (2) and (3). 'vms->high_compact' is false for now, meaning that we don't have any behavior changes until it becomes configurable through property 'compact-highmem' in next patch.

---
 hw/arm/virt.c         | 19 ++++++++++++-------
 include/hw/arm/virt.h |  1 +
 2 files changed, 13 insertions(+), 7 deletions(-)
hw/arm/virt.c +0 -0
index 59de7b78b5..4164da49e9 100644

hw/arm/virt.c static void virt_set_high_memmap(VirtMachineState *vms, · line 1720

Quoted 9 lines
hw/arm/virt.c +0 -3
@@ -1715,9 +1715,6 @@ static void virt_set_high_memmap(VirtMachineState *vms,
         region_base = ROUND_UP(base, extended_memmap[i].size);
         region_size = extended_memmap[i].size;
 
-        vms->memmap[i].base = region_base;
-        vms->memmap[i].size = region_size;
-
         /*
          * Check each device to see if they fit in the PA space,
          * moving highest_gpa as we go.

Maybe tweak this comment?

"Check each enabled device to see if they fit in the PA space, moving highest_gpa as we go. For compatibility, move highest_gpa for disabled fitting devices as well, if the compact layout has been disabled."

(Or would that be overkill?)

author
#13 lore raw

Re: [PATCH v4 5/6] hw/arm/virt: Improve high memory region address

Hi Connie,

Quoted 56 lines · Cornelia Huck
Quoted 42 lines · Gavin Shan

There are three high memory regions, which are VIRT_HIGH_REDIST2, VIRT_HIGH_PCIE_ECAM and VIRT_HIGH_PCIE_MMIO. Their base addresses are floating on highest RAM address. However, they can be disabled in several cases.

(1) One specific high memory region is disabled by developer by

     toggling vms->highmem_{redists, ecam, mmio}.

(2) VIRT_HIGH_PCIE_ECAM region is disabled on machine, which is

     'virt-2.12' or ealier than it.

(3) VIRT_HIGH_PCIE_ECAM region is disabled when firmware is loaded on 32-bits system.

(4) One specific high memory region is disabled when it breaks the PA space limit.

The current implementation of virt_set_memmap() isn't comprehensive because the space for one specific high memory region is always reserved from the PA space for case (1), (2) and (3). In the code, 'base' and 'vms->highest_gpa' are always increased for those three cases. It's unnecessary since the assigned space of the disabled high memory region won't be used afterwards.

This improves the address assignment for those three high memory region by skipping the address assignment for one specific high memory region if it has been disabled in case (1), (2) and (3). 'vms->high_compact' is false for now, meaning that we don't have any behavior changes until it becomes configurable through property 'compact-highmem' in next patch.

---
  hw/arm/virt.c         | 19 ++++++++++++-------
  include/hw/arm/virt.h |  1 +
  2 files changed, 13 insertions(+), 7 deletions(-)
hw/arm/virt.c +0 -0
index 59de7b78b5..4164da49e9 100644

hw/arm/virt.c static void virt_set_high_memmap(VirtMachineState *vms, · line 1720

Quoted 9 lines
hw/arm/virt.c +0 -3
@@ -1715,9 +1715,6 @@ static void virt_set_high_memmap(VirtMachineState *vms,
          region_base = ROUND_UP(base, extended_memmap[i].size);
          region_size = extended_memmap[i].size;
 
-        vms->memmap[i].base = region_base;
-        vms->memmap[i].size = region_size;
-
          /*
           * Check each device to see if they fit in the PA space,
           * moving highest_gpa as we go.

Maybe tweak this comment?

Quoted 7 lines

"Check each enabled device to see if they fit in the PA space, moving highest_gpa as we go. For compatibility, move highest_gpa for disabled fitting devices as well, if the compact layout has been disabled."

(Or would that be overkill?)

It looks overkill to me since the code is simple and clear. However, comments won't be harmful. I will integrate the proposed comment in next respin.

Thanks,
Gavin

author
#14 lore raw

PATCH v4 6/6 hw/arm/virt: Add 'compact-highmem' property

In reply to Gavin Shan

After the improvement to high memory region address assignment is applied, the memory layout can be changed, introducing possible migration breakage. For example, VIRT_HIGH_PCIE_MMIO memory region is disabled or enabled when the optimization is applied or not, with the following configuration.

  pa_bits              = 40;
  vms->highmem_redists = false;
  vms->highmem_ecam    = false;
  vms->highmem_mmio    = true;

  # qemu-system-aarch64 -accel kvm -cpu host    \
    -machine virt-7.2,compact-highmem={on, off} \
    -m 4G,maxmem=511G -monitor stdio

Region compact-highmem=off compact-highmem=on
----------------------------------------------------------------

  RAM               [1GB         512GB]        [1GB         512GB]
  HIGH_GIC_REDISTS  [512GB       512GB+64MB]   [disabled]
  HIGH_PCIE_ECAM    [512GB+256MB 512GB+512MB]  [disabled]
  HIGH_PCIE_MMIO    [disabled]                 [512GB       1TB]

In order to keep backwords compatibility, we need to disable the optimization on machines, which is virt-7.1 or ealier than it. It means the optimization is enabled by default from virt-7.2. Besides, 'compact-highmem' property is added so that the optimization can be explicitly enabled or disabled on all machine types by users.

---
 docs/system/arm/virt.rst |  4 ++++
 hw/arm/virt.c            | 47 ++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/virt.h    |  1 +
 3 files changed, 52 insertions(+)
docs/system/arm/virt.rst +4 -0
index 20442ea2c1..75bf5a4994 100644
@@ -94,6 +94,10 @@ highmem
   address space above 32 bits. The default is ``on`` for machine types
   later than ``virt-2.12``.
 
+compact-highmem
+  Set ``on``/``off`` to enable/disable compact space for high memory regions.
+  The default is ``on`` for machine types later than ``virt-7.2``
+
 gic-version
   Specify the version of the Generic Interrupt Controller (GIC) to provide.
   Valid values are:
hw/arm/virt.c +47 -0

79 lines not on this page · raw message

include/hw/arm/virt.h +1 -0
index 709f623741..c7dd59d7f1 100644
@@ -125,6 +125,7 @@ struct VirtMachineClass {
     bool no_pmu;
     bool claim_edge_triggered_timers;
     bool smbios_old_sys_ver;
+    bool no_highmem_compact;
     bool no_highmem_ecam;
     bool no_ged;   /* Machines < 4.2 have no support for ACPI GED device */
     bool kvm_no_adjvtime;
Signature · 1 line
2.23.0
#15 lore raw

Re: [PATCH v4 6/6] hw/arm/virt: Add 'compact-highmem' property

Quoted 11 lines · Gavin Shan

After the improvement to high memory region address assignment is applied, the memory layout can be changed, introducing possible migration breakage. For example, VIRT_HIGH_PCIE_MMIO memory region is disabled or enabled when the optimization is applied or not, with the following configuration.

  pa_bits              = 40;
  vms->highmem_redists = false;
  vms->highmem_ecam    = false;
  vms->highmem_mmio    = true;

The question is how are these parameters specified by a user? Short of hacking the code, this isn't really possible.

Quoted 12 lines
  # qemu-system-aarch64 -accel kvm -cpu host    \
    -machine virt-7.2,compact-highmem={on, off} \
    -m 4G,maxmem=511G -monitor stdio

Region compact-highmem=off compact-highmem=on
----------------------------------------------------------------

  RAM               [1GB         512GB]        [1GB         512GB]
  HIGH_GIC_REDISTS  [512GB       512GB+64MB]   [disabled]
  HIGH_PCIE_ECAM    [512GB+256MB 512GB+512MB]  [disabled]
  HIGH_PCIE_MMIO    [disabled]                 [512GB       1TB]
Quoted 5 lines

In order to keep backwords compatibility, we need to disable the optimization on machines, which is virt-7.1 or ealier than it. It means the optimization is enabled by default from virt-7.2. Besides, 'compact-highmem' property is added so that the optimization can be explicitly enabled or disabled on all machine types by users.

Not directly related to this series, but it seems to me that we should be aiming at reproducible results across HW implementations (at least with KVM). Depending on how many PA bits the HW implements, we end-up with a set of devices or another, which is likely to be confusing for a user.

I think we should consider an additional set of changes to allow a user to specify the PA bits as well as the devices they want to see enabled.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#16 lore raw

Re: [PATCH v4 6/6] hw/arm/virt: Add 'compact-highmem' property

Hi Marc,

Quoted 9 lines · Marc Zyngier
Quoted 7 lines · Gavin Shan

After the improvement to high memory region address assignment is applied, the memory layout can be changed, introducing possible migration breakage. For example, VIRT_HIGH_PCIE_MMIO memory region is disabled or enabled when the optimization is applied or not, with the following configuration.

Quoted 8 lines
Quoted 4 lines
   pa_bits              = 40;
   vms->highmem_redists = false;
   vms->highmem_ecam    = false;
   vms->highmem_mmio    = true;

The question is how are these parameters specified by a user? Short of hacking the code, this isn't really possible.

Yeah, It's impossible to have false for vms->highmem_redists unless the code is hacked.

Quoted 24 lines
Quoted 12 lines
   # qemu-system-aarch64 -accel kvm -cpu host    \
     -machine virt-7.2,compact-highmem={on, off} \
     -m 4G,maxmem=511G -monitor stdio

Region compact-highmem=off compact-highmem=on
----------------------------------------------------------------

   RAM               [1GB         512GB]        [1GB         512GB]
   HIGH_GIC_REDISTS  [512GB       512GB+64MB]   [disabled]
   HIGH_PCIE_ECAM    [512GB+256MB 512GB+512MB]  [disabled]
   HIGH_PCIE_MMIO    [disabled]                 [512GB       1TB]
Quoted 5 lines

In order to keep backwords compatibility, we need to disable the optimization on machines, which is virt-7.1 or ealier than it. It means the optimization is enabled by default from virt-7.2. Besides, 'compact-highmem' property is added so that the optimization can be explicitly enabled or disabled on all machine types by users.

Not directly related to this series, but it seems to me that we should be aiming at reproducible results across HW implementations (at least with KVM). Depending on how many PA bits the HW implements, we end-up with a set of devices or another, which is likely to be confusing for a user.

Quoted 4 lines

I think we should consider an additional set of changes to allow a user to specify the PA bits as well as the devices they want to see enabled.

I think the idea to selectively enable devices (high memory regions) is sensible. For example, users may needn't HIGH_PCIE_MMIO at all in some systems, where they have limited PCI devices.

I'm not sure about PA bits because it has been discovered from hardware and configure the automatically optimized value/bits back to KVM. The optimized value/bits is automatically calculated based on the enabled high memory regions.

Thanks,
Gavin