qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper
@ 2021-03-26  0:27 Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions Philippe Mathieu-Daudé
                   ` (11 more replies)
  0 siblings, 12 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

Hi,

This series introduce the memory_region_add_subregion_aliased()
helper which basically create a device which maps a subregion
multiple times.

Examples are easier, so having a subregion aliased every @span_size
then mapped onto a container at an offset, you get something like:

          ^-----------^
          |           |
          |           |
          | +-------+ |                 +---------+          <--+
          |           |                 +---------+             |
          |           |                 |         |             |
          |           |   +-----------> | alias#3 |             |
          |           |   |             |         |             |
          |           |   |             +---------+             |
          |           |   |             +---------+             |
          |           |   |             |         |             |
          |           |   |   +-------> | alias#2 |             |
          |           |   |   |         |         |             |region
          | container |   |   |         +---------+             | size
          |           |   |   |         +---------+             |
          |           |   |   |         |         |             |
          |           |   |   |  +----> | alias#1 |             |
          |           |   |   |  |      |         |             |
          |           |   |   |  |      +---------+  <--+       |
          |           | +-+---+--+--+   +---------+     |       |
          |           | |           |   |         |     |span   |
          |           | | subregion +-> | alias#0 |     |size   |
   offset |           | |           |   |         |     |       |
   +----> | +-------+ | +-----------+   +---------+  <--+    <--+
   |      |           |
   |      |           |
   |      |           |
   |      |           |
   |      |           |
   |      ^-----------^

I know it need more documentation and tests, but I prefer to send
as draft RFC for early review before spending more time on it.

Based-on: <20210325120921.858993-1-f4bug@amsat.org>
https://www.mail-archive.com/qemu-devel@nongnu.org/msg795218.html

Philippe Mathieu-Daudé (10):
  hw/misc: Add device to help managing aliased memory regions
  hw/arm/musicpal: Open-code pflash_cfi02_register() call
  hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased()
  hw/arm/digic: Open-code pflash_cfi02_register() call
  hw/arm/digic: Map flash using memory_region_add_subregion_aliased()
  hw/block/pflash_cfi02: Remove pflash_setup_mappings()
  hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype
  hw/misc/aliased_region: Simplify aliased I/O regions
  hw/m68k/q800: Add MacIO container
  hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased()

 include/hw/block/flash.h         |   1 -
 include/hw/misc/aliased_region.h |  91 +++++++++++++++
 hw/arm/digic_boards.c            |  28 ++++-
 hw/arm/musicpal.c                |  29 ++++-
 hw/arm/xilinx_zynq.c             |   2 +-
 hw/block/pflash_cfi02.c          |  36 +-----
 hw/lm32/lm32_boards.c            |   4 +-
 hw/m68k/q800.c                   |  61 +++++-----
 hw/misc/aliased_region.c         | 185 +++++++++++++++++++++++++++++++
 hw/ppc/ppc405_boards.c           |   6 +-
 hw/sh4/r2d.c                     |   2 +-
 MAINTAINERS                      |   6 +
 hw/arm/Kconfig                   |   2 +
 hw/m68k/Kconfig                  |   1 +
 hw/misc/Kconfig                  |   3 +
 hw/misc/meson.build              |   1 +
 16 files changed, 375 insertions(+), 83 deletions(-)
 create mode 100644 include/hw/misc/aliased_region.h
 create mode 100644 hw/misc/aliased_region.c

-- 
2.26.2



^ permalink raw reply	[flat|nested] 16+ messages in thread

* [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [PATCH-for-6.1 02/10] hw/arm/musicpal: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

// TODO explain here how buses work? when some address lines are
// not bound we get memory aliasing, high addresses are masked.
// etc...

Add a helper to manage this use case easily.

For example a having @span_size = @region_size / 4 we get such mapping:

          ^-----------^
          |           |
          |           |
          | +-------+ |                 +---------+          <--+
          |           |                 +---------+             |
          |           |                 |         |             |
          |           |   +-----------> | alias#3 |             |
          |           |   |             |         |             |
          |           |   |             +---------+             |
          |           |   |             +---------+             |
          |           |   |             |         |             |
          |           |   |   +-------> | alias#2 |             |
          |           |   |   |         |         |             |region
          | container |   |   |         +---------+             | size
          |           |   |   |         +---------+             |
          |           |   |   |         |         |             |
          |           |   |   |  +----> | alias#1 |             |
          |           |   |   |  |      |         |             |
          |           |   |   |  |      +---------+  <--+       |
          |           | +-+---+--+--+   +---------+     |       |
          |           | |           |   |         |     |span   |
          |           | | subregion +-> | alias#0 |     |size   |
   offset |           | |           |   |         |     |       |
   +----> | +-------+ | +-----------+   +---------+  <--+    <--+
   |      |           |
   |      |           |
   |      |           |
   |      |           |
   |      |           |
   |      ^-----------^

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/misc/aliased_region.h |  87 ++++++++++++++++++++
 hw/misc/aliased_region.c         | 133 +++++++++++++++++++++++++++++++
 MAINTAINERS                      |   6 ++
 hw/misc/Kconfig                  |   3 +
 hw/misc/meson.build              |   1 +
 5 files changed, 230 insertions(+)
 create mode 100644 include/hw/misc/aliased_region.h
 create mode 100644 hw/misc/aliased_region.c

diff --git a/include/hw/misc/aliased_region.h b/include/hw/misc/aliased_region.h
new file mode 100644
index 00000000000..0ce0d5d1cef
--- /dev/null
+++ b/include/hw/misc/aliased_region.h
@@ -0,0 +1,87 @@
+/*
+ * Aliased memory regions
+ *
+ * Copyright (c) 2018  Philippe Mathieu-Daudé <f4bug@amsat.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_MISC_ALIASED_REGION_H
+#define HW_MISC_ALIASED_REGION_H
+
+#include "exec/memory.h"
+#include "hw/sysbus.h"
+
+#define TYPE_ALIASED_REGION "aliased-memory-region"
+OBJECT_DECLARE_SIMPLE_TYPE(AliasedRegionState, ALIASED_REGION)
+
+struct AliasedRegionState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion container;
+    uint64_t region_size;
+    uint64_t span_size;
+    MemoryRegion *mr;
+
+    struct {
+        size_t count;
+        MemoryRegion *alias;
+    } mem;
+};
+
+/**
+ * memory_region_add_subregion_aliased:
+ * @container: the #MemoryRegion to contain the aliased subregions.
+ * @offset: the offset relative to @container where the aliased subregion
+ *          are added.
+ * @region_size: size of the region containing the aliased subregions.
+ * @subregion: the subregion to be aliased.
+ * @span_size: size between each aliased subregion
+ *
+ * This utility function creates and maps an instance of aliased-memory-region,
+ * which is a dummy device of a single region which simply contains multiple
+ * aliases of the provided @subregion, spanned over the @region_size every
+ * @span_size. The device is mapped at @offset within @container.
+ *
+ * For example a having @span_size = @region_size / 4 we get such mapping:
+ *
+ *               +-----------+
+ *               |           |
+ *               |           |
+ *               | +-------+ |                 +---------+          <--+
+ *               |           |                 +---------+             |
+ *               |           |                 |         |             |
+ *               |           |   +-----------> | alias#3 |             |
+ *               |           |   |             |         |             |
+ *               |           |   |             +---------+             |
+ *               |           |   |             +---------+             |
+ *               |           |   |             |         |             |
+ *               |           |   |   +-------> | alias#2 |             |
+ *               |           |   |   |         |         |             |region
+ *               | container |   |   |         +---------+             | size
+ *               |           |   |   |         +---------+             |
+ *               |           |   |   |         |         |             |
+ *               |           |   |   |  +----> | alias#1 |             |
+ *               |           |   |   |  |      |         |             |
+ *               |           |   |   |  |      +---------+  <--+       |
+ *               |           | +-+---+--+--+   +---------+     |       |
+ *               |           | |           |   |         |     |span   |
+ *               |           | | subregion +-> | alias#0 |     |size   |
+ *        offset |           | |           |   |         |     |       |
+ *        +----> | +-------+ | +-----------+   +---------+  <--+    <--+
+ *        |      |           |
+ *        |      |           |
+ *        |      |           |
+ *        |      |           |
+ *        |      |           |
+ *        +      +-----------+
+ */
+void memory_region_add_subregion_aliased(MemoryRegion *container,
+                                         hwaddr offset,
+                                         uint64_t region_size,
+                                         MemoryRegion *subregion,
+                                         uint64_t span_size);
+
+#endif
diff --git a/hw/misc/aliased_region.c b/hw/misc/aliased_region.c
new file mode 100644
index 00000000000..8fcc63f2648
--- /dev/null
+++ b/hw/misc/aliased_region.c
@@ -0,0 +1,133 @@
+/*
+ * Aliased memory regions
+ *
+ * Copyright (c) 2018  Philippe Mathieu-Daudé <f4bug@amsat.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "qapi/error.h"
+#include "hw/hw.h"
+#include "hw/sysbus.h"
+#include "hw/misc/aliased_region.h"
+#include "hw/qdev-properties.h"
+
+static void aliased_mem_realize(AliasedRegionState *s, const char *mr_name)
+{
+    uint64_t subregion_size;
+    int subregion_bits;
+
+    memory_region_init(&s->container, OBJECT(s), mr_name, s->region_size);
+
+    subregion_bits = 64 - clz64(s->span_size - 1);
+    s->mem.count = s->region_size >> subregion_bits;
+    assert(s->mem.count > 1);
+    subregion_size = 1ULL << subregion_bits;
+
+    s->mem.alias = g_new(MemoryRegion, s->mem.count);
+    for (size_t i = 0; i < s->mem.count; i++) {
+        g_autofree char *name = g_strdup_printf("%s [#%zu/%zu]",
+                                                memory_region_name(s->mr),
+                                                i, s->mem.count);
+        memory_region_init_alias(&s->mem.alias[i], OBJECT(s), name,
+                                 s->mr, 0, s->span_size);
+        memory_region_add_subregion(&s->container, i * subregion_size,
+                                    &s->mem.alias[i]);
+    }
+}
+
+static void aliased_mr_realize(DeviceState *dev, Error **errp)
+{
+    AliasedRegionState *s = ALIASED_REGION(dev);
+    g_autofree char *name = NULL, *span = NULL;
+
+    if (s->region_size == 0) {
+        error_setg(errp, "property 'region-size' not specified or zero");
+        return;
+    }
+
+    if (s->mr == NULL) {
+        error_setg(errp, "property 'iomem' not specified");
+        return;
+    }
+
+    if (!s->span_size) {
+        s->span_size = pow2ceil(memory_region_size(s->mr));
+    } else if (!is_power_of_2(s->span_size)) {
+        error_setg(errp, "property 'span-size' must be a power of 2");
+        return;
+    }
+
+    span = size_to_str(s->span_size);
+    name = g_strdup_printf("masked %s [span of %s]",
+                           memory_region_name(s->mr), span);
+    aliased_mem_realize(s, name);
+    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->container);
+}
+
+static void aliased_mr_unrealize(DeviceState *dev)
+{
+    AliasedRegionState *s = ALIASED_REGION(dev);
+
+    g_free(s->mem.alias);
+}
+
+static Property aliased_mr_properties[] = {
+    DEFINE_PROP_UINT64("region-size", AliasedRegionState, region_size, 0),
+    DEFINE_PROP_UINT64("span-size", AliasedRegionState, span_size, 0),
+    DEFINE_PROP_LINK("iomem", AliasedRegionState, mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void aliased_mr_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = aliased_mr_realize;
+    dc->unrealize = aliased_mr_unrealize;
+    /* Reason: needs to be wired up to work */
+    dc->user_creatable = false;
+    device_class_set_props(dc, aliased_mr_properties);
+}
+
+static const TypeInfo aliased_mr_info = {
+    .name = TYPE_ALIASED_REGION,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(AliasedRegionState),
+    .class_init = aliased_mr_class_init,
+};
+
+static void aliased_mr_register_types(void)
+{
+    type_register_static(&aliased_mr_info);
+}
+
+type_init(aliased_mr_register_types)
+
+void memory_region_add_subregion_aliased(MemoryRegion *container,
+                                         hwaddr offset,
+                                         uint64_t region_size,
+                                         MemoryRegion *subregion,
+                                         uint64_t span_size)
+{
+    DeviceState *dev;
+
+    if (!region_size) {
+        region_size = pow2ceil(memory_region_size(container));
+    } else {
+        assert(region_size <= memory_region_size(container));
+    }
+
+    dev = qdev_new(TYPE_ALIASED_REGION);
+    qdev_prop_set_uint64(dev, "region-size", region_size);
+    qdev_prop_set_uint64(dev, "span-size", span_size);
+    object_property_set_link(OBJECT(dev), "iomem", OBJECT(subregion),
+                             &error_abort);
+    sysbus_realize(SYS_BUS_DEVICE(dev), &error_abort);
+
+    memory_region_add_subregion(container, offset,
+                                sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
+}
diff --git a/MAINTAINERS b/MAINTAINERS
index 554be84b321..f82ffd50a91 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2097,6 +2097,12 @@ S: Maintained
 F: include/hw/misc/empty_slot.h
 F: hw/misc/empty_slot.c
 
+Aliased memory region
+M: Philippe Mathieu-Daudé <f4bug@amsat.org>
+S: Maintained
+F: include/hw/misc/aliased_region.h
+F: hw/misc/aliased_region.c
+
 Standard VGA
 M: Gerd Hoffmann <kraxel@redhat.com>
 S: Maintained
diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig
index c71ed258204..ca51b99989e 100644
--- a/hw/misc/Kconfig
+++ b/hw/misc/Kconfig
@@ -151,6 +151,9 @@ config AUX
 config UNIMP
     bool
 
+config ALIASED_REGION
+    bool
+
 config LED
     bool
 
diff --git a/hw/misc/meson.build b/hw/misc/meson.build
index 21034dc60a8..e65541b835f 100644
--- a/hw/misc/meson.build
+++ b/hw/misc/meson.build
@@ -12,6 +12,7 @@
 softmmu_ss.add(when: 'CONFIG_EMC141X', if_true: files('emc141x.c'))
 softmmu_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c'))
 softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c'))
+softmmu_ss.add(when: 'CONFIG_ALIASED_REGION', if_true: files('aliased_region.c'))
 softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
 softmmu_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c'))
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH-for-6.1 02/10] hw/arm/musicpal: Open-code pflash_cfi02_register() call
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Jan Kiszka, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

To be able to manually map the flash region on the main memory
(in the next commit), first expand the pflash_cfi02_register
in place.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/arm/musicpal.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
index 9cebece2de0..8b58b66f263 100644
--- a/hw/arm/musicpal.c
+++ b/hw/arm/musicpal.c
@@ -10,6 +10,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/units.h"
 #include "qapi/error.h"
 #include "cpu.h"
 #include "hw/sysbus.h"
@@ -1640,6 +1641,7 @@ static void musicpal_init(MachineState *machine)
     /* Register flash */
     dinfo = drive_get(IF_PFLASH, 0, 0);
     if (dinfo) {
+        static const size_t sector_size = 64 * KiB;
         BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
 
         flash_size = blk_getlength(blk);
@@ -1649,17 +1651,30 @@ static void musicpal_init(MachineState *machine)
             exit(1);
         }
 
+        dev = qdev_new(TYPE_PFLASH_CFI02);
+        qdev_prop_set_drive(dev, "drive", blk);
+        qdev_prop_set_uint32(dev, "num-blocks", flash_size / sector_size);
+        qdev_prop_set_uint32(dev, "sector-length", sector_size);
+        qdev_prop_set_uint8(dev, "width", 2); /* 16-bit */
+        qdev_prop_set_uint8(dev, "mappings", MP_FLASH_SIZE_MAX / flash_size);
+        qdev_prop_set_uint8(dev, "big-endian", 0);
+        qdev_prop_set_uint16(dev, "id0", 0x00bf);
+        qdev_prop_set_uint16(dev, "id1", 0x236d);
+        qdev_prop_set_uint16(dev, "id2", 0x0000);
+        qdev_prop_set_uint16(dev, "id3", 0x0000);
+        qdev_prop_set_uint16(dev, "unlock-addr0", 0x5555);
+        qdev_prop_set_uint16(dev, "unlock-addr1", 0x2aaa);
+        qdev_prop_set_string(dev, "name", "musicpal.flash");
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+
+        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
+                        0x100000000ULL - MP_FLASH_SIZE_MAX);
+
         /*
          * The original U-Boot accesses the flash at 0xFE000000 instead of
          * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
          * image is smaller than 32 MB.
          */
-        pflash_cfi02_register(0x100000000ULL - MP_FLASH_SIZE_MAX,
-                              "musicpal.flash", flash_size,
-                              blk, 0x10000,
-                              MP_FLASH_SIZE_MAX / flash_size,
-                              2, 0x00BF, 0x236D, 0x0000, 0x0000,
-                              0x5555, 0x2AAA, 0);
     }
     sysbus_create_simple(TYPE_MV88W8618_FLASHCFG, MP_FLASHCFG_BASE, NULL);
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased()
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [PATCH-for-6.1 02/10] hw/arm/musicpal: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26 22:49   ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [PATCH-for-6.1 04/10] hw/arm/digic: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  11 siblings, 1 reply; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Jan Kiszka, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

Instead of using a device specific feature for mapping the
flash memory multiple times over a wider region, use the
generic memory_region_add_subregion_aliased() helper.

There is no change in the memory layout.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/arm/musicpal.c | 11 +++++++----
 hw/arm/Kconfig    |  1 +
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
index 8b58b66f263..7d1f2f3fb3f 100644
--- a/hw/arm/musicpal.c
+++ b/hw/arm/musicpal.c
@@ -30,6 +30,7 @@
 #include "hw/irq.h"
 #include "hw/or-irq.h"
 #include "hw/audio/wm8750.h"
+#include "hw/misc/aliased_region.h"
 #include "sysemu/block-backend.h"
 #include "sysemu/runstate.h"
 #include "sysemu/dma.h"
@@ -1656,7 +1657,7 @@ static void musicpal_init(MachineState *machine)
         qdev_prop_set_uint32(dev, "num-blocks", flash_size / sector_size);
         qdev_prop_set_uint32(dev, "sector-length", sector_size);
         qdev_prop_set_uint8(dev, "width", 2); /* 16-bit */
-        qdev_prop_set_uint8(dev, "mappings", MP_FLASH_SIZE_MAX / flash_size);
+        qdev_prop_set_uint8(dev, "mappings", 0);
         qdev_prop_set_uint8(dev, "big-endian", 0);
         qdev_prop_set_uint16(dev, "id0", 0x00bf);
         qdev_prop_set_uint16(dev, "id1", 0x236d);
@@ -1667,14 +1668,16 @@ static void musicpal_init(MachineState *machine)
         qdev_prop_set_string(dev, "name", "musicpal.flash");
         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
-        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
-                        0x100000000ULL - MP_FLASH_SIZE_MAX);
-
         /*
          * The original U-Boot accesses the flash at 0xFE000000 instead of
          * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
          * image is smaller than 32 MB.
          */
+        memory_region_add_subregion_aliased(get_system_memory(),
+                                0x100000000ULL - MP_FLASH_SIZE_MAX,
+                                MP_FLASH_SIZE_MAX,
+                                sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0),
+                                flash_size);
     }
     sysbus_create_simple(TYPE_MV88W8618_FLASHCFG, MP_FLASHCFG_BASE, NULL);
 
diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index 8c37cf00da7..aa8553b3cd3 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -101,6 +101,7 @@ config MUSICPAL
     select MARVELL_88W8618
     select PTIMER
     select PFLASH_CFI02
+    select ALIASED_REGION
     select SERIAL
     select WM8750
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH-for-6.1 04/10] hw/arm/digic: Open-code pflash_cfi02_register() call
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 05/10] hw/arm/digic: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Antony Pavlov,
	Edgar E. Iglesias, David Gibson

To be able to manually map the flash region on the main memory
(in the next commit), first expand the pflash_cfi02_register
in place.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/arm/digic_boards.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/hw/arm/digic_boards.c b/hw/arm/digic_boards.c
index 6cdc1d83fca..fc4a671b2e1 100644
--- a/hw/arm/digic_boards.c
+++ b/hw/arm/digic_boards.c
@@ -31,6 +31,8 @@
 #include "hw/boards.h"
 #include "exec/address-spaces.h"
 #include "qemu/error-report.h"
+#include "hw/qdev-properties.h"
+#include "hw/misc/aliased_region.h"
 #include "hw/arm/digic.h"
 #include "hw/block/flash.h"
 #include "hw/loader.h"
@@ -120,12 +122,25 @@ static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
 #define FLASH_K8P3215UQB_SIZE (4 * 1024 * 1024)
 #define FLASH_K8P3215UQB_SECTOR_SIZE (64 * 1024)
 
-    pflash_cfi02_register(addr, "pflash", FLASH_K8P3215UQB_SIZE,
-                          NULL, FLASH_K8P3215UQB_SECTOR_SIZE,
-                          DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE,
-                          4,
-                          0x00EC, 0x007E, 0x0003, 0x0001,
-                          0x0555, 0x2aa, 0);
+    DeviceState *dev = qdev_new(TYPE_PFLASH_CFI02);
+
+    qdev_prop_set_uint32(dev, "num-blocks",
+                         FLASH_K8P3215UQB_SIZE / FLASH_K8P3215UQB_SECTOR_SIZE);
+    qdev_prop_set_uint32(dev, "sector-length", FLASH_K8P3215UQB_SECTOR_SIZE);
+    qdev_prop_set_uint8(dev, "width", 4); /* 32-bit */
+    qdev_prop_set_uint8(dev, "mappings",
+                        DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE);
+    qdev_prop_set_uint8(dev, "big-endian", 0);
+    qdev_prop_set_uint16(dev, "id0", 0x00ec);
+    qdev_prop_set_uint16(dev, "id1", 0x007e);
+    qdev_prop_set_uint16(dev, "id2", 0x0003);
+    qdev_prop_set_uint16(dev, "id3", 0x0001);
+    qdev_prop_set_uint16(dev, "unlock-addr0", 0x0555);
+    qdev_prop_set_uint16(dev, "unlock-addr1", 0x2aa);
+    qdev_prop_set_string(dev, "name", "pflash");
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
 
     digic_load_rom(s, addr, FLASH_K8P3215UQB_SIZE, filename);
 }
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [RFC PATCH-for-6.1 05/10] hw/arm/digic: Map flash using memory_region_add_subregion_aliased()
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2021-03-26  0:27 ` [PATCH-for-6.1 04/10] hw/arm/digic: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings() Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Antony Pavlov,
	Edgar E. Iglesias, David Gibson

