All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
To: qemu-devel@nongnu.org
Subject: [PULL 16/51] hw/block/pflash_cfi02: Add migration support
Date: Thu,  3 Sep 2026 12:39:00 +0200	[thread overview]
Message-ID: <20260903103936.62355-17-philmd@oss.qualcomm.com> (raw)
In-Reply-To: <20260903103936.62355-1-philmd@oss.qualcomm.com>

From: Om Barkare <ombarkare123@gmail.com>

Device did not have migration support, which would result in its
internal state being lost during migration or saves.

Add VMStateDescription to serialize state and set memory regions
romd_mode to rom_mode field in post load hook

total_sectors was changed from uint32_t to int32_t to satisfy the
VMSTATE_BITMAP macro's _field_size argument, but that is OK because
it's a value is calculated using the size of the flash and it will
never be large enough to overflow int32_t

The only boards using pflash_cfi02 are the sh4 r2d and
the arm canon-a1100, musicpal and xilinx-zynq-a9

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4157

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Om Barkare <ombarkare123@gmail.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-ID: <20260827193942.69406-1-ombarkare123@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
 hw/block/pflash_cfi02.c | 52 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
index 6f952fe7de0..4f72c438c0c 100644
--- a/hw/block/pflash_cfi02.c
+++ b/hw/block/pflash_cfi02.c
@@ -46,6 +46,7 @@
 #include "qemu/module.h"
 #include "hw/core/sysbus.h"
 #include "migration/vmstate.h"
+#include "system/runstate.h"
 #include "trace.h"
 
 #define PFLASH_LAZY_ROMD_THRESHOLD 42
@@ -71,7 +72,7 @@ struct PFlashCFI02 {
     BlockBackend *blk;
     uint32_t uniform_nb_blocs;
     uint32_t uniform_sector_len;
-    uint32_t total_sectors;
+    int32_t total_sectors;
     uint32_t nb_blocs[PFLASH_MAX_ERASE_REGIONS];
     uint32_t sector_len[PFLASH_MAX_ERASE_REGIONS];
     uint32_t chip_len;
@@ -107,6 +108,29 @@ struct PFlashCFI02 {
     unsigned long *sector_erase_map;
     char *name;
     void *storage;
+    VMChangeStateEntry *vmstate;
+};
+
+static int pflash_post_load(void *opaque, int version_id);
+
+static const VMStateDescription vmstate_pflash = {
+    .name = "pflash_cfi02",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .post_load = pflash_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_INT32(wcycle, PFlashCFI02),
+        VMSTATE_INT32(bypass, PFlashCFI02),
+        VMSTATE_UINT8(cmd, PFlashCFI02),
+        VMSTATE_UINT8(status, PFlashCFI02),
+        VMSTATE_TIMER(timer, PFlashCFI02),
+        VMSTATE_BOOL(rom_mode, PFlashCFI02),
+        VMSTATE_INT32(read_counter, PFlashCFI02),
+        VMSTATE_INT32(sectors_to_erase, PFlashCFI02),
+        VMSTATE_UINT64(erase_time_remaining, PFlashCFI02),
+        VMSTATE_BITMAP(sector_erase_map, PFlashCFI02, 1, total_sectors),
+        VMSTATE_END_OF_LIST()
+    }
 };
 
 /*
@@ -976,6 +1000,7 @@ static void pflash_cfi02_class_init(ObjectClass *klass, const void *data)
     device_class_set_legacy_reset(dc, pflash_cfi02_reset);
     dc->unrealize = pflash_cfi02_unrealize;
     device_class_set_props(dc, pflash_cfi02_properties);
+    dc->vmsd = &vmstate_pflash;
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 }
 
@@ -1028,3 +1053,28 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
     return PFLASH_CFI02(dev);
 }
+
+static void postload_update_cb(void *opaque, bool running, RunState state)
+{
+    PFlashCFI02 *pfl = opaque;
+
+    /* This is called after bdrv_activate_all.  */
+    qemu_del_vm_change_state_handler(pfl->vmstate);
+    pfl->vmstate = NULL;
+
+    trace_pflash_postload_cb(pfl->name);
+    pflash_update(pfl, 0, pfl->chip_len);
+}
+
+static int pflash_post_load(void *opaque, int version_id)
+{
+    PFlashCFI02 *pfl = opaque;
+
+    if (!pfl->ro) {
+        pfl->vmstate = qemu_add_vm_change_state_handler(postload_update_cb, pfl);
+    }
+
+    memory_region_rom_device_set_romd(&pfl->orig_mem, pfl->rom_mode);
+
+    return 0;
+}
-- 
2.53.0



  parent reply	other threads:[~2026-09-03 10:41 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-03 10:38 [PULL 00/51] Misc HW/accel patches for 2026-09-03 Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 01/51] hw/sd: give the RPMB vmstate subsection a name of its own Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 02/51] hw/sd: sdhci: Fix SDMA boundary bug Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 03/51] qtest: add xilinx-zynq SDHCI sdma test Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 04/51] hw/sd: sdhci: Migrate the Host Control 2 register Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 05/51] hw/sd: sdhci: Accept version 4 enable without UHS-I Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 06/51] hw/sd: sdhci: Factor sdhci_advance_sdma_address() helper out Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 07/51] hw/sd: sdhci: Factor sdhci_sdma_address() " Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 08/51] hw/sd: sdhci: Use version 4 system address for SDMA Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 09/51] hw/sd: sdhci: Support version 4 ADMA 64-bit addressing Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 10/51] hw/sd: sdhci: Resume version 4 SDMA at buffer boundaries Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 11/51] hw/sd/sdcard: Register device ops in drives without media Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 12/51] hw/cxl: fix timer leak in cxl_destroy_cci() Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 13/51] hw/sd/axiado_sdhci: Add header guard Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 14/51] MAINTAINERS: Cover pflash QTest in pflash section Philippe Mathieu-Daudé
