Skip to contentSkip to conversation

hw/ppc: Remove tswap() calls

PATCH In review Philippe Mathieu-Daudé 3 participants 12 messages Dec 20, 2024 – Apr 25, 2025

lore
Share & tools
lore
#0 lore raw

v4 cover hw/ppc: Remove tswap() calls

Since v3:
- Addressed Nick & Harsh review comments

Remove the tswap() calls on ePAPR, and convert
them to big-endian LD/ST API on sPAPR.

Build-tested only.

Philippe Mathieu-Daudé (6):
hw/ppc/spapr: Convert HPTE() macro as hpte_get_ptr() method hw/ppc/spapr: Convert HPTE_VALID() macro as hpte_is_valid() method hw/ppc/spapr: Convert HPTE_DIRTY() macro as hpte_is_dirty() method hw/ppc/spapr: Convert CLEAN_HPTE() macro as hpte_set_clean() method hw/ppc/spapr: Convert DIRTY_HPTE() macro as hpte_set_dirty() method hw/ppc/epapr: Do not swap ePAPR magic value

 hw/ppc/sam460ex.c     |  2 +-
 hw/ppc/spapr.c        | 63 +++++++++++++++++++++++++++++--------------
 hw/ppc/virtex_ml507.c |  2 +-
 3 files changed, 45 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v4 1/6 hw/ppc/spapr: Convert HPTE() macro as hpte_get_ptr() method

Convert HPTE() macro as hpte_get_ptr() method.

---
 hw/ppc/spapr.c | 38 ++++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 16 deletions(-)
hw/ppc/spapr.c +22 -16
index 3b022e8da9e..bed60afbe22 100644
@@ -1399,7 +1399,13 @@ static bool spapr_get_pate(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu,
     }
 }
 
-#define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
+static uint64_t *hpte_get_ptr(SpaprMachineState *s, unsigned index)
+{
+    uint64_t *table = s->htab;
+
+    return &table[2 * index];
+}
+
 #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
 #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
 #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
@@ -1614,7 +1620,7 @@ int spapr_reallocate_hpt(SpaprMachineState *spapr, int shift, Error **errp)
         spapr->htab_shift = shift;
 
         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
-            DIRTY_HPTE(HPTE(spapr->htab, i));
+            DIRTY_HPTE(hpte_get_ptr(spapr, i));
         }
     }
     /* We're setting up a hash table, so that means we're not radix */
@@ -2172,7 +2178,7 @@ static void htab_save_chunk(QEMUFile *f, SpaprMachineState *spapr,
     qemu_put_be32(f, chunkstart);
     qemu_put_be16(f, n_valid);
     qemu_put_be16(f, n_invalid);
-    qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
+    qemu_put_buffer(f, (void *)hpte_get_ptr(spapr, chunkstart),
                     HASH_PTE_SIZE_64 * n_valid);
 }
 
@@ -2198,16 +2204,16 @@ static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
 
         /* Consume invalid HPTEs */
         while ((index < htabslots)
-               && !HPTE_VALID(HPTE(spapr->htab, index))) {
-            CLEAN_HPTE(HPTE(spapr->htab, index));
+               && !HPTE_VALID(hpte_get_ptr(spapr, index))) {
+            CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
         }
 
         /* Consume valid HPTEs */
         chunkstart = index;
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
-               && HPTE_VALID(HPTE(spapr->htab, index))) {
-            CLEAN_HPTE(HPTE(spapr->htab, index));
+               && HPTE_VALID(hpte_get_ptr(spapr, index))) {
+            CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
         }
 
@@ -2247,7 +2253,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
 
         /* Consume non-dirty HPTEs */
         while ((index < htabslots)
-               && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
+               && !HPTE_DIRTY(hpte_get_ptr(spapr, index))) {
             index++;
             examined++;
         }
@@ -2255,9 +2261,9 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         chunkstart = index;
         /* Consume valid dirty HPTEs */
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
-               && HPTE_DIRTY(HPTE(spapr->htab, index))
-               && HPTE_VALID(HPTE(spapr->htab, index))) {
-            CLEAN_HPTE(HPTE(spapr->htab, index));
+               && HPTE_DIRTY(hpte_get_ptr(spapr, index))
+               && HPTE_VALID(hpte_get_ptr(spapr, index))) {
+            CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
             examined++;
         }
@@ -2265,9 +2271,9 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         invalidstart = index;
         /* Consume invalid dirty HPTEs */
         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
-               && HPTE_DIRTY(HPTE(spapr->htab, index))
-               && !HPTE_VALID(HPTE(spapr->htab, index))) {
-            CLEAN_HPTE(HPTE(spapr->htab, index));
+               && HPTE_DIRTY(hpte_get_ptr(spapr, index))
+               && !HPTE_VALID(hpte_get_ptr(spapr, index))) {
+            CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
             examined++;
         }