Instead of using a device specific feature for mapping the
flash memory multiple times over a wider region, use the
generic memory_region_add_subregion_aliased() helper.

There is no change in the memory layout.

* before:

  $ qemu-system-arm -M canon-a1100 -S -monitor stdio
  QEMU 5.2.90 monitor - type 'help' for more information
  (qemu) info mtree
  address-space: memory
    0000000000000000-ffffffffffffffff (prio 0, i/o): system
      0000000000000000-0000000003ffffff (prio 0, ram): ram
      00000000c0210000-00000000c02100ff (prio 0, i/o): digic-timer
      00000000c0210100-00000000c02101ff (prio 0, i/o): digic-timer
      00000000c0210200-00000000c02102ff (prio 0, i/o): digic-timer
      00000000c0800000-00000000c0800017 (prio 0, i/o): digic-uart
      00000000f8000000-00000000ffffffff (prio 0, i/o): pflash
        00000000f8000000-00000000f83fffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff
        00000000f8400000-00000000f87fffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff
        00000000f8800000-00000000f8bfffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff
        ...
        00000000ff400000-00000000ff7fffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff
        00000000ff800000-00000000ffbfffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff
        00000000ffc00000-00000000ffffffff (prio 0, romd): alias pflash-alias @pflash 0000000000000000-00000000003fffff