2026-09-03 10:38 ` [PULL 15/51] hw/block/pflash_cfi01: Always set romd mode when clearing wcycle and cmd Philippe Mathieu-Daudé
2026-09-03 10:39 ` Philippe Mathieu-Daudé [this message]
2026-09-03 10:39 ` [PULL 17/51] accel/tcg: Rename for exception codes named @ret as @excp Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 18/51] accel/tcg: Restrict EXCP_HALTED handling to system emulation Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 19/51] accel/tcg: Check %halted field in cpu_handle_halt() caller Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 20/51] target/i386: Remove const qualifier in ptw_setl*() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 21/51] linux-user: Uncast void pointer argument as Object in target_cpu_free() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 22/51] cpus: Add const-qualified CPU environment accessors Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 23/51] linux-user: Replace env_cpu_const() by generic env_cpu() equivalent Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 24/51] system/cpus: Constify various CPUState arguments Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 25/51] target/avr: Constify CPUAVRState for some cpu_*() getters Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 26/51] target/hexagon: Constify CPUHexagonState in hexagon_thread_is_enabled() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 27/51] target/i386: Constify CPU*State for cpu_*_interrupt() getters Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 28/51] target/loongarch: Constify LoongArchTLB Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 29/51] target/mips: Constify CPUMIPSState for various cpu_*() getters Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 30/51] target/s390x: Constify S390CPU for cpu_has_*() getters Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 31/51] target/riscv: Constify @iprio argument in riscv_cpu_pending_to_irq() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 32/51] target/sparc: Constify CPUSPARCState for various cpu_*() getters Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 33/51] target/tricore: Document architectural interrupts as not implemented Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 34/51] target/xtensa: Constify CPUXtensaState in xtensa_replicate_windowstart() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 35/51] target/arm: Return immediately on error in kvm_arch_init() Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 36/51] qom/object: add is_available callback to TypeInfo Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 37/51] hw/arm: filter minimal set of machines Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 38/51] system: query machines using TYPE_MACHINE Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 39/51] target-info: remove machine_typename Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 40/51] hw/arm: remove TYPE_TARGET_{AARCH64,ARM}_MACHINE Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 41/51] hw/arm: remove {arm, arm_aarch64, aarch64}_machine_interfaces Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 42/51] hw/arm: remove DEFINE_MACHINE_{AARCH64,ARM} Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 43/51] hw/arm: remove machines-qom.h Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 44/51] hw/riscv: remove TYPE_TARGET_{RISCV32,RISCV64}_MACHINE Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 45/51] hw/riscv: remove {riscv32, riscv32_64, riscv64}_machine_interfaces Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 46/51] hw/riscv: remove DEFINE_MACHINE_{RISCV32,RISCV64} Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 47/51] hw/riscv: remove machines-qom.h Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 48/51] rustfmt: disable wrap_comments Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 49/51] system: improve -mon deprecation warning Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 50/51] MAINTAINERS: Stop reviewing meson-related patches Philippe Mathieu-Daudé
2026-09-03 10:39 ` [PULL 51/51] MAINTAINERS: Update PhilMD's email address Philippe Mathieu-Daudé
2026-09-03 19:15 ` [PULL 00/51] Misc HW/accel patches for 2026-09-03 Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260903103936.62355-17-philmd@oss.qualcomm.com \
    --to=philmd@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.