Skip to contentSkip to conversation

hw/ppc: Implement -dtb support for PowerNV

RFC PATCH In review Aditya Gupta 3 participants 6 messages Jul 31, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 hw/ppc: Implement -dtb support for PowerNV

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works
---

---
 hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +26 -3
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
     PnvMachineState *pnv = PNV_MACHINE(machine);
     IPMIBmc *bmc;
     void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
 
     qemu_devices_reset(reason);
 
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
         }
     }
 
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");
 
-    /* Pack resulting tree */
-    _FDT((fdt_pack(fdt)));
+        fdt = g_malloc0(FDT_MAX_SIZE);
+
+        /* read the file 'machine->dtb', and load it into 'fdt' buffer */
+        fdt_file = fopen(machine->dtb, "r");
+        if (fdt_file != NULL) {
+            fdt_size = fread(fdt, sizeof(char), FDT_MAX_SIZE, fdt_file);
+            if (ferror(fdt_file) != 0) {
+                error_report("Could not load dtb '%s'",
+                             machine->dtb);
+                exit(1);
+            }
+
+            /* mark end of the fdt buffer with '\0' */
+            ((char *)fdt)[fdt_size] = '\0';
+        }
+    } else {
+        fdt = pnv_dt_create(machine);
+
+        /* Pack resulting tree */
+        _FDT((fdt_pack(fdt)));
+    }
 
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
Signature · 1 line
2.45.2
#1 lore raw
Quoted 37 lines · Aditya Gupta

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works
---

---
 hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +2 -0
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
     PnvMachineState *pnv = PNV_MACHINE(machine);
     IPMIBmc *bmc;
     void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
 
     qemu_devices_reset(reason);
 

hw/ppc/pnv.c static void pnv_reset(MachineState *machine, ShutdownCause reason) · line 743

Quoted 7 lines
hw/ppc/pnv.c +3 -1
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
         }
     }
 
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");

Check whether append is actually set, and report an fatal error in that case.

Quoted 8 lines
hw/ppc/pnv.c +5 -2
-    /* Pack resulting tree */
-    _FDT((fdt_pack(fdt)));
+        fdt = g_malloc0(FDT_MAX_SIZE);
+
+        /* read the file 'machine->dtb', and load it into 'fdt' buffer */
+        fdt_file = fopen(machine->dtb, "r");
+        if (fdt_file != NULL) {

hw/ppc/pnv.c static void pnv_reset(MachineState *machine, ShutdownCause reason) · line 759

Quoted 10 lines
hw/ppc/pnv.c +10 -0
+            fdt_size = fread(fdt, sizeof(char), FDT_MAX_SIZE, fdt_file);
+            if (ferror(fdt_file) != 0) {
+                error_report("Could not load dtb '%s'",
+                             machine->dtb);
+                exit(1);
+            }
+
+            /* mark end of the fdt buffer with '\0' */
+            ((char *)fdt)[fdt_size] = '\0';
+        }

Preferrable to use g_file_get_contents()

Quoted 13 lines
hw/ppc/pnv.c +6 -0
+    } else {
+        fdt = pnv_dt_create(machine);
+
+        /* Pack resulting tree */
+        _FDT((fdt_pack(fdt)));
+    }
 
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
Signature · 1 line
2.45.2

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw

Hi Daniel,

Thank you for the review.

Quoted 41 lines · Daniel P. Berrangé
Quoted 40 lines · Aditya Gupta

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works
---

---
 hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +2 -0
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
     PnvMachineState *pnv = PNV_MACHINE(machine);
     IPMIBmc *bmc;
     void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
 
     qemu_devices_reset(reason);
 
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
         }
     }
 
Quoted 7 lines

hw/ppc/pnv.c static void pnv_reset(MachineState *machine, ShutdownCause reason) · line 743

Quoted 4 lines
hw/ppc/pnv.c +3 -1
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");

Check whether append is actually set, and report an fatal error in that case.

Got it.

Though there might be more options which might get ignored, such as maybe even -device options, as whatever QEMU adds to it's device tree, that will get ignored, and only the device nodes present in passed DTB will be used.

Quoted 13 lines
Quoted 13 lines
hw/ppc/pnv.c +9 -2
-    /* Pack resulting tree */
-    _FDT((fdt_pack(fdt)));
+        fdt = g_malloc0(FDT_MAX_SIZE);
+
+        /* read the file 'machine->dtb', and load it into 'fdt' buffer */
+        fdt_file = fopen(machine->dtb, "r");
+        if (fdt_file != NULL) {
+            fdt_size = fread(fdt, sizeof(char), FDT_MAX_SIZE, fdt_file);
+            if (ferror(fdt_file) != 0) {
+                error_report("Could not load dtb '%s'",
+                             machine->dtb);
Quoted 8 lines

hw/ppc/pnv.c static void pnv_reset(MachineState *machine, ShutdownCause reason) · line 759

Quoted 6 lines
hw/ppc/pnv.c +6 -0
+                exit(1);
+            }
+
+            /* mark end of the fdt buffer with '\0' */
+            ((char *)fdt)[fdt_size] = '\0';
+        }

Preferrable to use g_file_get_contents()

Sure, thanks, didn't know that !

Thanks,
Aditya Gupta

Quoted 22 lines
Quoted 14 lines
hw/ppc/pnv.c +6 -0
+    } else {
+        fdt = pnv_dt_create(machine);
+
+        /* Pack resulting tree */
+        _FDT((fdt_pack(fdt)));
+    }
 
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
Signature · 1 line
2.45.2

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 54 lines · Aditya Gupta

Hi Daniel,

Thank you for the review.

Quoted 41 lines · Daniel P. Berrangé
Quoted 40 lines · Aditya Gupta

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works
---