* after:

  (qemu) info mtree
  address-space: memory
    0000000000000000-ffffffffffffffff (prio 0, i/o): system
      0000000000000000-0000000003ffffff (prio 0, ram): ram
      00000000c0210000-00000000c02100ff (prio 0, i/o): digic-timer
      00000000c0210100-00000000c02101ff (prio 0, i/o): digic-timer
      00000000c0210200-00000000c02102ff (prio 0, i/o): digic-timer
      00000000c0800000-00000000c0800017 (prio 0, i/o): digic-uart
      00000000f8000000-00000000ffffffff (prio 0, i/o): masked pflash [span of 4 MiB]
        00000000f8000000-00000000f83fffff (prio 0, romd): alias pflash [#0/32] @pflash 0000000000000000-00000000003fffff
        00000000f8400000-00000000f87fffff (prio 0, romd): alias pflash [#1/32] @pflash 0000000000000000-00000000003fffff
        00000000f8800000-00000000f8bfffff (prio 0, romd): alias pflash [#2/32] @pflash 0000000000000000-00000000003fffff
        ...
        00000000ff400000-00000000ff7fffff (prio 0, romd): alias pflash [#29/32] @pflash 0000000000000000-00000000003fffff
        00000000ff800000-00000000ffbfffff (prio 0, romd): alias pflash [#30/32] @pflash 0000000000000000-00000000003fffff
        00000000ffc00000-00000000ffffffff (prio 0, romd): alias pflash [#31/32] @pflash 0000000000000000-00000000003fffff

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/arm/digic_boards.c | 8 +++++---
 hw/arm/Kconfig        | 1 +
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/hw/arm/digic_boards.c b/hw/arm/digic_boards.c
index fc4a671b2e1..293402b1240 100644
--- a/hw/arm/digic_boards.c
+++ b/hw/arm/digic_boards.c
@@ -128,8 +128,7 @@ static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
                          FLASH_K8P3215UQB_SIZE / FLASH_K8P3215UQB_SECTOR_SIZE);
     qdev_prop_set_uint32(dev, "sector-length", FLASH_K8P3215UQB_SECTOR_SIZE);
     qdev_prop_set_uint8(dev, "width", 4); /* 32-bit */
-    qdev_prop_set_uint8(dev, "mappings",
-                        DIGIC4_ROM_MAX_SIZE / FLASH_K8P3215UQB_SIZE);
+    qdev_prop_set_uint8(dev, "mappings", 0);
     qdev_prop_set_uint8(dev, "big-endian", 0);
     qdev_prop_set_uint16(dev, "id0", 0x00ec);
     qdev_prop_set_uint16(dev, "id1", 0x007e);
@@ -140,7 +139,10 @@ static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
     qdev_prop_set_string(dev, "name", "pflash");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
-    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
+    memory_region_add_subregion_aliased(get_system_memory(),
+                            addr, DIGIC4_ROM_MAX_SIZE,
+                            sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0),
+                            FLASH_K8P3215UQB_SIZE);
 
     digic_load_rom(s, addr, FLASH_K8P3215UQB_SIZE, filename);
 }
diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index aa8553b3cd3..1a7b9724d6c 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -42,6 +42,7 @@ config DIGIC
     bool
     select PTIMER
     select PFLASH_CFI02
+    select ALIASED_REGION
 
 config EXYNOS4
     bool
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings()
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 05/10] hw/arm/digic: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-29  4:11   ` David Gibson
  2021-03-26  0:27 ` [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  11 siblings, 1 reply; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Peter Maydell, open list:Block layer core,
	Mark Cave-Ayland, Alistair Francis, Richard Henderson,
	Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias, Max Reitz,
	Philippe Mathieu-Daudé,
	David Gibson

All boards calling pflash_cfi02_register() use nb_mappings=1,
which does not do any mapping:

  $ git grep -wl pflash_cfi02_register hw/
  hw/arm/xilinx_zynq.c
  hw/block/pflash_cfi02.c
  hw/lm32/lm32_boards.c
  hw/ppc/ppc405_boards.c
  hw/sh4/r2d.c

We can remove this now unneeded code.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/block/pflash_cfi02.c | 35 ++---------------------------------
 1 file changed, 2 insertions(+), 33 deletions(-)

diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
index 02c514fb6e0..6f4b3e3c3fe 100644
--- a/hw/block/pflash_cfi02.c
+++ b/hw/block/pflash_cfi02.c
@@ -75,7 +75,6 @@ struct PFlashCFI02 {
     uint32_t nb_blocs[PFLASH_MAX_ERASE_REGIONS];
     uint32_t sector_len[PFLASH_MAX_ERASE_REGIONS];
     uint32_t chip_len;
-    uint8_t mappings;
     uint8_t width;
     uint8_t be;
     int wcycle; /* if 0, the flash is read normally */
@@ -92,13 +91,6 @@ struct PFlashCFI02 {
     uint16_t unlock_addr1;
     uint8_t cfi_table[0x4d];
     QEMUTimer timer;
-    /*
-     * The device replicates the flash memory across its memory space.  Emulate
-     * that by having a container (.mem) filled with an array of aliases
-     * (.mem_mappings) pointing to the flash memory (.orig_mem).
-     */
-    MemoryRegion mem;
-    MemoryRegion *mem_mappings;    /* array; one per mapping */
     MemoryRegion orig_mem;
     bool rom_mode;
     int read_counter; /* used for lazy switch-back to rom mode */
@@ -158,23 +150,6 @@ static inline void toggle_dq2(PFlashCFI02 *pfl)
     pfl->status ^= 0x04;
 }
 
-/*
- * Set up replicated mappings of the same region.
- */
-static void pflash_setup_mappings(PFlashCFI02 *pfl)
-{
-    unsigned i;
-    hwaddr size = memory_region_size(&pfl->orig_mem);
-
-    memory_region_init(&pfl->mem, OBJECT(pfl), "pflash", pfl->mappings * size);
-    pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings);
-    for (i = 0; i < pfl->mappings; ++i) {
-        memory_region_init_alias(&pfl->mem_mappings[i], OBJECT(pfl),
-                                 "pflash-alias", &pfl->orig_mem, 0, size);
-        memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]);
-    }
-}
-
 static void pflash_reset_state_machine(PFlashCFI02 *pfl)
 {
     trace_pflash_reset(pfl->name);
@@ -917,12 +892,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
     pfl->sector_erase_map = bitmap_new(pfl->total_sectors);
 
     pfl->rom_mode = true;
-    if (pfl->mappings > 1) {
-        pflash_setup_mappings(pfl);
-        sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
-    } else {
-        sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->orig_mem);
-    }
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->orig_mem);
 
     timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
     pfl->status = 0;
@@ -950,7 +920,6 @@ static Property pflash_cfi02_properties[] = {
     DEFINE_PROP_UINT32("num-blocks3", PFlashCFI02, nb_blocs[3], 0),
     DEFINE_PROP_UINT32("sector-length3", PFlashCFI02, sector_len[3], 0),
     DEFINE_PROP_UINT8("width", PFlashCFI02, width, 0),
-    DEFINE_PROP_UINT8("mappings", PFlashCFI02, mappings, 0),
     DEFINE_PROP_UINT8("big-endian", PFlashCFI02, be, 0),
     DEFINE_PROP_UINT16("id0", PFlashCFI02, ident0, 0),
     DEFINE_PROP_UINT16("id1", PFlashCFI02, ident1, 0),
@@ -1008,6 +977,7 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
 {
     DeviceState *dev = qdev_new(TYPE_PFLASH_CFI02);
 
+    assert(nb_mappings <= 1);
     if (blk) {
         qdev_prop_set_drive(dev, "drive", blk);
     }
@@ -1015,7 +985,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
     qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
     qdev_prop_set_uint32(dev, "sector-length", sector_len);
     qdev_prop_set_uint8(dev, "width", width);
-    qdev_prop_set_uint8(dev, "mappings", nb_mappings);
     qdev_prop_set_uint8(dev, "big-endian", !!be);
     qdev_prop_set_uint16(dev, "id0", id0);
     qdev_prop_set_uint16(dev, "id1", id1);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2021-03-26  0:27 ` [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings() Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-29  4:12   ` David Gibson
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 08/10] hw/misc/aliased_region: Simplify aliased I/O regions Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  11 siblings, 1 reply; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Peter Maydell, Magnus Damm, Jan Kiszka,
	open list:Block layer core, Yoshinori Sato, Mark Cave-Ayland,
	Alistair Francis, Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, Michael Walle, qemu-arm, qemu-ppc, Antony Pavlov,
	Edgar E. Iglesias, Greg Kurz, Max Reitz,
	Philippe Mathieu-Daudé,
	David Gibson

The previous commit removed the mapping code from TYPE_PFLASH_CFI02.
pflash_cfi02_register() doesn't use the 'nb_mappings' argument
anymore. Simply remove it to simplify.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/block/flash.h | 1 -
 hw/arm/digic_boards.c    | 1 -
 hw/arm/musicpal.c        | 1 -
 hw/arm/xilinx_zynq.c     | 2 +-
 hw/block/pflash_cfi02.c  | 3 +--
 hw/lm32/lm32_boards.c    | 4 ++--
 hw/ppc/ppc405_boards.c   | 6 +++---
 hw/sh4/r2d.c             | 2 +-
 8 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/include/hw/block/flash.h b/include/hw/block/flash.h
index 7dde0adcee7..0e5dd818a9d 100644
--- a/include/hw/block/flash.h
+++ b/include/hw/block/flash.h
@@ -36,7 +36,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
                                    hwaddr size,
                                    BlockBackend *blk,
                                    uint32_t sector_len,
-                                   int nb_mappings,
                                    int width,
                                    uint16_t id0, uint16_t id1,
                                    uint16_t id2, uint16_t id3,
diff --git a/hw/arm/digic_boards.c b/hw/arm/digic_boards.c
index 293402b1240..eb694c70d4c 100644
--- a/hw/arm/digic_boards.c
+++ b/hw/arm/digic_boards.c
@@ -128,7 +128,6 @@ static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
                          FLASH_K8P3215UQB_SIZE / FLASH_K8P3215UQB_SECTOR_SIZE);
     qdev_prop_set_uint32(dev, "sector-length", FLASH_K8P3215UQB_SECTOR_SIZE);
     qdev_prop_set_uint8(dev, "width", 4); /* 32-bit */