@@ -2449,11 +2455,11 @@ static int htab_load(QEMUFile *f, void *opaque, int version_id)
 
         if (spapr->htab) {
             if (n_valid) {
-                qemu_get_buffer(f, HPTE(spapr->htab, index),
+                qemu_get_buffer(f, (void *)hpte_get_ptr(spapr, index),
                                 HASH_PTE_SIZE_64 * n_valid);
             }
             if (n_invalid) {
-                memset(HPTE(spapr->htab, index + n_valid), 0,
+                memset(hpte_get_ptr(spapr, index + n_valid), 0,
                        HASH_PTE_SIZE_64 * n_invalid);
             }
         } else {
Signature · 1 line
2.47.1
#2 lore raw

PATCH v4 2/6 hw/ppc/spapr: Convert HPTE_VALID() macro as hpte_is_valid() method

In reply to Philippe Mathieu-Daudé

Convert HPTE_VALID() macro as hpte_is_valid() method.

sPAPR data structures including the hash page table are big-endian regardless of current CPU endian mode, so use the big-endian LD/ST API to access the hash PTEs.

---
 hw/ppc/spapr.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
hw/ppc/spapr.c +9 -5
index bed60afbe22..43220e93c4b 100644
@@ -1406,7 +1406,11 @@ static uint64_t *hpte_get_ptr(SpaprMachineState *s, unsigned index)
     return &table[2 * index];
 }
 
-#define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
+static bool hpte_is_valid(SpaprMachineState *s, unsigned index)
+{
+    return ldq_be_p(hpte_get_ptr(s, index)) & HPTE64_V_VALID;
+}
+
 #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
 #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
 #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
@@ -2204,7 +2208,7 @@ static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
 
         /* Consume invalid HPTEs */
         while ((index < htabslots)
-               && !HPTE_VALID(hpte_get_ptr(spapr, index))) {
+               && !hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
         }
@@ -2212,7 +2216,7 @@ static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
         /* Consume valid HPTEs */
         chunkstart = index;
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
-               && HPTE_VALID(hpte_get_ptr(spapr, index))) {
+               && hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
         }
@@ -2262,7 +2266,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         /* Consume valid dirty HPTEs */
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
                && HPTE_DIRTY(hpte_get_ptr(spapr, index))
-               && HPTE_VALID(hpte_get_ptr(spapr, index))) {
+               && hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
             examined++;
@@ -2272,7 +2276,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         /* Consume invalid dirty HPTEs */
         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
                && HPTE_DIRTY(hpte_get_ptr(spapr, index))
-               && !HPTE_VALID(hpte_get_ptr(spapr, index))) {
+               && !hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
             examined++;
Signature · 1 line
2.47.1
#3 lore raw

PATCH v4 3/6 hw/ppc/spapr: Convert HPTE_DIRTY() macro as hpte_is_dirty() method

In reply to Philippe Mathieu-Daudé

Convert HPTE_DIRTY() macro as hpte_is_dirty() method.

sPAPR data structures including the hash page table are big-endian regardless of current CPU endian mode, so use the big-endian LD/ST API to access the hash PTEs.

---
 hw/ppc/spapr.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
hw/ppc/spapr.c +8 -4
index 43220e93c4b..31df6fdc40c 100644
@@ -1411,7 +1411,11 @@ static bool hpte_is_valid(SpaprMachineState *s, unsigned index)
     return ldq_be_p(hpte_get_ptr(s, index)) & HPTE64_V_VALID;
 }
 
-#define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
+static bool hpte_is_dirty(SpaprMachineState *s, unsigned index)
+{
+    return ldq_be_p(hpte_get_ptr(s, index)) & HPTE64_V_HPTE_DIRTY;
+}
+
 #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
 #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
 
@@ -2257,7 +2261,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
 
         /* Consume non-dirty HPTEs */
         while ((index < htabslots)
-               && !HPTE_DIRTY(hpte_get_ptr(spapr, index))) {
+               && !hpte_is_dirty(spapr, index)) {
             index++;
             examined++;
         }
@@ -2265,7 +2269,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         chunkstart = index;
         /* Consume valid dirty HPTEs */
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
-               && HPTE_DIRTY(hpte_get_ptr(spapr, index))
+               && hpte_is_dirty(spapr, index)
                && hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
@@ -2275,7 +2279,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         invalidstart = index;
         /* Consume invalid dirty HPTEs */
         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