---
  hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
  1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +2 -0
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
      PnvMachineState *pnv = PNV_MACHINE(machine);
      IPMIBmc *bmc;
      void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
 
      qemu_devices_reset(reason);
 
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
          }
      }
 
Quoted 7 lines

hw/ppc/pnv.c static void pnv_reset(MachineState *machine, ShutdownCause reason) · line 743

Quoted 4 lines
hw/ppc/pnv.c +3 -1
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");

Check whether append is actually set, and report an fatal error in that case.

Quoted 1 line

Got it.

and this check should be done preferably when the machine is initialized, not in the reset handler.

Thanks,

C.

#4 lore raw
In reply to Aditya Gupta

Hello Aditya,

Quoted 5 lines · Aditya Gupta

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

I thought we had enough controls with the QEMU command line options to generate a custom DTB. We should improve that first. Unless you want to mimic a bogus DTB as generated by hostboot and check skiboot behavior.

Can you explain more the use case please ? Is it for skiboot testing ?

Thanks,

C.

Quoted 65 lines

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works

---
  hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
  1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +26 -3
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
      PnvMachineState *pnv = PNV_MACHINE(machine);
      IPMIBmc *bmc;
      void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
 
      qemu_devices_reset(reason);
 
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
          }
      }
 
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");
 
-    /* Pack resulting tree */
-    _FDT((fdt_pack(fdt)));
+        fdt = g_malloc0(FDT_MAX_SIZE);
+
+        /* read the file 'machine->dtb', and load it into 'fdt' buffer */
+        fdt_file = fopen(machine->dtb, "r");
+        if (fdt_file != NULL) {
+            fdt_size = fread(fdt, sizeof(char), FDT_MAX_SIZE, fdt_file);
+            if (ferror(fdt_file) != 0) {
+                error_report("Could not load dtb '%s'",
+                             machine->dtb);
+                exit(1);
+            }
+
+            /* mark end of the fdt buffer with '\0' */
+            ((char *)fdt)[fdt_size] = '\0';
+        }
+    } else {
+        fdt = pnv_dt_create(machine);
+
+        /* Pack resulting tree */
+        _FDT((fdt_pack(fdt)));
+    }
 
      qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
      cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
#5 lore raw

Hi Cedric,

Quoted 6 lines · Cédric Le Goater

Hello Aditya,

Quoted 3 lines · Aditya Gupta

Currently any device tree passed with -dtb option in QEMU, was ignored by the PowerNV code.

Quoted 8 lines
Quoted 2 lines

Read and pass the passed -dtb to the kernel, thus enabling easier debugging with custom DTBs.

I thought we had enough controls with the QEMU command line options to generate a custom DTB. We should improve that first. Unless you want to mimic a bogus DTB as generated by hostboot and check skiboot behavior.

Can you explain more the use case please ? Is it for skiboot testing ?

My usecase is mostly experimental, where I am changing skiboot's relocation, trying to boot with almost no/minimal parts of skiboot, and thereby I am passing a custom DTB.

Though the DTB I pass is basically the DTB QEMU passes to skiboot, and edited it to remove things, add an 'opal' node, and basically have more control on what device nodes QEMU passes to the firmware/kernel.

The usecase you told seems interesting though, I have not encountered bogus DTBs by hostboot yet (still new), but might help test skiboot when that happens.

'-dtb' option will not be for the usual usecase, but can help try these experimental things with a quick and dirty dtb.

Thanks,
Aditya Gupta

Quoted 67 lines

Thanks,

C.

Quoted 60 lines

The existing behaviour when -dtb is 'not' passed, is preserved as-is.

But when a '-dtb' is passed, it completely overrides any dtb nodes or changes QEMU might have done, such as '-append' arguments to the kernel (which are mentioned in /chosen/bootargs in the dtb), hence add warning when -dtb is being used

---
This is an RFC patch, and hence might not be the final implementation, though this current one is a solution which works

---
  hw/ppc/pnv.c | 29 ++++++++++++++++++++++++++---
  1 file changed, 26 insertions(+), 3 deletions(-)
hw/ppc/pnv.c +26 -3
index 3526852685b4..12cc909b9e26 100644
@@ -714,6 +714,8 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
      PnvMachineState *pnv = PNV_MACHINE(machine);
      IPMIBmc *bmc;
      void *fdt;
+    FILE *fdt_file;
+    uint32_t fdt_size;
      qemu_devices_reset(reason);
@@ -736,10 +738,31 @@ static void pnv_reset(MachineState *machine, ShutdownCause reason)
          }
      }
-    fdt = pnv_dt_create(machine);
+    if (machine->dtb) {
+        warn_report("with manually passed dtb, some options like '-append'"
+                " might ignored and the dtb passed will be used as-is");
-    /* Pack resulting tree */
-    _FDT((fdt_pack(fdt)));
+        fdt = g_malloc0(FDT_MAX_SIZE);
+
+        /* read the file 'machine->dtb', and load it into 'fdt' buffer */
+        fdt_file = fopen(machine->dtb, "r");
+        if (fdt_file != NULL) {
+            fdt_size = fread(fdt, sizeof(char), FDT_MAX_SIZE, fdt_file);
+            if (ferror(fdt_file) != 0) {
+                error_report("Could not load dtb '%s'",
+                             machine->dtb);
+                exit(1);
+            }
+
+            /* mark end of the fdt buffer with '\0' */
+            ((char *)fdt)[fdt_size] = '\0';
+        }
+    } else {
+        fdt = pnv_dt_create(machine);
+
+        /* Pack resulting tree */
+        _FDT((fdt_pack(fdt)));
+    }
      qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
      cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));