-    qdev_prop_set_uint8(dev, "mappings", 0);
     qdev_prop_set_uint8(dev, "big-endian", 0);
     qdev_prop_set_uint16(dev, "id0", 0x00ec);
     qdev_prop_set_uint16(dev, "id1", 0x007e);
diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
index 7d1f2f3fb3f..e882e11df36 100644
--- a/hw/arm/musicpal.c
+++ b/hw/arm/musicpal.c
@@ -1657,7 +1657,6 @@ static void musicpal_init(MachineState *machine)
         qdev_prop_set_uint32(dev, "num-blocks", flash_size / sector_size);
         qdev_prop_set_uint32(dev, "sector-length", sector_size);
         qdev_prop_set_uint8(dev, "width", 2); /* 16-bit */
-        qdev_prop_set_uint8(dev, "mappings", 0);
         qdev_prop_set_uint8(dev, "big-endian", 0);
         qdev_prop_set_uint16(dev, "id0", 0x00bf);
         qdev_prop_set_uint16(dev, "id1", 0x236d);
diff --git a/hw/arm/xilinx_zynq.c b/hw/arm/xilinx_zynq.c
index 8db6cfd47f5..d12b00e7648 100644
--- a/hw/arm/xilinx_zynq.c
+++ b/hw/arm/xilinx_zynq.c
@@ -220,7 +220,7 @@ static void zynq_init(MachineState *machine)
     pflash_cfi02_register(0xe2000000, "zynq.pflash", FLASH_SIZE,
                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
                           FLASH_SECTOR_SIZE, 1,
-                          1, 0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
+                          0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
                           0);
 
     /* Create the main clock source, and feed slcr with it */
diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
index 6f4b3e3c3fe..2b412402fac 100644
--- a/hw/block/pflash_cfi02.c
+++ b/hw/block/pflash_cfi02.c
@@ -968,7 +968,7 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
                                    hwaddr size,
                                    BlockBackend *blk,
                                    uint32_t sector_len,
-                                   int nb_mappings, int width,
+                                   int width,
                                    uint16_t id0, uint16_t id1,
                                    uint16_t id2, uint16_t id3,
                                    uint16_t unlock_addr0,
@@ -977,7 +977,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
 {
     DeviceState *dev = qdev_new(TYPE_PFLASH_CFI02);
 
-    assert(nb_mappings <= 1);
     if (blk) {
         qdev_prop_set_drive(dev, "drive", blk);
     }
diff --git a/hw/lm32/lm32_boards.c b/hw/lm32/lm32_boards.c
index b5d97dd53ed..96877ba7cfb 100644
--- a/hw/lm32/lm32_boards.c
+++ b/hw/lm32/lm32_boards.c
@@ -121,7 +121,7 @@ static void lm32_evr_init(MachineState *machine)
     pflash_cfi02_register(flash_base, "lm32_evr.flash", flash_size,
                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
                           flash_sector_size,
-                          1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
+                          2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
 
     /* create irq lines */
     env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
@@ -218,7 +218,7 @@ static void lm32_uclinux_init(MachineState *machine)
     pflash_cfi02_register(flash_base, "lm32_uclinux.flash", flash_size,
                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
                           flash_sector_size,
-                          1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
+                          2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
 
     /* create irq lines */
     env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, env, 0));
diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
index 8f77887fb18..2503e033497 100644
--- a/hw/ppc/ppc405_boards.c
+++ b/hw/ppc/ppc405_boards.c
@@ -198,7 +198,7 @@ static void ref405ep_init(MachineState *machine)
         pflash_cfi02_register((uint32_t)(-bios_size),
                               "ef405ep.bios", bios_size,
                               blk_by_legacy_dinfo(dinfo),
-                              64 * KiB, 1,
+                              64 * KiB,
                               2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                               1);
     } else
@@ -469,7 +469,7 @@ static void taihu_405ep_init(MachineState *machine)
         pflash_cfi02_register(0xFFE00000,
                               "taihu_405ep.bios", bios_size,
                               blk_by_legacy_dinfo(dinfo),
-                              64 * KiB, 1,
+                              64 * KiB,
                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                               1);
         fl_idx++;
@@ -502,7 +502,7 @@ static void taihu_405ep_init(MachineState *machine)
         bios_size = 32 * MiB;
         pflash_cfi02_register(0xfc000000, "taihu_405ep.flash", bios_size,
                               blk_by_legacy_dinfo(dinfo),
-                              64 * KiB, 1,
+                              64 * KiB,
                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                               1);
         fl_idx++;