-               && HPTE_DIRTY(hpte_get_ptr(spapr, index))
+               && hpte_is_dirty(spapr, index)
                && !hpte_is_valid(spapr, index)) {
             CLEAN_HPTE(hpte_get_ptr(spapr, index));
             index++;
Signature · 1 line
2.47.1
#4 lore raw

PATCH v4 4/6 hw/ppc/spapr: Convert CLEAN_HPTE() macro as hpte_set_clean() method

In reply to Philippe Mathieu-Daudé

Convert CLEAN_HPTE() macro as hpte_set_clean() method.

sPAPR data structures including the hash page table are big-endian regardless of current CPU endian mode, so use the big-endian LD/ST API to access the hash PTEs.

---
 hw/ppc/spapr.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
hw/ppc/spapr.c +10 -5
index 31df6fdc40c..e68e8c320f4 100644
@@ -1416,7 +1416,12 @@ static bool hpte_is_dirty(SpaprMachineState *s, unsigned index)
     return ldq_be_p(hpte_get_ptr(s, index)) & HPTE64_V_HPTE_DIRTY;
 }
 
-#define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
+static void hpte_set_clean(SpaprMachineState *s, unsigned index)
+{
+    stq_be_p(hpte_get_ptr(s, index),
+             ldq_be_p(hpte_get_ptr(s, index)) & ~HPTE64_V_HPTE_DIRTY);
+}
+
 #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
 
 /*
@@ -2213,7 +2218,7 @@ static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
         /* Consume invalid HPTEs */
         while ((index < htabslots)
                && !hpte_is_valid(spapr, index)) {
-            CLEAN_HPTE(hpte_get_ptr(spapr, index));
+            hpte_set_clean(spapr, index);
             index++;
         }
 
@@ -2221,7 +2226,7 @@ static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
         chunkstart = index;
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
                && hpte_is_valid(spapr, index)) {
-            CLEAN_HPTE(hpte_get_ptr(spapr, index));
+            hpte_set_clean(spapr, index);
             index++;
         }
 
@@ -2271,7 +2276,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
                && hpte_is_dirty(spapr, index)
                && hpte_is_valid(spapr, index)) {
-            CLEAN_HPTE(hpte_get_ptr(spapr, index));
+            hpte_set_clean(spapr, index);
             index++;
             examined++;
         }
@@ -2281,7 +2286,7 @@ static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
                && hpte_is_dirty(spapr, index)
                && !hpte_is_valid(spapr, index)) {
-            CLEAN_HPTE(hpte_get_ptr(spapr, index));
+            hpte_set_clean(spapr, index);
             index++;
             examined++;
         }
Signature · 1 line
2.47.1
#5 lore raw

PATCH v4 5/6 hw/ppc/spapr: Convert DIRTY_HPTE() macro as hpte_set_dirty() method

In reply to Philippe Mathieu-Daudé

Convert DIRTY_HPTE() macro as hpte_set_dirty() method.

sPAPR data structures including the hash page table are big-endian regardless of current CPU endian mode, so use the big-endian LD/ST API to access the hash PTEs.

---
 hw/ppc/spapr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +6 -2
index e68e8c320f4..d318ce2a350 100644
@@ -1422,7 +1422,11 @@ static void hpte_set_clean(SpaprMachineState *s, unsigned index)
              ldq_be_p(hpte_get_ptr(s, index)) & ~HPTE64_V_HPTE_DIRTY);
 }
 
-#define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
+static void hpte_set_dirty(SpaprMachineState *s, unsigned index)
+{
+    stq_be_p(hpte_get_ptr(s, index),
+             ldq_be_p(hpte_get_ptr(s, index)) | HPTE64_V_HPTE_DIRTY);
+}
 
 /*
  * Get the fd to access the kernel htab, re-opening it if necessary
@@ -1633,7 +1637,7 @@ int spapr_reallocate_hpt(SpaprMachineState *spapr, int shift, Error **errp)
         spapr->htab_shift = shift;
 
         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
-            DIRTY_HPTE(hpte_get_ptr(spapr, i));
+            hpte_set_dirty(spapr, i);
         }
     }
     /* We're setting up a hash table, so that means we're not radix */
Signature · 1 line
2.47.1
#6 lore raw

PATCH v4 6/6 hw/ppc/epapr: Do not swap ePAPR magic value

In reply to Philippe Mathieu-Daudé

The ePAPR magic value in $r6 doesn't need to be byte swapped.

See ePAPR-v1.1.pdf chapter 5.4.1 "Boot CPU Initial Register State" and the following mailing-list threads:
https://lore.kernel.org/qemu-devel/CAFEAcA_NR4XW5DNL4nq7vnH4XRH5UWbhQCxuLyKqYk6_FCBrAA@mail.gmail.com/
https://lore.kernel.org/qemu-devel/[email protected]/

---
 hw/ppc/sam460ex.c     | 2 +-
 hw/ppc/virtex_ml507.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/ppc/sam460ex.c +1 -1
index 78e2a46e753..db9c8f3fa6e 100644
@@ -234,7 +234,7 @@ static void main_cpu_reset(void *opaque)
 
         /* Create a mapping for the kernel.  */
         booke_set_tlb(&env->tlb.tlbe[0], 0, 0, 1 << 31);