diff --git a/hw/sh4/r2d.c b/hw/sh4/r2d.c
index 443820901d4..b7288dcba80 100644
--- a/hw/sh4/r2d.c
+++ b/hw/sh4/r2d.c
@@ -301,7 +301,7 @@ static void r2d_init(MachineState *machine)
     dinfo = drive_get(IF_PFLASH, 0, 0);
     pflash_cfi02_register(0x0, "r2d.flash", FLASH_SIZE,
                           dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
-                          64 * KiB, 1, 2, 0x0001, 0x227e, 0x2220, 0x2200,
+                          64 * KiB, 2, 0x0001, 0x227e, 0x2220, 0x2200,
                           0x555, 0x2aa, 0);
 
     /* NIC: rtl8139 on-board, and 2 slots. */
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [RFC PATCH-for-6.1 08/10] hw/misc/aliased_region: Simplify aliased I/O regions
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2021-03-26  0:27 ` [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [PATCH-for-6.1 09/10] hw/m68k/q800: Add MacIO container Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

Currently a small I/O region aliased on a wide container creates
many aliases. For example, a 8 bytes regions in a 4KiB container
creates 512 aliases! As I/O goes via the slow path, we can optimize
by using an I/O region which dispatch via an address space view
of the aliased region.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/misc/aliased_region.h |  6 +++-
 hw/misc/aliased_region.c         | 56 ++++++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/include/hw/misc/aliased_region.h b/include/hw/misc/aliased_region.h
index 0ce0d5d1cef..30b54cf06b7 100644
--- a/include/hw/misc/aliased_region.h
+++ b/include/hw/misc/aliased_region.h
@@ -1,7 +1,7 @@
 /*
  * Aliased memory regions
  *
- * Copyright (c) 2018  Philippe Mathieu-Daudé <f4bug@amsat.org>
+ * Copyright (c) 2018, 2020  Philippe Mathieu-Daudé <f4bug@amsat.org>
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -25,6 +25,10 @@ struct AliasedRegionState {
     uint64_t span_size;
     MemoryRegion *mr;
 
+    struct {
+        AddressSpace as;
+    } io;
+
     struct {
         size_t count;
         MemoryRegion *alias;
diff --git a/hw/misc/aliased_region.c b/hw/misc/aliased_region.c
index 8fcc63f2648..46b4412430a 100644
--- a/hw/misc/aliased_region.c
+++ b/hw/misc/aliased_region.c
@@ -1,7 +1,7 @@
 /*
  * Aliased memory regions
  *
- * Copyright (c) 2018  Philippe Mathieu-Daudé <f4bug@amsat.org>
+ * Copyright (c) 2018, 2020  Philippe Mathieu-Daudé <f4bug@amsat.org>
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -14,6 +14,50 @@
 #include "hw/misc/aliased_region.h"
 #include "hw/qdev-properties.h"
 
+static MemTxResult aliased_io_read(void *opaque, hwaddr offset,
+                                   uint64_t *data, unsigned size,
+                                   MemTxAttrs attrs)
+{
+    AliasedRegionState *s = ALIASED_REGION(opaque);
+
+    return address_space_read(&s->io.as, offset, attrs, data, size);
+}
+
+static MemTxResult aliased_io_write(void *opaque, hwaddr offset,
+                                    uint64_t data, unsigned size,
+                                    MemTxAttrs attrs)
+{
+    AliasedRegionState *s = ALIASED_REGION(opaque);
+
+    return address_space_write(&s->io.as, offset, attrs, &data, size);
+}
+
+static bool aliased_io_accepts(void *opaque, hwaddr offset, unsigned size,
+                               bool is_write, MemTxAttrs attrs)
+{
+    AliasedRegionState *s = ALIASED_REGION(opaque);
+
+    return address_space_access_valid(&s->io.as, offset, size, is_write, attrs);
+}
+
+static const MemoryRegionOps aliased_io_ops = {
+    .read_with_attrs = aliased_io_read,
+    .write_with_attrs = aliased_io_write,
+    .impl.min_access_size = 1,
+    .impl.max_access_size = 8,
+    .valid.min_access_size = 1,
+    .valid.max_access_size = 8,
+    .valid.accepts = aliased_io_accepts,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static void aliased_io_realize(AliasedRegionState *s, const char *mr_name)
+{
+    memory_region_init_io(&s->container, OBJECT(s), &aliased_io_ops, s,
+                          mr_name, s->region_size);
+    address_space_init(&s->io.as, s->mr, memory_region_name(s->mr));
+}
+
 static void aliased_mem_realize(AliasedRegionState *s, const char *mr_name)
 {
     uint64_t subregion_size;
@@ -63,7 +107,15 @@ static void aliased_mr_realize(DeviceState *dev, Error **errp)
     span = size_to_str(s->span_size);
     name = g_strdup_printf("masked %s [span of %s]",
                            memory_region_name(s->mr), span);
-    aliased_mem_realize(s, name);
+
+    if (memory_region_is_ram(s->mr)
+            || memory_region_is_ram_device(s->mr)
+            || memory_region_is_romd(s->mr)) {
+        aliased_mem_realize(s, name);
+    } else {
+        /* I/O or container */
+        aliased_io_realize(s, name);
+    }
     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->container);
 }
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH-for-6.1 09/10] hw/m68k/q800: Add MacIO container
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 08/10] hw/misc/aliased_region: Simplify aliased I/O regions Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 10/10] hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

Instead of having devices mapped onto some main memory range,
then having many aliases pointing to this memory range, add
a container, map the devices onto it, and map aliases of the
container onto the main memory.

We gain a better visibility of the devices on the mac-io bus.

- before:

  (qemu) info mtree
  address-space: memory
    0000000000000000-ffffffffffffffff (prio 0, i/o): system
      0000000000000000-0000000007ffffff (prio 0, ram): m68k_mac.ram
      0000000040800000-00000000408fffff (prio 0, rom): m68k_mac.rom
      0000000050000000-0000000050003fff (prio 0, i/o): mac-via
        0000000050000000-0000000050001fff (prio 0, i/o): via1
        0000000050002000-0000000050003fff (prio 0, i/o): via2
      0000000050008000-0000000050008fff (prio 0, rom): dp8393x-prom
      000000005000a000-000000005000a0ff (prio 0, i/o): dp8393x-regs
      000000005000c020-000000005000c027 (prio 0, i/o): escc
      0000000050010000-00000000500100ff (prio 0, i/o): esp-regs
      0000000050010100-0000000050010103 (prio 0, i/o): esp-pdma
      000000005001e000-000000005001ffff (prio 0, i/o): swim
      0000000050040000-000000005007ffff (prio 0, i/o): alias mac_m68k.io[1] @system 0000000050000000-000000005003ffff
      0000000050080000-00000000500bffff (prio 0, i/o): alias mac_m68k.io[2] @system 0000000050000000-000000005003ffff
      00000000500c0000-00000000500fffff (prio 0, i/o): alias mac_m68k.io[3] @system 0000000050000000-000000005003ffff
      ...
      0000000053f40000-0000000053f7ffff (prio 0, i/o): alias mac_m68k.io[253] @system 0000000050000000-000000005003ffff
      0000000053f80000-0000000053fbffff (prio 0, i/o): alias mac_m68k.io[254] @system 0000000050000000-000000005003ffff
      0000000053fc0000-0000000053ffffff (prio 0, i/o): alias mac_m68k.io[255] @system 0000000050000000-000000005003ffff
      0000000060000000-00000000efffffff (prio 0, i/o): nubus-super-slots
      00000000f0000000-00000000feffffff (prio 0, i/o): nubus-slots
        00000000f9000000-00000000f9ffffff (prio 0, i/o): nubus-slot-9
          00000000f9001000-00000000f9400fff (prio 0, ram): macfb-vram
          00000000f9800000-00000000f9800fff (prio 0, i/o): macfb-ctrl
          00000000f9ffffe8-00000000f9ffffeb (prio 0, i/o): nubus-slot-9-rom
          00000000f9ffffec-00000000f9ffffff (prio 0, i/o): nubus-slot-9-format-block

- after:

  address-space: memory
      0000000000000000-0000000007ffffff (prio 0, ram): m68k_mac.ram
      0000000040800000-00000000408fffff (prio 0, rom): m68k_mac.rom
      0000000050000000-000000005003ffff (prio 0, i/o): alias mac_m68k.io[0] @mac-io 0000000000000000-000000000003ffff
      0000000050040000-000000005007ffff (prio 0, i/o): alias mac_m68k.io[1] @mac-io 0000000000000000-000000000003ffff
      0000000050080000-00000000500bffff (prio 0, i/o): alias mac_m68k.io[2] @mac-io 0000000000000000-000000000003ffff
      ...
      0000000053f40000-0000000053f7ffff (prio 0, i/o): alias mac_m68k.io[253] @mac-io 0000000000000000-000000000003ffff
      0000000053f80000-0000000053fbffff (prio 0, i/o): alias mac_m68k.io[254] @mac-io 0000000000000000-000000000003ffff
      0000000053fc0000-0000000053ffffff (prio 0, i/o): alias mac_m68k.io[255] @mac-io 0000000000000000-000000000003ffff
      0000000060000000-00000000efffffff (prio 0, i/o): nubus-super-slots
      00000000f0000000-00000000feffffff (prio 0, i/o): nubus-slots
        00000000f9000000-00000000f9ffffff (prio 0, i/o): nubus-slot-9
          00000000f9001000-00000000f9400fff (prio 0, ram): macfb-vram
          00000000f9800000-00000000f9800fff (prio 0, i/o): macfb-ctrl
          00000000f9ffffe8-00000000f9ffffeb (prio 0, i/o): nubus-slot-9-rom
          00000000f9ffffec-00000000f9ffffff (prio 0, i/o): nubus-slot-9-format-block

  memory-region: mac-io
    0000000000000000-000000000003ffff (prio 0, i/o): mac-io
      0000000000000000-0000000000003fff (prio 0, i/o): mac-via
        0000000000000000-0000000000001fff (prio 0, i/o): via1
        0000000000002000-0000000000003fff (prio 0, i/o): via2
      0000000000008000-0000000000008fff (prio 0, rom): dp8393x-prom
      000000000000a000-000000000000a0ff (prio 0, i/o): dp8393x-regs
      000000000000c020-000000000000c027 (prio 0, i/o): escc
      0000000000010000-00000000000100ff (prio 0, i/o): esp-regs
      0000000000010100-0000000000010103 (prio 0, i/o): esp-pdma
      000000000001e000-000000000001ffff (prio 0, i/o): swim

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/m68k/q800.c | 54 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 21 deletions(-)

diff --git a/hw/m68k/q800.c b/hw/m68k/q800.c
index 4d2e866eec7..8f14e677077 100644
--- a/hw/m68k/q800.c
+++ b/hw/m68k/q800.c
@@ -61,14 +61,14 @@
 #define IO_SLICE              0x00040000
 #define IO_SIZE               0x04000000
 
-#define VIA_BASE              (IO_BASE + 0x00000)
-#define SONIC_PROM_BASE       (IO_BASE + 0x08000)
-#define SONIC_BASE            (IO_BASE + 0x0a000)
-#define SCC_BASE              (IO_BASE + 0x0c020)
-#define ESP_BASE              (IO_BASE + 0x10000)
-#define ESP_PDMA              (IO_BASE + 0x10100)
-#define ASC_BASE              (IO_BASE + 0x14000)
-#define SWIM_BASE             (IO_BASE + 0x1E000)
+#define VIA_OFFSET            (0x00000)
+#define SONIC_PROM_OFFSET     (0x08000)
+#define SONIC_IO_OFFSET       (0x0a000)
+#define SCC_OFFSET            (0x0c020)
+#define ESP_OFFSET            (0x10000)
+#define ESP_PDMA_OFFSET       (0x10100)
+#define ASC_OFFSET            (0x14000)
+#define SWIM_OFFSET           (0x1e000)
 
 #define NUBUS_SUPER_SLOT_BASE 0x60000000
 #define NUBUS_SLOT_BASE       0xf0000000
@@ -213,8 +213,9 @@ static void q800_init(MachineState *machine)
     ram_addr_t initrd_base;
     int32_t initrd_size;
     MemoryRegion *rom;
+    MemoryRegion *macio;
     MemoryRegion *io;
-    const int io_slice_nb = (IO_SIZE / IO_SLICE) - 1;
+    const int io_slice_nb = (IO_SIZE / IO_SLICE);
     int i;
     ram_addr_t ram_size = machine->ram_size;
     const char *kernel_filename = machine->kernel_filename;
@@ -249,18 +250,21 @@ static void q800_init(MachineState *machine)
     /* RAM */
     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
 
+    /* MacIO bus */
+    macio = g_new(MemoryRegion, 1);
+    memory_region_init(macio, NULL, "mac-io", 256 * KiB); /* FIXME or 128K? */
+
     /*
      * Memory from IO_BASE to IO_BASE + IO_SLICE is repeated
      * from IO_BASE + IO_SLICE to IO_BASE + IO_SIZE
      */
     io = g_new(MemoryRegion, io_slice_nb);
     for (i = 0; i < io_slice_nb; i++) {
-        char *name = g_strdup_printf("mac_m68k.io[%d]", i + 1);
+        char *name = g_strdup_printf("mac_m68k.io[%d]", i);
 
-        memory_region_init_alias(&io[i], NULL, name, get_system_memory(),
-                                 IO_BASE, IO_SLICE);
+        memory_region_init_alias(&io[i], NULL, name, macio, 0, IO_SLICE);
         memory_region_add_subregion(get_system_memory(),
-                                    IO_BASE + (i + 1) * IO_SLICE, &io[i]);
+                                    IO_BASE + i * IO_SLICE, &io[i]);
         g_free(name);
     }
 
@@ -278,7 +282,8 @@ static void q800_init(MachineState *machine)
     }
     sysbus = SYS_BUS_DEVICE(via_dev);
     sysbus_realize_and_unref(sysbus, &error_fatal);
-    sysbus_mmio_map(sysbus, 0, VIA_BASE);
+    memory_region_add_subregion(macio, VIA_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 0));
     qdev_connect_gpio_out_named(DEVICE(sysbus), "irq", 0,
                                 qdev_get_gpio_in(glue, 0));
     qdev_connect_gpio_out_named(DEVICE(sysbus), "irq", 1,
@@ -321,9 +326,11 @@ static void q800_init(MachineState *machine)
                              OBJECT(get_system_memory()), &error_abort);
     sysbus = SYS_BUS_DEVICE(dev);
     sysbus_realize_and_unref(sysbus, &error_fatal);
-    sysbus_mmio_map(sysbus, 0, SONIC_BASE);
-    sysbus_mmio_map(sysbus, 1, SONIC_PROM_BASE);
     sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(glue, 2));
+    memory_region_add_subregion(macio, SONIC_IO_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 0));
+    memory_region_add_subregion(macio, SONIC_PROM_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 1));
 
     /* SCC */
 
@@ -346,7 +353,8 @@ static void q800_init(MachineState *machine)
     sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(escc_orgate, 0));
     sysbus_connect_irq(sysbus, 1, qdev_get_gpio_in(escc_orgate, 1));
     qdev_connect_gpio_out(DEVICE(escc_orgate), 0, qdev_get_gpio_in(glue, 3));
-    sysbus_mmio_map(sysbus, 0, SCC_BASE);
+    memory_region_add_subregion(macio, SCC_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 0));
 
     /* SCSI */
 
@@ -367,8 +375,10 @@ static void q800_init(MachineState *machine)
     sysbus_connect_irq(sysbus, 1,
                        qdev_get_gpio_in_named(via_dev, "via2-irq",
                                               VIA2_IRQ_SCSI_DATA_BIT));
-    sysbus_mmio_map(sysbus, 0, ESP_BASE);
-    sysbus_mmio_map(sysbus, 1, ESP_PDMA);
+    memory_region_add_subregion(macio, ESP_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 0));
+    memory_region_add_subregion(macio, ESP_PDMA_OFFSET,
+                                sysbus_mmio_get_region(sysbus, 1));
 
     scsi_bus_legacy_handle_cmdline(&esp->bus);
 
@@ -376,7 +386,8 @@ static void q800_init(MachineState *machine)
 
     dev = qdev_new(TYPE_SWIM);
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
-    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, SWIM_BASE);
+    memory_region_add_subregion(macio, SWIM_OFFSET,
+                                sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
 
     /* NuBus */
 
@@ -423,7 +434,8 @@ static void q800_init(MachineState *machine)
                   (graphic_height << 16) | graphic_width);
         BOOTINFO1(cs->as, parameters_base, BI_MAC_VROW,
                   (graphic_width * graphic_depth + 7) / 8);
-        BOOTINFO1(cs->as, parameters_base, BI_MAC_SCCBASE, SCC_BASE);
+        BOOTINFO1(cs->as, parameters_base, BI_MAC_SCCBASE,
+                  IO_BASE + SCC_OFFSET);
 
         rom = g_malloc(sizeof(*rom));
         memory_region_init_ram_ptr(rom, NULL, "m68k_fake_mac.rom",
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [RFC PATCH-for-6.1 10/10] hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased()
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2021-03-26  0:27 ` [PATCH-for-6.1 09/10] hw/m68k/q800: Add MacIO container Philippe Mathieu-Daudé
@ 2021-03-26  0:27 ` Philippe Mathieu-Daudé
  2021-03-26 12:58 ` [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Richard Henderson
  2021-03-27 21:45 ` Mark Cave-Ayland
  11 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26  0:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Mark Cave-Ayland, Alistair Francis,
	Richard Henderson, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

Currently the memory and flatview tree is huge, not very practical:

  $ (echo info mtree -f;echo q) \
    | qemu-system-m68k -M q800 -S -monitor stdio \
    | wc -l
  2073

Use the memory_region_add_subregion_aliased() helper to manage the
MacIO aliases under the hood.

- before:

  (qemu) info mtree
  address-space: memory
      0000000000000000-0000000007ffffff (prio 0, ram): m68k_mac.ram
      0000000040800000-00000000408fffff (prio 0, rom): m68k_mac.rom
      0000000050000000-000000005003ffff (prio 0, i/o): alias mac_m68k.io[0] @mac-io 0000000000000000-000000000003ffff
      0000000050040000-000000005007ffff (prio 0, i/o): alias mac_m68k.io[1] @mac-io 0000000000000000-000000000003ffff
      0000000050080000-00000000500bffff (prio 0, i/o): alias mac_m68k.io[2] @mac-io 0000000000000000-000000000003ffff
      ...
      0000000053f40000-0000000053f7ffff (prio 0, i/o): alias mac_m68k.io[253] @mac-io 0000000000000000-000000000003ffff
      0000000053f80000-0000000053fbffff (prio 0, i/o): alias mac_m68k.io[254] @mac-io 0000000000000000-000000000003ffff
      0000000053fc0000-0000000053ffffff (prio 0, i/o): alias mac_m68k.io[255] @mac-io 0000000000000000-000000000003ffff
      0000000060000000-00000000efffffff (prio 0, i/o): nubus-super-slots
      00000000f0000000-00000000feffffff (prio 0, i/o): nubus-slots
        00000000f9000000-00000000f9ffffff (prio 0, i/o): nubus-slot-9
          00000000f9001000-00000000f9400fff (prio 0, ram): macfb-vram
          00000000f9800000-00000000f9800fff (prio 0, i/o): macfb-ctrl
          00000000f9ffffe8-00000000f9ffffeb (prio 0, i/o): nubus-slot-9-rom
          00000000f9ffffec-00000000f9ffffff (prio 0, i/o): nubus-slot-9-format-block

- after:

  (qemu) info mtree
  address-space: memory
    0000000000000000-ffffffffffffffff (prio 0, i/o): system
      0000000000000000-0000000007ffffff (prio 0, ram): m68k_mac.ram
      0000000040800000-00000000408fffff (prio 0, rom): m68k_mac.rom
      0000000050000000-0000000053ffffff (prio 0, i/o): masked mac-io [span of 256 KiB]
      0000000060000000-00000000efffffff (prio 0, i/o): nubus-super-slots
      00000000f0000000-00000000feffffff (prio 0, i/o): nubus-slots
        00000000f9000000-00000000f9ffffff (prio 0, i/o): nubus-slot-9
          00000000f9001000-00000000f9400fff (prio 0, ram): macfb-vram
          00000000f9800000-00000000f9800fff (prio 0, i/o): macfb-ctrl
          00000000f9ffffe8-00000000f9ffffeb (prio 0, i/o): nubus-slot-9-rom
          00000000f9ffffec-00000000f9ffffff (prio 0, i/o): nubus-slot-9-format-block

  (qemu) info mtree -f
  FlatView #0
   AS "memory", root: system
   AS "cpu-memory-0", root: system
   AS "dp8393x", root: system
   Root memory region: system
    0000000000000000-0000000007ffffff (prio 0, ram): m68k_mac.ram
    0000000040800000-00000000408fffff (prio 0, rom): m68k_mac.rom
    0000000050000000-0000000053ffffff (prio 0, i/o): masked mac-io [span of 256 KiB]
    0000000060000000-00000000efffffff (prio 0, i/o): nubus-super-slots
    00000000f0000000-00000000f9000fff (prio 0, i/o): nubus-slots
    00000000f9001000-00000000f9400fff (prio 0, ram): macfb-vram
    00000000f9401000-00000000f97fffff (prio 0, i/o): nubus-slots @0000000009401000
    00000000f9800000-00000000f9800fff (prio 0, i/o): macfb-ctrl
    00000000f9801000-00000000f9ffffe7 (prio 0, i/o): nubus-slots @0000000009801000
    00000000f9ffffe8-00000000f9ffffeb (prio 0, rom): nubus-slot-9-rom
    00000000f9ffffec-00000000f9ffffff (prio 0, i/o): nubus-slot-9-format-block
    00000000fa000000-00000000feffffff (prio 0, i/o): nubus-slots @000000000a000000

  FlatView #1
   AS "mac-io", root: mac-io
   Root memory region: mac-io
    0000000000000000-0000000000001fff (prio 0, i/o): via1
    0000000000002000-0000000000003fff (prio 0, i/o): via2
    0000000000008000-0000000000008fff (prio 0, rom): dp8393x-prom
    000000000000a000-000000000000a0ff (prio 0, i/o): dp8393x-regs
    000000000000c020-000000000000c027 (prio 0, i/o): escc
    0000000000010000-00000000000100ff (prio 0, i/o): esp-regs
    0000000000010100-0000000000010103 (prio 0, i/o): esp-pdma
    000000000001e000-000000000001ffff (prio 0, i/o): swim

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/m68k/q800.c  | 15 +++------------
 hw/m68k/Kconfig |  1 +
 2 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/hw/m68k/q800.c b/hw/m68k/q800.c
index 8f14e677077..4ca7e1af08e 100644
--- a/hw/m68k/q800.c
+++ b/hw/m68k/q800.c
@@ -40,6 +40,7 @@
 #include "standard-headers/asm-m68k/bootinfo.h"
 #include "standard-headers/asm-m68k/bootinfo-mac.h"
 #include "bootinfo.h"
+#include "hw/misc/aliased_region.h"
 #include "hw/misc/mac_via.h"
 #include "hw/input/adb.h"
 #include "hw/nubus/mac-nubus-bridge.h"
@@ -214,9 +215,6 @@ static void q800_init(MachineState *machine)
     int32_t initrd_size;
     MemoryRegion *rom;
     MemoryRegion *macio;
-    MemoryRegion *io;
-    const int io_slice_nb = (IO_SIZE / IO_SLICE);
-    int i;
     ram_addr_t ram_size = machine->ram_size;
     const char *kernel_filename = machine->kernel_filename;
     const char *initrd_filename = machine->initrd_filename;
@@ -258,15 +256,8 @@ static void q800_init(MachineState *machine)
      * Memory from IO_BASE to IO_BASE + IO_SLICE is repeated
      * from IO_BASE + IO_SLICE to IO_BASE + IO_SIZE
      */
-    io = g_new(MemoryRegion, io_slice_nb);
-    for (i = 0; i < io_slice_nb; i++) {
-        char *name = g_strdup_printf("mac_m68k.io[%d]", i);
-
-        memory_region_init_alias(&io[i], NULL, name, macio, 0, IO_SLICE);
-        memory_region_add_subregion(get_system_memory(),
-                                    IO_BASE + i * IO_SLICE, &io[i]);
-        g_free(name);
-    }
+    memory_region_add_subregion_aliased(get_system_memory(),
+                                        IO_BASE, IO_SIZE, macio, IO_SLICE);
 
     /* IRQ Glue */
     glue = qdev_new(TYPE_GLUE);
diff --git a/hw/m68k/Kconfig b/hw/m68k/Kconfig
index f839f8a0306..d2e588913b7 100644
--- a/hw/m68k/Kconfig
+++ b/hw/m68k/Kconfig
@@ -23,6 +23,7 @@ config Q800
     select ESP
     select DP8393X
     select OR_IRQ
+    select ALIASED_REGION
 
 config M68K_VIRT
     bool
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (9 preceding siblings ...)
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 10/10] hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
@ 2021-03-26 12:58 ` Richard Henderson
  2021-03-27 21:45 ` Mark Cave-Ayland
  11 siblings, 0 replies; 16+ messages in thread
From: Richard Henderson @ 2021-03-26 12:58 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Peter Maydell, Alistair Francis, Mark Cave-Ayland,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

On 3/25/21 6:27 PM, Philippe Mathieu-Daudé wrote:
> This series introduce the memory_region_add_subregion_aliased()
> helper which basically create a device which maps a subregion
> multiple times.

I must say, the example mtree changes are persuasive.
I'll look in more detail later.


r~


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased()
  2021-03-26  0:27 ` [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
@ 2021-03-26 22:49   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 16+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-03-26 22:49 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Alistair Francis,
	Mark Cave-Ayland, Laurent Vivier, qemu-arm, Jan Kiszka, qemu-ppc,
	David Gibson

On 3/26/21 1:27 AM, Philippe Mathieu-Daudé wrote:
> Instead of using a device specific feature for mapping the
> flash memory multiple times over a wider region, use the
> generic memory_region_add_subregion_aliased() helper.
> 
> There is no change in the memory layout.

- before:

  (qemu) info mtree
  00000000fe000000-00000000ffffffff (prio 0, i/o): pflash
    00000000fe000000-00000000fe7fffff (prio 0, romd): alias pflash-alias
@musicpal.flash 0000000000000000-00000000007fffff
    00000000fe800000-00000000feffffff (prio 0, romd): alias pflash-alias
@musicpal.flash 0000000000000000-00000000007fffff
    00000000ff000000-00000000ff7fffff (prio 0, romd): alias pflash-alias
@musicpal.flash 0000000000000000-00000000007fffff
    00000000ff800000-00000000ffffffff (prio 0, romd): alias pflash-alias
@musicpal.flash 0000000000000000-00000000007fffff

- after:

  00000000fe000000-00000000ffffffff (prio 0, i/o): masked musicpal.flash
[span of 8 MiB]
    00000000fe000000-00000000fe7fffff (prio 0, romd): alias
musicpal.flash [#0/4] @musicpal.flash 0000000000000000-00000000007fffff
    00000000fe800000-00000000feffffff (prio 0, romd): alias
musicpal.flash [#1/4] @musicpal.flash 0000000000000000-00000000007fffff
    00000000ff000000-00000000ff7fffff (prio 0, romd): alias
musicpal.flash [#2/4] @musicpal.flash 0000000000000000-00000000007fffff
    00000000ff800000-00000000ffffffff (prio 0, romd): alias
musicpal.flash [#3/4] @musicpal.flash 0000000000000000-00000000007fffff

Flatview is the same:

  (qemu) info mtree -f
  FlatView #0
   AS "memory", root: system
   AS "cpu-memory-0", root: system
   AS "emac-dma", root: system
   Root memory region: system
    00000000fe000000-00000000fe7fffff (prio 0, romd): musicpal.flash
    00000000fe800000-00000000feffffff (prio 0, romd): musicpal.flash
    00000000ff000000-00000000ff7fffff (prio 0, romd): musicpal.flash
    00000000ff800000-00000000ffffffff (prio 0, romd): musicpal.flash

> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  hw/arm/musicpal.c | 11 +++++++----
>  hw/arm/Kconfig    |  1 +
>  2 files changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
> index 8b58b66f263..7d1f2f3fb3f 100644
> --- a/hw/arm/musicpal.c
> +++ b/hw/arm/musicpal.c
> @@ -30,6 +30,7 @@
>  #include "hw/irq.h"
>  #include "hw/or-irq.h"
>  #include "hw/audio/wm8750.h"
> +#include "hw/misc/aliased_region.h"
>  #include "sysemu/block-backend.h"
>  #include "sysemu/runstate.h"
>  #include "sysemu/dma.h"
> @@ -1656,7 +1657,7 @@ static void musicpal_init(MachineState *machine)
>          qdev_prop_set_uint32(dev, "num-blocks", flash_size / sector_size);
>          qdev_prop_set_uint32(dev, "sector-length", sector_size);
>          qdev_prop_set_uint8(dev, "width", 2); /* 16-bit */
> -        qdev_prop_set_uint8(dev, "mappings", MP_FLASH_SIZE_MAX / flash_size);
> +        qdev_prop_set_uint8(dev, "mappings", 0);
>          qdev_prop_set_uint8(dev, "big-endian", 0);
>          qdev_prop_set_uint16(dev, "id0", 0x00bf);
>          qdev_prop_set_uint16(dev, "id1", 0x236d);
> @@ -1667,14 +1668,16 @@ static void musicpal_init(MachineState *machine)
>          qdev_prop_set_string(dev, "name", "musicpal.flash");
>          sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
>  
> -        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
> -                        0x100000000ULL - MP_FLASH_SIZE_MAX);
> -
>          /*
>           * The original U-Boot accesses the flash at 0xFE000000 instead of
>           * 0xFF800000 (if there is 8 MB flash). So remap flash access if the
>           * image is smaller than 32 MB.
>           */
> +        memory_region_add_subregion_aliased(get_system_memory(),
> +                                0x100000000ULL - MP_FLASH_SIZE_MAX,
> +                                MP_FLASH_SIZE_MAX,
> +                                sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0),
> +                                flash_size);
>      }
>      sysbus_create_simple(TYPE_MV88W8618_FLASHCFG, MP_FLASHCFG_BASE, NULL);
>  
> diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
> index 8c37cf00da7..aa8553b3cd3 100644
> --- a/hw/arm/Kconfig
> +++ b/hw/arm/Kconfig
> @@ -101,6 +101,7 @@ config MUSICPAL
>      select MARVELL_88W8618
>      select PTIMER
>      select PFLASH_CFI02
> +    select ALIASED_REGION
>      select SERIAL
>      select WM8750
>  
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper
  2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
                   ` (10 preceding siblings ...)
  2021-03-26 12:58 ` [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Richard Henderson
@ 2021-03-27 21:45 ` Mark Cave-Ayland
  11 siblings, 0 replies; 16+ messages in thread
From: Mark Cave-Ayland @ 2021-03-27 21:45 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Peter Maydell, Alistair Francis, Richard Henderson,
	Laurent Vivier, qemu-arm, qemu-ppc, Edgar E. Iglesias,
	David Gibson

On 26/03/2021 00:27, Philippe Mathieu-Daudé wrote:

> Hi,
> 
> This series introduce the memory_region_add_subregion_aliased()
> helper which basically create a device which maps a subregion
> multiple times.
> 
> Examples are easier, so having a subregion aliased every @span_size
> then mapped onto a container at an offset, you get something like:
> 
>            ^-----------^
>            |           |
>            |           |
>            | +-------+ |                 +---------+          <--+
>            |           |                 +---------+             |
>            |           |                 |         |             |
>            |           |   +-----------> | alias#3 |             |
>            |           |   |             |         |             |
>            |           |   |             +---------+             |
>            |           |   |             +---------+             |
>            |           |   |             |         |             |
>            |           |   |   +-------> | alias#2 |             |
>            |           |   |   |         |         |             |region
>            | container |   |   |         +---------+             | size
>            |           |   |   |         +---------+             |
>            |           |   |   |         |         |             |
>            |           |   |   |  +----> | alias#1 |             |
>            |           |   |   |  |      |         |             |
>            |           |   |   |  |      +---------+  <--+       |
>            |           | +-+---+--+--+   +---------+     |       |
>            |           | |           |   |         |     |span   |
>            |           | | subregion +-> | alias#0 |     |size   |
>     offset |           | |           |   |         |     |       |
>     +----> | +-------+ | +-----------+   +---------+  <--+    <--+
>     |      |           |
>     |      |           |
>     |      |           |
>     |      |           |
>     |      |           |
>     |      ^-----------^
> 
> I know it need more documentation and tests, but I prefer to send
> as draft RFC for early review before spending more time on it.
> 
> Based-on: <20210325120921.858993-1-f4bug@amsat.org>
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg795218.html
> 
> Philippe Mathieu-Daudé (10):
>    hw/misc: Add device to help managing aliased memory regions
>    hw/arm/musicpal: Open-code pflash_cfi02_register() call
>    hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased()
>    hw/arm/digic: Open-code pflash_cfi02_register() call
>    hw/arm/digic: Map flash using memory_region_add_subregion_aliased()
>    hw/block/pflash_cfi02: Remove pflash_setup_mappings()
>    hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype
>    hw/misc/aliased_region: Simplify aliased I/O regions
>    hw/m68k/q800: Add MacIO container
>    hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased()
> 
>   include/hw/block/flash.h         |   1 -
>   include/hw/misc/aliased_region.h |  91 +++++++++++++++
>   hw/arm/digic_boards.c            |  28 ++++-
>   hw/arm/musicpal.c                |  29 ++++-
>   hw/arm/xilinx_zynq.c             |   2 +-
>   hw/block/pflash_cfi02.c          |  36 +-----
>   hw/lm32/lm32_boards.c            |   4 +-
>   hw/m68k/q800.c                   |  61 +++++-----
>   hw/misc/aliased_region.c         | 185 +++++++++++++++++++++++++++++++
>   hw/ppc/ppc405_boards.c           |   6 +-
>   hw/sh4/r2d.c                     |   2 +-
>   MAINTAINERS                      |   6 +
>   hw/arm/Kconfig                   |   2 +
>   hw/m68k/Kconfig                  |   1 +
>   hw/misc/Kconfig                  |   3 +
>   hw/misc/meson.build              |   1 +
>   16 files changed, 375 insertions(+), 83 deletions(-)
>   create mode 100644 include/hw/misc/aliased_region.h
>   create mode 100644 hw/misc/aliased_region.c

Now this is interesting. Are there any limits to the number of aliased memory regions 
supported? In my q800 dev branch I have the following commit: 
https://github.com/mcayland/qemu/commit/272547abbca69906dab5d94af32c5117691a1050 
("q800: reimplement mac-io region aliasing using IO memory region") to implement 
similar functionality because adding just one more device into the aliased region is 
enough to cause QEMU to assert with "phys_section_add: Assertion `map->sections_nb < 
TARGET_PAGE_SIZE'
failed" on startup.


ATB,

Mark.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings()
  2021-03-26  0:27 ` [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings() Philippe Mathieu-Daudé
@ 2021-03-29  4:11   ` David Gibson
  0 siblings, 0 replies; 16+ messages in thread
From: David Gibson @ 2021-03-29  4:11 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Kevin Wolf, Peter Maydell, open list:Block layer core,
	Mark Cave-Ayland, Alistair Francis, Richard Henderson,
	qemu-devel, Laurent Vivier, qemu-arm, qemu-ppc,
	Edgar E. Iglesias, Max Reitz, Philippe Mathieu-Daudé

[-- Attachment #1: Type: text/plain, Size: 4634 bytes --]

On Fri, Mar 26, 2021 at 01:27:24AM +0100, Philippe Mathieu-Daudé wrote:
> All boards calling pflash_cfi02_register() use nb_mappings=1,
> which does not do any mapping:
> 
>   $ git grep -wl pflash_cfi02_register hw/
>   hw/arm/xilinx_zynq.c
>   hw/block/pflash_cfi02.c
>   hw/lm32/lm32_boards.c
>   hw/ppc/ppc405_boards.c
>   hw/sh4/r2d.c
> 
> We can remove this now unneeded code.
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  hw/block/pflash_cfi02.c | 35 ++---------------------------------
>  1 file changed, 2 insertions(+), 33 deletions(-)
> 
> diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
> index 02c514fb6e0..6f4b3e3c3fe 100644
> --- a/hw/block/pflash_cfi02.c
> +++ b/hw/block/pflash_cfi02.c
> @@ -75,7 +75,6 @@ struct PFlashCFI02 {
>      uint32_t nb_blocs[PFLASH_MAX_ERASE_REGIONS];
>      uint32_t sector_len[PFLASH_MAX_ERASE_REGIONS];
>      uint32_t chip_len;
> -    uint8_t mappings;
>      uint8_t width;
>      uint8_t be;
>      int wcycle; /* if 0, the flash is read normally */
> @@ -92,13 +91,6 @@ struct PFlashCFI02 {
>      uint16_t unlock_addr1;
>      uint8_t cfi_table[0x4d];
>      QEMUTimer timer;
> -    /*
> -     * The device replicates the flash memory across its memory space.  Emulate
> -     * that by having a container (.mem) filled with an array of aliases
> -     * (.mem_mappings) pointing to the flash memory (.orig_mem).
> -     */
> -    MemoryRegion mem;
> -    MemoryRegion *mem_mappings;    /* array; one per mapping */
>      MemoryRegion orig_mem;
>      bool rom_mode;
>      int read_counter; /* used for lazy switch-back to rom mode */
> @@ -158,23 +150,6 @@ static inline void toggle_dq2(PFlashCFI02 *pfl)
>      pfl->status ^= 0x04;
>  }
>  
> -/*
> - * Set up replicated mappings of the same region.
> - */
> -static void pflash_setup_mappings(PFlashCFI02 *pfl)
> -{
> -    unsigned i;
> -    hwaddr size = memory_region_size(&pfl->orig_mem);
> -
> -    memory_region_init(&pfl->mem, OBJECT(pfl), "pflash", pfl->mappings * size);
> -    pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings);
> -    for (i = 0; i < pfl->mappings; ++i) {
> -        memory_region_init_alias(&pfl->mem_mappings[i], OBJECT(pfl),
> -                                 "pflash-alias", &pfl->orig_mem, 0, size);
> -        memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]);
> -    }
> -}
> -
>  static void pflash_reset_state_machine(PFlashCFI02 *pfl)
>  {
>      trace_pflash_reset(pfl->name);
> @@ -917,12 +892,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
>      pfl->sector_erase_map = bitmap_new(pfl->total_sectors);
>  
>      pfl->rom_mode = true;
> -    if (pfl->mappings > 1) {
> -        pflash_setup_mappings(pfl);
> -        sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
> -    } else {
> -        sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->orig_mem);
> -    }
> +    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->orig_mem);
>  
>      timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
>      pfl->status = 0;
> @@ -950,7 +920,6 @@ static Property pflash_cfi02_properties[] = {
>      DEFINE_PROP_UINT32("num-blocks3", PFlashCFI02, nb_blocs[3], 0),
>      DEFINE_PROP_UINT32("sector-length3", PFlashCFI02, sector_len[3], 0),
>      DEFINE_PROP_UINT8("width", PFlashCFI02, width, 0),
> -    DEFINE_PROP_UINT8("mappings", PFlashCFI02, mappings, 0),
>      DEFINE_PROP_UINT8("big-endian", PFlashCFI02, be, 0),
>      DEFINE_PROP_UINT16("id0", PFlashCFI02, ident0, 0),
>      DEFINE_PROP_UINT16("id1", PFlashCFI02, ident1, 0),
> @@ -1008,6 +977,7 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
>  {
>      DeviceState *dev = qdev_new(TYPE_PFLASH_CFI02);
>  
> +    assert(nb_mappings <= 1);
>      if (blk) {
>          qdev_prop_set_drive(dev, "drive", blk);
>      }
> @@ -1015,7 +985,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
>      qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
>      qdev_prop_set_uint32(dev, "sector-length", sector_len);
>      qdev_prop_set_uint8(dev, "width", width);
> -    qdev_prop_set_uint8(dev, "mappings", nb_mappings);
>      qdev_prop_set_uint8(dev, "big-endian", !!be);
>      qdev_prop_set_uint16(dev, "id0", id0);
>      qdev_prop_set_uint16(dev, "id1", id1);

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype
  2021-03-26  0:27 ` [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype Philippe Mathieu-Daudé
@ 2021-03-29  4:12   ` David Gibson
  0 siblings, 0 replies; 16+ messages in thread
From: David Gibson @ 2021-03-29  4:12 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Kevin Wolf, Peter Maydell, Magnus Damm, Jan Kiszka,
	open list:Block layer core, Yoshinori Sato, Mark Cave-Ayland,
	Alistair Francis, Richard Henderson, qemu-devel, Laurent Vivier,
	Michael Walle, qemu-arm, qemu-ppc, Antony Pavlov,
	Edgar E. Iglesias, Max Reitz, Philippe Mathieu-Daudé,
	Greg Kurz

[-- Attachment #1: Type: text/plain, Size: 8073 bytes --]

On Fri, Mar 26, 2021 at 01:27:25AM +0100, Philippe Mathieu-Daudé wrote:
> The previous commit removed the mapping code from TYPE_PFLASH_CFI02.
> pflash_cfi02_register() doesn't use the 'nb_mappings' argument
> anymore. Simply remove it to simplify.
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Revieed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  include/hw/block/flash.h | 1 -
>  hw/arm/digic_boards.c    | 1 -
>  hw/arm/musicpal.c        | 1 -
>  hw/arm/xilinx_zynq.c     | 2 +-
>  hw/block/pflash_cfi02.c  | 3 +--
>  hw/lm32/lm32_boards.c    | 4 ++--
>  hw/ppc/ppc405_boards.c   | 6 +++---
>  hw/sh4/r2d.c             | 2 +-
>  8 files changed, 8 insertions(+), 12 deletions(-)
> 
> diff --git a/include/hw/block/flash.h b/include/hw/block/flash.h
> index 7dde0adcee7..0e5dd818a9d 100644
> --- a/include/hw/block/flash.h
> +++ b/include/hw/block/flash.h
> @@ -36,7 +36,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
>                                     hwaddr size,
>                                     BlockBackend *blk,
>                                     uint32_t sector_len,
> -                                   int nb_mappings,
>                                     int width,
>                                     uint16_t id0, uint16_t id1,
>                                     uint16_t id2, uint16_t id3,
> diff --git a/hw/arm/digic_boards.c b/hw/arm/digic_boards.c
> index 293402b1240..eb694c70d4c 100644
> --- a/hw/arm/digic_boards.c
> +++ b/hw/arm/digic_boards.c
> @@ -128,7 +128,6 @@ static void digic4_add_k8p3215uqb_rom(DigicState *s, hwaddr addr,
>                           FLASH_K8P3215UQB_SIZE / FLASH_K8P3215UQB_SECTOR_SIZE);
>      qdev_prop_set_uint32(dev, "sector-length", FLASH_K8P3215UQB_SECTOR_SIZE);
>      qdev_prop_set_uint8(dev, "width", 4); /* 32-bit */
> -    qdev_prop_set_uint8(dev, "mappings", 0);
>      qdev_prop_set_uint8(dev, "big-endian", 0);
>      qdev_prop_set_uint16(dev, "id0", 0x00ec);
>      qdev_prop_set_uint16(dev, "id1", 0x007e);
> diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
> index 7d1f2f3fb3f..e882e11df36 100644
> --- a/hw/arm/musicpal.c
> +++ b/hw/arm/musicpal.c
> @@ -1657,7 +1657,6 @@ static void musicpal_init(MachineState *machine)
>          qdev_prop_set_uint32(dev, "num-blocks", flash_size / sector_size);
>          qdev_prop_set_uint32(dev, "sector-length", sector_size);
>          qdev_prop_set_uint8(dev, "width", 2); /* 16-bit */
> -        qdev_prop_set_uint8(dev, "mappings", 0);
>          qdev_prop_set_uint8(dev, "big-endian", 0);
>          qdev_prop_set_uint16(dev, "id0", 0x00bf);
>          qdev_prop_set_uint16(dev, "id1", 0x236d);
> diff --git a/hw/arm/xilinx_zynq.c b/hw/arm/xilinx_zynq.c
> index 8db6cfd47f5..d12b00e7648 100644
> --- a/hw/arm/xilinx_zynq.c
> +++ b/hw/arm/xilinx_zynq.c
> @@ -220,7 +220,7 @@ static void zynq_init(MachineState *machine)
>      pflash_cfi02_register(0xe2000000, "zynq.pflash", FLASH_SIZE,
>                            dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
>                            FLASH_SECTOR_SIZE, 1,
> -                          1, 0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
> +                          0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
>                            0);
>  
>      /* Create the main clock source, and feed slcr with it */
> diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
> index 6f4b3e3c3fe..2b412402fac 100644
> --- a/hw/block/pflash_cfi02.c
> +++ b/hw/block/pflash_cfi02.c
> @@ -968,7 +968,7 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
>                                     hwaddr size,
>                                     BlockBackend *blk,
>                                     uint32_t sector_len,
> -                                   int nb_mappings, int width,
> +                                   int width,
>                                     uint16_t id0, uint16_t id1,
>                                     uint16_t id2, uint16_t id3,
>                                     uint16_t unlock_addr0,
> @@ -977,7 +977,6 @@ PFlashCFI02 *pflash_cfi02_register(hwaddr base,
>  {
>      DeviceState *dev = qdev_new(TYPE_PFLASH_CFI02);
>  
> -    assert(nb_mappings <= 1);
>      if (blk) {
>          qdev_prop_set_drive(dev, "drive", blk);
>      }
> diff --git a/hw/lm32/lm32_boards.c b/hw/lm32/lm32_boards.c
> index b5d97dd53ed..96877ba7cfb 100644
> --- a/hw/lm32/lm32_boards.c
> +++ b/hw/lm32/lm32_boards.c
> @@ -121,7 +121,7 @@ static void lm32_evr_init(MachineState *machine)
>      pflash_cfi02_register(flash_base, "lm32_evr.flash", flash_size,
>                            dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
>                            flash_sector_size,
> -                          1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
> +                          2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
>  
>      /* create irq lines */
>      env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
> @@ -218,7 +218,7 @@ static void lm32_uclinux_init(MachineState *machine)
>      pflash_cfi02_register(flash_base, "lm32_uclinux.flash", flash_size,
>                            dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
>                            flash_sector_size,
> -                          1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
> +                          2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
>  
>      /* create irq lines */
>      env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, env, 0));
> diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
> index 8f77887fb18..2503e033497 100644
> --- a/hw/ppc/ppc405_boards.c
> +++ b/hw/ppc/ppc405_boards.c
> @@ -198,7 +198,7 @@ static void ref405ep_init(MachineState *machine)
>          pflash_cfi02_register((uint32_t)(-bios_size),
>                                "ef405ep.bios", bios_size,
>                                blk_by_legacy_dinfo(dinfo),
> -                              64 * KiB, 1,
> +                              64 * KiB,
>                                2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
>                                1);
>      } else
> @@ -469,7 +469,7 @@ static void taihu_405ep_init(MachineState *machine)
>          pflash_cfi02_register(0xFFE00000,
>                                "taihu_405ep.bios", bios_size,
>                                blk_by_legacy_dinfo(dinfo),
> -                              64 * KiB, 1,
> +                              64 * KiB,
>                                4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
>                                1);
>          fl_idx++;
> @@ -502,7 +502,7 @@ static void taihu_405ep_init(MachineState *machine)
>          bios_size = 32 * MiB;
>          pflash_cfi02_register(0xfc000000, "taihu_405ep.flash", bios_size,
>                                blk_by_legacy_dinfo(dinfo),
> -                              64 * KiB, 1,
> +                              64 * KiB,
>                                4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
>                                1);
>          fl_idx++;
> diff --git a/hw/sh4/r2d.c b/hw/sh4/r2d.c
> index 443820901d4..b7288dcba80 100644
> --- a/hw/sh4/r2d.c
> +++ b/hw/sh4/r2d.c
> @@ -301,7 +301,7 @@ static void r2d_init(MachineState *machine)
>      dinfo = drive_get(IF_PFLASH, 0, 0);
>      pflash_cfi02_register(0x0, "r2d.flash", FLASH_SIZE,
>                            dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
> -                          64 * KiB, 1, 2, 0x0001, 0x227e, 0x2220, 0x2200,
> +                          64 * KiB, 2, 0x0001, 0x227e, 0x2220, 0x2200,
>                            0x555, 0x2aa, 0);
>  
>      /* NIC: rtl8139 on-board, and 2 slots. */

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-03-29  4:24 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-26  0:27 [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Philippe Mathieu-Daudé
2021-03-26  0:27 ` [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions Philippe Mathieu-Daudé
2021-03-26  0:27 ` [PATCH-for-6.1 02/10] hw/arm/musicpal: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
2021-03-26  0:27 ` [RFC PATCH-for-6.1 03/10] hw/arm/musicpal: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
2021-03-26 22:49   ` Philippe Mathieu-Daudé
2021-03-26  0:27 ` [PATCH-for-6.1 04/10] hw/arm/digic: Open-code pflash_cfi02_register() call Philippe Mathieu-Daudé
2021-03-26  0:27 ` [RFC PATCH-for-6.1 05/10] hw/arm/digic: Map flash using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
2021-03-26  0:27 ` [PATCH-for-6.1 06/10] hw/block/pflash_cfi02: Remove pflash_setup_mappings() Philippe Mathieu-Daudé
2021-03-29  4:11   ` David Gibson
2021-03-26  0:27 ` [PATCH-for-6.1 07/10] hw/block/pflash_cfi02: Simplify pflash_cfi02_register() prototype Philippe Mathieu-Daudé
2021-03-29  4:12   ` David Gibson
2021-03-26  0:27 ` [RFC PATCH-for-6.1 08/10] hw/misc/aliased_region: Simplify aliased I/O regions Philippe Mathieu-Daudé
2021-03-26  0:27 ` [PATCH-for-6.1 09/10] hw/m68k/q800: Add MacIO container Philippe Mathieu-Daudé
2021-03-26  0:27 ` [RFC PATCH-for-6.1 10/10] hw/m68k/q800: Map MacIO using memory_region_add_subregion_aliased() Philippe Mathieu-Daudé
2021-03-26 12:58 ` [RFC PATCH-for-6.1 00/10] hw/misc: Add memory_region_add_subregion_aliased() helper Richard Henderson
2021-03-27 21:45 ` Mark Cave-Ayland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).