-        env->gpr[6] = tswap32(EPAPR_MAGIC);
+        env->gpr[6] = EPAPR_MAGIC;
         env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
 
     } else {
hw/ppc/virtex_ml507.c +1 -1
index f378e5c4a90..6197d31d88f 100644
@@ -119,7 +119,7 @@ static void main_cpu_reset(void *opaque)
     /* Create a mapping spanning the 32bit addr space. */
     booke_set_tlb(&env->tlb.tlbe[0], 0, 0, 1U << 31);
     booke_set_tlb(&env->tlb.tlbe[1], 0x80000000, 0x80000000, 1U << 31);
-    env->gpr[6] = tswap32(EPAPR_MAGIC);
+    env->gpr[6] = EPAPR_MAGIC;
     env->gpr[7] = bi->ima_size;
 }
 
Signature · 1 line
2.47.1
Tested-by on v4 6/6
#7 lore raw

Re: [PATCH v4 6/6] hw/ppc/epapr: Do not swap ePAPR magic value

Quoted 9 lines · Philippe Mathieu-Daudé

The ePAPR magic value in $r6 doesn't need to be byte swapped.

See ePAPR-v1.1.pdf chapter 5.4.1 "Boot CPU Initial Register State" and the following mailing-list threads:
https://lore.kernel.org/qemu-devel/CAFEAcA_NR4XW5DNL4nq7vnH4XRH5UWbhQCxuLyKqYk6_FCBrAA@mail.gmail.com/
https://lore.kernel.org/qemu-devel/[email protected]/

The Linux image I have still seems to boot on sam460ex so

Quoted 32 lines

---
hw/ppc/sam460ex.c | 2 +-
hw/ppc/virtex_ml507.c | 2 +-

2 files changed, 2 insertions(+), 2 deletions(-)
hw/ppc/sam460ex.c +1 -1
index 78e2a46e753..db9c8f3fa6e 100644
@@ -234,7 +234,7 @@ static void main_cpu_reset(void *opaque)
 
        /* Create a mapping for the kernel.  */
        booke_set_tlb(&env->tlb.tlbe[0], 0, 0, 1 << 31);
-        env->gpr[6] = tswap32(EPAPR_MAGIC);
+        env->gpr[6] = EPAPR_MAGIC;
        env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
 
    } else {
hw/ppc/virtex_ml507.c +1 -1
index f378e5c4a90..6197d31d88f 100644
@@ -119,7 +119,7 @@ static void main_cpu_reset(void *opaque)
    /* Create a mapping spanning the 32bit addr space. */
    booke_set_tlb(&env->tlb.tlbe[0], 0, 0, 1U << 31);
    booke_set_tlb(&env->tlb.tlbe[1], 0x80000000, 0x80000000, 1U << 31);
-    env->gpr[6] = tswap32(EPAPR_MAGIC);
+    env->gpr[6] = EPAPR_MAGIC;
    env->gpr[7] = bi->ima_size;
 }
 
Reviewed-by on the v4 series
#8 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé,

Since v3:
- Addressed Nick & Harsh review comments

Remove the tswap() calls on ePAPR, and convert
them to big-endian LD/ST API on sPAPR.

Build-tested only.

Quoted 8 lines

Philippe Mathieu-Daudé (6):
hw/ppc/spapr: Convert HPTE() macro as hpte_get_ptr() method hw/ppc/spapr: Convert HPTE_VALID() macro as hpte_is_valid() method hw/ppc/spapr: Convert HPTE_DIRTY() macro as hpte_is_dirty() method hw/ppc/spapr: Convert CLEAN_HPTE() macro as hpte_set_clean() method hw/ppc/spapr: Convert DIRTY_HPTE() macro as hpte_set_dirty() method hw/ppc/epapr: Do not swap ePAPR magic value

For spapr:

Quoted 10 lines
 hw/ppc/sam460ex.c     |  2 +-
 hw/ppc/spapr.c        | 63 +++++++++++++++++++++++++++++--------------
 hw/ppc/virtex_ml507.c |  2 +-
 3 files changed, 45 insertions(+), 22 deletions(-)

--
2.47.1

#10 lore raw

ping

Quoted 10 lines · Philippe Mathieu-Daudé

Hi Nick,

Ping? (series fully reviewed)

Quoted 5 lines · Philippe Mathieu-Daudé

Since v3:
- Addressed Nick & Harsh  review comments

Remove the tswap() calls on ePAPR, and convert
them to big-endian LD/ST API on sPAPR.

#11 lore raw
Quoted 1 line · Philippe Mathieu-Daudé

ping

Merged as c894bdf78b3..c2ac9f4c297, thanks!

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Remove the tswap() calls on ePAPR, and convert
them to big-endian LD/ST API on sPAPR.