qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/12] hw: Strengthen SysBus & QBus API
@ 2023-10-19  7:15 Philippe Mathieu-Daudé
  2023-10-19  7:15 ` [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
                   ` (13 more replies)
  0 siblings, 14 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

Hi,

This series ensure:

- qbus_new() and sysbus_init_mmio() are called *before*
  a device is realized,
- sysbus_mmio_map() is called *after* it is realized.

First we fix some abuse, then we enforce in qdev/sysbus
core code.

There is still a failure in PXA2xx:

  qemu-system-aarch64: sysbus_init_mmio(type:pxa2xx_pic) but object is realized

Apparently it cames from commit 3f6c925f37 ("Use i2c_slave_init() to
allocate the PXA (dummy) I2C slave"), which I presume was how to model
slave<->master transactions *before* I2C bus modelling.

Based-on: <20231018133059.85765-1-philmd@linaro.org>
          "hw/ppc: SysBus simplifications" [1]
Based-on: <20231018131220.84380-1-philmd@linaro.org>
          "hw/arm/pxa2xx: SysBus/QDev fixes" [2]

v1: https://lore.kernel.org/qemu-devel/20231018141151.87466-1-philmd@linaro.org/
[1] https://lore.kernel.org/qemu-devel/20231018133059.85765-1-philmd@linaro.org/
[2] https://lore.kernel.org/qemu-devel/20231018131220.84380-1-philmd@linaro.org/

Philippe Mathieu-Daudé (12):
  hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region
  hw/i386/intel_iommu: Do not use SysBus API to map local MMIO region
  hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init ->
    realize
  hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO
    region
  hw/pci-host/bonito: Do not use SysBus API to map local MMIO region
  hw/acpi: Realize ACPI_GED sysbus device before accessing it
  hw/arm/virt: Realize ARM_GICV2M sysbus device before accessing it
  hw/isa: Realize ISA BUS sysbus device before accessing it
  hw/s390x/css-bridge: Realize sysbus device before accessing it
  hw/qdev: Ensure parent device is not realized before adding bus
  hw/sysbus: Ensure device is not realized before adding MMIO region
  hw/sysbus: Ensure device is realized before mapping it

 hw/arm/virt.c                 |  5 ++---
 hw/core/bus.c                 |  7 +++++++
 hw/core/sysbus.c              | 13 +++++++++++++
 hw/i386/amd_iommu.c           |  5 ++---
 hw/i386/intel_iommu.c         |  5 ++---
 hw/i386/microvm.c             |  2 +-
 hw/isa/isa-bus.c              | 11 +++++++++--
 hw/loongarch/virt.c           |  2 +-
 hw/misc/allwinner-r40-dramc.c | 20 +++++++++-----------
 hw/pci-host/bonito.c          | 32 ++++++++++++++++----------------
 hw/s390x/css-bridge.c         |  7 +++----
 11 files changed, 65 insertions(+), 44 deletions(-)

-- 
2.41.0



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

* [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
@ 2023-10-19  7:15 ` Philippe Mathieu-Daudé
  2023-10-20  3:33   ` Zhao Liu
  2023-10-19  7:16 ` [PATCH v2 02/12] hw/i386/intel_iommu: " Philippe Mathieu-Daudé
                   ` (12 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

There is no point in exposing an internal MMIO region via
SysBus and directly mapping it in the very same device.

Just map it without using the SysBus API.

Transformation done using the following coccinelle script:

  @@
  expression sbdev;
  expression index;
  expression addr;
  expression subregion;
  @@
  -    sysbus_init_mmio(sbdev, subregion);
       ... when != sbdev
  -    sysbus_mmio_map(sbdev, index, addr);
  +    memory_region_add_subregion(get_system_memory(), addr, subregion);

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/i386/amd_iommu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index 8d0f2f99dd..7965415b47 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -1579,9 +1579,8 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     /* set up MMIO */
     memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
                           AMDVI_MMIO_SIZE);
-
-    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio);
-    sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, AMDVI_BASE_ADDR);
+    memory_region_add_subregion(get_system_memory(), AMDVI_BASE_ADDR,
+                                &s->mmio);
     pci_setup_iommu(bus, amdvi_host_dma_iommu, s);
     amdvi_init(s);
 }
-- 
2.41.0



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

* [PATCH v2 02/12] hw/i386/intel_iommu: Do not use SysBus API to map local MMIO region
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
  2023-10-19  7:15 ` [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-20  3:35   ` Zhao Liu
  2023-10-19  7:16 ` [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize Philippe Mathieu-Daudé
                   ` (11 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

There is no point in exposing an internal MMIO region via
SysBus and directly mapping it in the very same device.

Just map it without using the SysBus API.

Transformation done using the following coccinelle script:

  @@
  expression sbdev;
  expression index;
  expression addr;
  expression subregion;
  @@
  -    sysbus_init_mmio(sbdev, subregion);
       ... when != sbdev
  -    sysbus_mmio_map(sbdev, index, addr);
  +    memory_region_add_subregion(get_system_memory(), addr, subregion);

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/i386/intel_iommu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 2c832ab68b..e4f6cedcb1 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -4134,6 +4134,8 @@ static void vtd_realize(DeviceState *dev, Error **errp)
     qemu_mutex_init(&s->iommu_lock);
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
+    memory_region_add_subregion(get_system_memory(),
+                                Q35_HOST_BRIDGE_IOMMU_ADDR, &s->csrmem);
 
     /* Create the shared memory regions by all devices */
     memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar",
@@ -4148,15 +4150,12 @@ static void vtd_realize(DeviceState *dev, Error **errp)
     memory_region_add_subregion_overlap(&s->mr_nodmar,
                                         VTD_INTERRUPT_ADDR_FIRST,
                                         &s->mr_ir, 1);
-
-    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
     /* No corresponding destroy */
     s->iotlb = g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal,
                                      g_free, g_free);
     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
                                       g_free, g_free);
     vtd_init(s);
-    sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
     pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
     /* Pseudo address space under root PCI bus. */
     x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
-- 
2.41.0



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

* [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
  2023-10-19  7:15 ` [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 02/12] hw/i386/intel_iommu: " Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:34   ` Thomas Huth
  2023-10-19  7:16 ` [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
                   ` (10 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

In order to make the next commit trivial, move the sysbus_init_mmio()
call in allwinner_r40_dramc_init() just before the corresponding
sysbus_mmio_map_overlap() call in allwinner_r40_dramc_realize().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/misc/allwinner-r40-dramc.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/hw/misc/allwinner-r40-dramc.c b/hw/misc/allwinner-r40-dramc.c
index 6944f84455..2cc0254a55 100644
--- a/hw/misc/allwinner-r40-dramc.c
+++ b/hw/misc/allwinner-r40-dramc.c
@@ -414,6 +414,7 @@ static void allwinner_r40_dramc_reset(DeviceState *dev)
 static void allwinner_r40_dramc_realize(DeviceState *dev, Error **errp)
 {
     AwR40DramCtlState *s = AW_R40_DRAMC(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
 
     if (!get_match_ddr(s->ram_size)) {
         error_report("%s: ram-size %u MiB is not supported",
@@ -421,8 +422,12 @@ static void allwinner_r40_dramc_realize(DeviceState *dev, Error **errp)
         exit(1);
     }
 
-    /* detect_cells */
-    sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s), 3, s->ram_addr, 10);
+    /* R40 support max 2G memory but we only support up to 1G now. index 3 */
+    memory_region_init_io(&s->detect_cells, OBJECT(s),
+                          &allwinner_r40_detect_ops, s,
+                          "DRAMCELLS", 1 * GiB);
+    sysbus_init_mmio(sbd, &s->detect_cells);
+    sysbus_mmio_map_overlap(sbd, 3, s->ram_addr, 10);
     memory_region_set_enabled(&s->detect_cells, false);
 
     /*
@@ -458,12 +463,6 @@ static void allwinner_r40_dramc_init(Object *obj)
                           &allwinner_r40_dramphy_ops, s,
                           "DRAMPHY", 4 * KiB);
     sysbus_init_mmio(sbd, &s->dramphy_iomem);
-
-    /* R40 support max 2G memory but we only support up to 1G now. index 3 */
-    memory_region_init_io(&s->detect_cells, OBJECT(s),
-                          &allwinner_r40_detect_ops, s,
-                          "DRAMCELLS", 1 * GiB);
-    sysbus_init_mmio(sbd, &s->detect_cells);
 }
 
 static Property allwinner_r40_dramc_properties[] = {
-- 
2.41.0



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

* [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:44   ` Thomas Huth
  2023-10-19  7:16 ` [PATCH v2 05/12] hw/pci-host/bonito: " Philippe Mathieu-Daudé
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

There is no point in exposing an internal MMIO region via
SysBus and directly mapping it in the very same device.

Just map it without using the SysBus API.

Transformation done using the following coccinelle script:

  @@
  expression sbdev;
  expression index;
  expression addr;
  expression subregion;
  @@
  -    sysbus_init_mmio(sbdev, subregion);
       ... when != sbdev
  -    sysbus_mmio_map(sbdev, index, addr);
  +    memory_region_add_subregion(get_system_memory(),
  +                                addr, subregion);

  @@
  expression priority;
  @@
  -    sysbus_init_mmio(sbdev, subregion);
       ... when != sbdev
  -    sysbus_mmio_map_overlap(sbdev, index, addr, priority);
  +    memory_region_add_subregion_overlap(get_system_memory(),
  +                                        addr,
  +                                        subregion, priority);

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/misc/allwinner-r40-dramc.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/hw/misc/allwinner-r40-dramc.c b/hw/misc/allwinner-r40-dramc.c
index 2cc0254a55..3d81ddb2e1 100644
--- a/hw/misc/allwinner-r40-dramc.c
+++ b/hw/misc/allwinner-r40-dramc.c
@@ -414,7 +414,6 @@ static void allwinner_r40_dramc_reset(DeviceState *dev)
 static void allwinner_r40_dramc_realize(DeviceState *dev, Error **errp)
 {
     AwR40DramCtlState *s = AW_R40_DRAMC(dev);
-    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
 
     if (!get_match_ddr(s->ram_size)) {
         error_report("%s: ram-size %u MiB is not supported",
@@ -422,23 +421,23 @@ static void allwinner_r40_dramc_realize(DeviceState *dev, Error **errp)
         exit(1);
     }
 
-    /* R40 support max 2G memory but we only support up to 1G now. index 3 */
+    /* R40 support max 2G memory but we only support up to 1G now. */
     memory_region_init_io(&s->detect_cells, OBJECT(s),
                           &allwinner_r40_detect_ops, s,
                           "DRAMCELLS", 1 * GiB);
-    sysbus_init_mmio(sbd, &s->detect_cells);
-    sysbus_mmio_map_overlap(sbd, 3, s->ram_addr, 10);
+    memory_region_add_subregion_overlap(get_system_memory(), s->ram_addr,
+                                        &s->detect_cells, 10);
     memory_region_set_enabled(&s->detect_cells, false);
 
     /*
      * We only support DRAM size up to 1G now, so prepare a high memory page
-     * after 1G for dualrank detect. index = 4
+     * after 1G for dualrank detect.
      */
     memory_region_init_io(&s->dram_high, OBJECT(s),
                             &allwinner_r40_dualrank_detect_ops, s,
                             "DRAMHIGH", KiB);
-    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->dram_high);
-    sysbus_mmio_map(SYS_BUS_DEVICE(s), 4, s->ram_addr + GiB);
+    memory_region_add_subregion(get_system_memory(), s->ram_addr + GiB,
+                                &s->dram_high);
 }
 
 static void allwinner_r40_dramc_init(Object *obj)
-- 
2.41.0



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

* [PATCH v2 05/12] hw/pci-host/bonito: Do not use SysBus API to map local MMIO region
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:26   ` Thomas Huth
  2023-10-19  7:16 ` [PATCH v2 06/12] hw/acpi: Realize ACPI_GED sysbus device before accessing it Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

There is no point in exposing an internal MMIO region via
SysBus and directly mapping it in the very same device.

Just map it without using the SysBus API.

Transformation done using the following coccinelle script:

  @@
  expression sbdev;
  expression index;
  expression addr;
  expression subregion;
  @@
  -    sysbus_init_mmio(sbdev, subregion);
       ... when != sbdev
  -    sysbus_mmio_map(sbdev, index, addr);
  +    memory_region_add_subregion(get_system_memory(), addr, subregion);

and manually adding the local 'host_mem' variable to
avoid multiple calls to get_system_memory().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/pci-host/bonito.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/hw/pci-host/bonito.c b/hw/pci-host/bonito.c
index ee6cb85e97..96bd028671 100644
--- a/hw/pci-host/bonito.c
+++ b/hw/pci-host/bonito.c
@@ -654,10 +654,10 @@ static void bonito_host_realize(DeviceState *dev, Error **errp)
 static void bonito_pci_realize(PCIDevice *dev, Error **errp)
 {
     PCIBonitoState *s = PCI_BONITO(dev);
-    SysBusDevice *sysbus = SYS_BUS_DEVICE(s->pcihost);
     PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
     BonitoState *bs = s->pcihost;
     MemoryRegion *pcimem_alias = g_new(MemoryRegion, 1);
+    MemoryRegion *host_mem = get_system_memory();
 
     /*
      * Bonito North Bridge, built on FPGA,
@@ -668,48 +668,48 @@ static void bonito_pci_realize(PCIDevice *dev, Error **errp)
     /* set the north bridge register mapping */
     memory_region_init_io(&s->iomem, OBJECT(s), &bonito_ops, s,
                           "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
-    sysbus_init_mmio(sysbus, &s->iomem);
-    sysbus_mmio_map(sysbus, 0, BONITO_INTERNAL_REG_BASE);
+    memory_region_add_subregion(host_mem, BONITO_INTERNAL_REG_BASE,
+                                &s->iomem);
 
     /* set the north bridge pci configure  mapping */
     memory_region_init_io(&phb->conf_mem, OBJECT(s), &bonito_pciconf_ops, s,
                           "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
-    sysbus_init_mmio(sysbus, &phb->conf_mem);
-    sysbus_mmio_map(sysbus, 1, BONITO_PCICONFIG_BASE);
+    memory_region_add_subregion(host_mem, BONITO_PCICONFIG_BASE,
+                                &phb->conf_mem);
 
     /* set the south bridge pci configure  mapping */
     memory_region_init_io(&phb->data_mem, OBJECT(s), &bonito_spciconf_ops, s,
                           "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
-    sysbus_init_mmio(sysbus, &phb->data_mem);
-    sysbus_mmio_map(sysbus, 2, BONITO_SPCICONFIG_BASE);
+    memory_region_add_subregion(host_mem, BONITO_SPCICONFIG_BASE,
+                                &phb->data_mem);
 
     create_unimplemented_device("bonito", BONITO_REG_BASE, BONITO_REG_SIZE);
 
     memory_region_init_io(&s->iomem_ldma, OBJECT(s), &bonito_ldma_ops, s,
                           "ldma", 0x100);
-    sysbus_init_mmio(sysbus, &s->iomem_ldma);
-    sysbus_mmio_map(sysbus, 3, 0x1fe00200);
+    memory_region_add_subregion(host_mem, 0x1fe00200,
+                                &s->iomem_ldma);
 
     /* PCI copier */
     memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
                           "cop", 0x100);
-    sysbus_init_mmio(sysbus, &s->iomem_cop);
-    sysbus_mmio_map(sysbus, 4, 0x1fe00300);
+    memory_region_add_subregion(host_mem, 0x1fe00300,
+                                &s->iomem_cop);
 
     create_unimplemented_device("ROMCS", BONITO_FLASH_BASE, 60 * MiB);
 
     /* Map PCI IO Space  0x1fd0 0000 - 0x1fd1 0000 */
     memory_region_init_alias(&s->bonito_pciio, OBJECT(s), "isa_mmio",
                              get_system_io(), 0, BONITO_PCIIO_SIZE);
-    sysbus_init_mmio(sysbus, &s->bonito_pciio);
-    sysbus_mmio_map(sysbus, 5, BONITO_PCIIO_BASE);
+    memory_region_add_subregion(host_mem, BONITO_PCIIO_BASE,
+                                &s->bonito_pciio);
 
     /* add pci local io mapping */
 
     memory_region_init_alias(&s->bonito_localio, OBJECT(s), "IOCS[0]",
                              get_system_io(), 0, 256 * KiB);
-    sysbus_init_mmio(sysbus, &s->bonito_localio);
-    sysbus_mmio_map(sysbus, 6, BONITO_DEV_BASE);
+    memory_region_add_subregion(host_mem, BONITO_DEV_BASE,
+                                &s->bonito_localio);
     create_unimplemented_device("IOCS[1]", BONITO_DEV_BASE + 1 * 256 * KiB,
                                 256 * KiB);
     create_unimplemented_device("IOCS[2]", BONITO_DEV_BASE + 2 * 256 * KiB,
@@ -719,7 +719,7 @@ static void bonito_pci_realize(PCIDevice *dev, Error **errp)
 
     memory_region_init_alias(pcimem_alias, NULL, "pci.mem.alias",
                              &bs->pci_mem, 0, BONITO_PCIHI_SIZE);
-    memory_region_add_subregion(get_system_memory(),
+    memory_region_add_subregion(host_mem,
                                 BONITO_PCIHI_BASE, pcimem_alias);
     create_unimplemented_device("PCI_2",
                                 (hwaddr)BONITO_PCIHI_BASE + BONITO_PCIHI_SIZE,
-- 
2.41.0



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

* [PATCH v2 06/12] hw/acpi: Realize ACPI_GED sysbus device before accessing it
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 05/12] hw/pci-host/bonito: " Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 07/12] hw/arm/virt: Realize ARM_GICV2M " Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

sysbus_mmio_map() should not be called on unrealized device.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/arm/virt.c       | 3 +--
 hw/i386/microvm.c   | 2 +-
 hw/loongarch/virt.c | 2 +-
 3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 15e74249f9..02c7a7ff3c 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -647,13 +647,12 @@ static inline DeviceState *create_acpi_ged(VirtMachineState *vms)
 
     dev = qdev_new(TYPE_ACPI_GED);
     qdev_prop_set_uint32(dev, "ged-event", event);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base);
     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(vms->gic, irq));
 
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
-
     return dev;
 }
 
diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
index b9c93039e2..ca55aecc3b 100644
--- a/hw/i386/microvm.c
+++ b/hw/i386/microvm.c
@@ -206,12 +206,12 @@ static void microvm_devices_init(MicrovmMachineState *mms)
     if (x86_machine_is_acpi_enabled(x86ms)) {
         DeviceState *dev = qdev_new(TYPE_ACPI_GED);
         qdev_prop_set_uint32(dev, "ged-event", ACPI_GED_PWR_DOWN_EVT);
+        sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, GED_MMIO_BASE);
         /* sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, GED_MMIO_BASE_MEMHP); */
         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, GED_MMIO_BASE_REGS);
         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
                            x86ms->gsi[GED_MMIO_IRQ]);
-        sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
         x86ms->acpi_dev = HOTPLUG_HANDLER(dev);
     }
 
diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index 2952fe452e..4b7dc67a2d 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -412,6 +412,7 @@ static DeviceState *create_acpi_ged(DeviceState *pch_pic, LoongArchMachineState
     }
     dev = qdev_new(TYPE_ACPI_GED);
     qdev_prop_set_uint32(dev, "ged-event", event);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
     /* ged event */
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, VIRT_GED_EVT_ADDR);
@@ -422,7 +423,6 @@ static DeviceState *create_acpi_ged(DeviceState *pch_pic, LoongArchMachineState
 
     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
                        qdev_get_gpio_in(pch_pic, VIRT_SCI_IRQ - VIRT_GSI_BASE));
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     return dev;
 }
 
-- 
2.41.0



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

* [PATCH v2 07/12] hw/arm/virt: Realize ARM_GICV2M sysbus device before accessing it
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 06/12] hw/acpi: Realize ACPI_GED sysbus device before accessing it Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 08/12] hw/isa: Realize ISA BUS " Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

sysbus_mmio_map() should not be called on unrealized device.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 hw/arm/virt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 02c7a7ff3c..5b08a98f07 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -690,10 +690,10 @@ static void create_v2m(VirtMachineState *vms)
     DeviceState *dev;
 
     dev = qdev_new("arm-gicv2m");
-    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
     qdev_prop_set_uint32(dev, "base-spi", irq);
     qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
 
     for (i = 0; i < NUM_GICV2M_SPIS; i++) {
         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
-- 
2.41.0



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

* [PATCH v2 08/12] hw/isa: Realize ISA BUS sysbus device before accessing it
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 07/12] hw/arm/virt: Realize ARM_GICV2M " Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 09/12] hw/s390x/css-bridge: Realize " Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

qbus_new() should not be called on unrealized device.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/isa/isa-bus.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/hw/isa/isa-bus.c b/hw/isa/isa-bus.c
index a289eccfb1..f1e0f14007 100644
--- a/hw/isa/isa-bus.c
+++ b/hw/isa/isa-bus.c
@@ -52,18 +52,25 @@ static const TypeInfo isa_bus_info = {
 ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space,
                     MemoryRegion *address_space_io, Error **errp)
 {
+    DeviceState *bridge = NULL;
+
     if (isabus) {
         error_setg(errp, "Can't create a second ISA bus");
         return NULL;
     }
     if (!dev) {
-        dev = qdev_new("isabus-bridge");
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+        bridge = qdev_new("isabus-bridge");
+        dev = bridge;
     }
 
     isabus = ISA_BUS(qbus_new(TYPE_ISA_BUS, dev, NULL));
     isabus->address_space = address_space;
     isabus->address_space_io = address_space_io;
+
+    if (bridge) {
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(bridge), &error_fatal);
+    }
+
     return isabus;
 }
 
-- 
2.41.0



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

* [PATCH v2 09/12] hw/s390x/css-bridge: Realize sysbus device before accessing it
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 08/12] hw/isa: Realize ISA BUS " Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:46   ` Thomas Huth
  2023-10-20 14:21   ` Eric Farman
  2023-10-19  7:16 ` [PATCH v2 10/12] hw/qdev: Ensure parent device is not realized before adding bus Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  13 siblings, 2 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

qbus_new() should not be called on unrealized device.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/s390x/css-bridge.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/hw/s390x/css-bridge.c b/hw/s390x/css-bridge.c
index 4017081d49..15d26efc95 100644
--- a/hw/s390x/css-bridge.c
+++ b/hw/s390x/css-bridge.c
@@ -95,7 +95,6 @@ static const TypeInfo virtual_css_bus_info = {
 
 VirtualCssBus *virtual_css_bus_init(void)
 {
-    VirtualCssBus *cbus;
     BusState *bus;
     DeviceState *dev;
 
@@ -103,19 +102,19 @@ VirtualCssBus *virtual_css_bus_init(void)
     dev = qdev_new(TYPE_VIRTUAL_CSS_BRIDGE);
     object_property_add_child(qdev_get_machine(), TYPE_VIRTUAL_CSS_BRIDGE,
                               OBJECT(dev));
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
     /* Create bus on bridge device */
     bus = qbus_new(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
-    cbus = VIRTUAL_CSS_BUS(bus);
 
     /* Enable hotplugging */
     qbus_set_hotplug_handler(bus, OBJECT(dev));
 
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+
     css_register_io_adapters(CSS_IO_ADAPTER_VIRTIO, true, false,
                              0, &error_abort);
 
-    return cbus;
+    return VIRTUAL_CSS_BUS(bus);
  }
 
 /***************** Virtual-css Bus Bridge Device ********************/
-- 
2.41.0



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

* [PATCH v2 10/12] hw/qdev: Ensure parent device is not realized before adding bus
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 09/12] hw/s390x/css-bridge: Realize " Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 11/12] hw/sysbus: Ensure device is not realized before adding MMIO region Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

qbus_new() should not be called on realized device.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 hw/core/bus.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/hw/core/bus.c b/hw/core/bus.c
index c7831b5293..c92d07667b 100644
--- a/hw/core/bus.c
+++ b/hw/core/bus.c
@@ -21,6 +21,7 @@
 #include "hw/qdev-properties.h"
 #include "qemu/ctype.h"
 #include "qemu/module.h"
+#include "qemu/error-report.h"
 #include "qapi/error.h"
 
 void qbus_set_hotplug_handler(BusState *bus, Object *handler)
@@ -163,6 +164,12 @@ BusState *qbus_new(const char *typename, DeviceState *parent, const char *name)
 {
     BusState *bus;
 
+    if (parent->realized) {
+        error_report("qbus_new(type:%s parent:%s, name:%s) but parent realized",
+                     typename, object_get_typename(OBJECT(parent)), name);
+        abort();
+    }
+
     bus = BUS(object_new(typename));
     qbus_init_internal(bus, parent, name);
 
-- 
2.41.0



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

* [PATCH v2 11/12] hw/sysbus: Ensure device is not realized before adding MMIO region
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (9 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 10/12] hw/qdev: Ensure parent device is not realized before adding bus Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:16 ` [PATCH v2 12/12] hw/sysbus: Ensure device is realized before mapping it Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

sysbus_init_mmio() should not be called on realized device.

Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/core/sysbus.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 35f902b582..8f53cb926b 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
+#include "qemu/error-report.h"
 #include "hw/sysbus.h"
 #include "monitor/monitor.h"
 #include "exec/address-spaces.h"
@@ -192,6 +193,11 @@ void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
     int n;
 
     assert(dev->num_mmio < QDEV_MAX_MMIO);
+    if (DEVICE(dev)->realized) {
+        error_report("sysbus_init_mmio(type:%s) but object is realized",
+                     object_get_typename(OBJECT(dev)));
+        abort();
+    }
     n = dev->num_mmio++;
     dev->mmio[n].addr = -1;
     dev->mmio[n].memory = memory;
-- 
2.41.0



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

* [PATCH v2 12/12] hw/sysbus: Ensure device is realized before mapping it
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (10 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 11/12] hw/sysbus: Ensure device is not realized before adding MMIO region Philippe Mathieu-Daudé
@ 2023-10-19  7:16 ` Philippe Mathieu-Daudé
  2023-10-19  7:32 ` [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Thomas Huth
  2023-10-19 21:40 ` Philippe Mathieu-Daudé
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:16 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, Philippe Mathieu-Daudé, qemu-s390x,
	Strahinja Jankovic, Richard Henderson, Markus Armbruster,
	Song Gao, Eduardo Habkost, Peter Xu, Sergio Lopez,
	Christian Borntraeger, David Hildenbrand, Peter Maydell,
	Michael S. Tsirkin, Jason Wang

sysbus_mmio_map() should not be called on unrealized device.

Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/core/sysbus.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 8f53cb926b..a46828a808 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -133,6 +133,13 @@ static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
 {
     assert(n >= 0 && n < dev->num_mmio);
 
+    if (!DEVICE(dev)->realized) {
+        error_report("sysbus_mmio_map(type:%s, index:%d, addr:0x%"HWADDR_PRIx","
+                     " prio:%d) but object is not realized",
+                     object_get_typename(OBJECT(dev)), n, addr, priority);
+        abort();
+    }
+
     if (dev->mmio[n].addr == addr) {
         /* ??? region already mapped here.  */
         return;
-- 
2.41.0



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

* Re: [PATCH v2 05/12] hw/pci-host/bonito: Do not use SysBus API to map local MMIO region
  2023-10-19  7:16 ` [PATCH v2 05/12] hw/pci-host/bonito: " Philippe Mathieu-Daudé
@ 2023-10-19  7:26   ` Thomas Huth
  2023-10-19  7:38     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 24+ messages in thread
From: Thomas Huth @ 2023-10-19  7:26 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/2023 09.16, Philippe Mathieu-Daudé wrote:
> There is no point in exposing an internal MMIO region via
> SysBus and directly mapping it in the very same device.
> 
> Just map it without using the SysBus API.
> 
> Transformation done using the following coccinelle script:
> 
>    @@
>    expression sbdev;
>    expression index;
>    expression addr;
>    expression subregion;
>    @@
>    -    sysbus_init_mmio(sbdev, subregion);
>         ... when != sbdev
>    -    sysbus_mmio_map(sbdev, index, addr);
>    +    memory_region_add_subregion(get_system_memory(), addr, subregion);
> 
> and manually adding the local 'host_mem' variable to
> avoid multiple calls to get_system_memory().

Thanks for updating it!

> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/pci-host/bonito.c | 32 ++++++++++++++++----------------
>   1 file changed, 16 insertions(+), 16 deletions(-)
> 
> diff --git a/hw/pci-host/bonito.c b/hw/pci-host/bonito.c
> index ee6cb85e97..96bd028671 100644
> --- a/hw/pci-host/bonito.c
> +++ b/hw/pci-host/bonito.c
> @@ -654,10 +654,10 @@ static void bonito_host_realize(DeviceState *dev, Error **errp)
>   static void bonito_pci_realize(PCIDevice *dev, Error **errp)
>   {
>       PCIBonitoState *s = PCI_BONITO(dev);
> -    SysBusDevice *sysbus = SYS_BUS_DEVICE(s->pcihost);
>       PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
>       BonitoState *bs = s->pcihost;
>       MemoryRegion *pcimem_alias = g_new(MemoryRegion, 1);
> +    MemoryRegion *host_mem = get_system_memory();
>   
>       /*
>        * Bonito North Bridge, built on FPGA,
> @@ -668,48 +668,48 @@ static void bonito_pci_realize(PCIDevice *dev, Error **errp)
>       /* set the north bridge register mapping */
>       memory_region_init_io(&s->iomem, OBJECT(s), &bonito_ops, s,
>                             "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
> -    sysbus_init_mmio(sysbus, &s->iomem);
> -    sysbus_mmio_map(sysbus, 0, BONITO_INTERNAL_REG_BASE);
> +    memory_region_add_subregion(host_mem, BONITO_INTERNAL_REG_BASE,
> +                                &s->iomem);
>   
>       /* set the north bridge pci configure  mapping */
>       memory_region_init_io(&phb->conf_mem, OBJECT(s), &bonito_pciconf_ops, s,
>                             "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
> -    sysbus_init_mmio(sysbus, &phb->conf_mem);
> -    sysbus_mmio_map(sysbus, 1, BONITO_PCICONFIG_BASE);
> +    memory_region_add_subregion(host_mem, BONITO_PCICONFIG_BASE,
> +                                &phb->conf_mem);
>   
>       /* set the south bridge pci configure  mapping */
>       memory_region_init_io(&phb->data_mem, OBJECT(s), &bonito_spciconf_ops, s,
>                             "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
> -    sysbus_init_mmio(sysbus, &phb->data_mem);
> -    sysbus_mmio_map(sysbus, 2, BONITO_SPCICONFIG_BASE);
> +    memory_region_add_subregion(host_mem, BONITO_SPCICONFIG_BASE,
> +                                &phb->data_mem);
>   
>       create_unimplemented_device("bonito", BONITO_REG_BASE, BONITO_REG_SIZE);
>   
>       memory_region_init_io(&s->iomem_ldma, OBJECT(s), &bonito_ldma_ops, s,
>                             "ldma", 0x100);
> -    sysbus_init_mmio(sysbus, &s->iomem_ldma);
> -    sysbus_mmio_map(sysbus, 3, 0x1fe00200);
> +    memory_region_add_subregion(host_mem, 0x1fe00200,
> +                                &s->iomem_ldma);
>   
>       /* PCI copier */
>       memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
>                             "cop", 0x100);
> -    sysbus_init_mmio(sysbus, &s->iomem_cop);
> -    sysbus_mmio_map(sysbus, 4, 0x1fe00300);
> +    memory_region_add_subregion(host_mem, 0x1fe00300,
> +                                &s->iomem_cop);

At least the above two hunks look like they could now fit into one line?

Anyway:
Reviewed-by: Thomas Huth <thuth@redhat.com>



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

* Re: [PATCH v2 00/12] hw: Strengthen SysBus & QBus API
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (11 preceding siblings ...)
  2023-10-19  7:16 ` [PATCH v2 12/12] hw/sysbus: Ensure device is realized before mapping it Philippe Mathieu-Daudé
@ 2023-10-19  7:32 ` Thomas Huth
  2023-10-19  7:35   ` Philippe Mathieu-Daudé
  2023-10-19 21:40 ` Philippe Mathieu-Daudé
  13 siblings, 1 reply; 24+ messages in thread
From: Thomas Huth @ 2023-10-19  7:32 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/2023 09.15, Philippe Mathieu-Daudé wrote:
> Hi,
> 
> This series ensure:
> 
> - qbus_new() and sysbus_init_mmio() are called *before*
>    a device is realized,
> - sysbus_mmio_map() is called *after* it is realized.

Just an additional idea that came up while reading your series:
I think we should also add proper function descriptions for 
sysbus_init_mmio() and sysbus_mmio_map() in include/hw/sysbus.h, to make 
people aware that the first function should be used within the device to 
expose the mmio region, while the latter should be used in the machine code 
that wires the devices to the correct location in the sysbus memory. What do 
you think?

  Thomas




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

* Re: [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize
  2023-10-19  7:16 ` [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize Philippe Mathieu-Daudé
@ 2023-10-19  7:34   ` Thomas Huth
  0 siblings, 0 replies; 24+ messages in thread
From: Thomas Huth @ 2023-10-19  7:34 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/2023 09.16, Philippe Mathieu-Daudé wrote:
> In order to make the next commit trivial, move the sysbus_init_mmio()
> call in allwinner_r40_dramc_init() just before the corresponding
> sysbus_mmio_map_overlap() call in allwinner_r40_dramc_realize().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/misc/allwinner-r40-dramc.c | 15 +++++++--------
>   1 file changed, 7 insertions(+), 8 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>




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

* Re: [PATCH v2 00/12] hw: Strengthen SysBus & QBus API
  2023-10-19  7:32 ` [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Thomas Huth
@ 2023-10-19  7:35   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:35 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/23 09:32, Thomas Huth wrote:
> On 19/10/2023 09.15, Philippe Mathieu-Daudé wrote:
>> Hi,
>>
>> This series ensure:
>>
>> - qbus_new() and sysbus_init_mmio() are called *before*
>>    a device is realized,
>> - sysbus_mmio_map() is called *after* it is realized.
> 
> Just an additional idea that came up while reading your series:
> I think we should also add proper function descriptions for 
> sysbus_init_mmio() and sysbus_mmio_map() in include/hw/sysbus.h, to make 
> people aware that the first function should be used within the device to 
> expose the mmio region, while the latter should be used in the machine 
> code that wires the devices to the correct location in the sysbus 
> memory. What do you think?

I've been thinking about this, but I plan to eventually merge these
calls to the QDev API (after removing more unuseful / duplicated
SysBus functions). So I was planning to document there. But yeah,
better to start doing that now.


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

* Re: [PATCH v2 05/12] hw/pci-host/bonito: Do not use SysBus API to map local MMIO region
  2023-10-19  7:26   ` Thomas Huth
@ 2023-10-19  7:38     ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19  7:38 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/23 09:26, Thomas Huth wrote:
> On 19/10/2023 09.16, Philippe Mathieu-Daudé wrote:
>> There is no point in exposing an internal MMIO region via
>> SysBus and directly mapping it in the very same device.
>>
>> Just map it without using the SysBus API.
>>
>> Transformation done using the following coccinelle script:
>>
>>    @@
>>    expression sbdev;
>>    expression index;
>>    expression addr;
>>    expression subregion;
>>    @@
>>    -    sysbus_init_mmio(sbdev, subregion);
>>         ... when != sbdev
>>    -    sysbus_mmio_map(sbdev, index, addr);
>>    +    memory_region_add_subregion(get_system_memory(), addr, 
>> subregion);
>>
>> and manually adding the local 'host_mem' variable to
>> avoid multiple calls to get_system_memory().
> 
> Thanks for updating it!
> 
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   hw/pci-host/bonito.c | 32 ++++++++++++++++----------------
>>   1 file changed, 16 insertions(+), 16 deletions(-)


>>       /* PCI copier */
>>       memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
>>                             "cop", 0x100);
>> -    sysbus_init_mmio(sysbus, &s->iomem_cop);
>> -    sysbus_mmio_map(sysbus, 4, 0x1fe00300);
>> +    memory_region_add_subregion(host_mem, 0x1fe00300,
>> +                                &s->iomem_cop);
> 
> At least the above two hunks look like they could now fit into one line?

This file will be heavily reworked soon, but meanwhile I did what you
suggested.

> Anyway:
> Reviewed-by: Thomas Huth <thuth@redhat.com>

Thanks!



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

* Re: [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region
  2023-10-19  7:16 ` [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
@ 2023-10-19  7:44   ` Thomas Huth
  0 siblings, 0 replies; 24+ messages in thread
From: Thomas Huth @ 2023-10-19  7:44 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/2023 09.16, Philippe Mathieu-Daudé wrote:
> There is no point in exposing an internal MMIO region via
> SysBus and directly mapping it in the very same device.
> 
> Just map it without using the SysBus API.
> 
> Transformation done using the following coccinelle script:
> 
>    @@
>    expression sbdev;
>    expression index;
>    expression addr;
>    expression subregion;
>    @@
>    -    sysbus_init_mmio(sbdev, subregion);
>         ... when != sbdev
>    -    sysbus_mmio_map(sbdev, index, addr);
>    +    memory_region_add_subregion(get_system_memory(),
>    +                                addr, subregion);
> 
>    @@
>    expression priority;
>    @@
>    -    sysbus_init_mmio(sbdev, subregion);
>         ... when != sbdev
>    -    sysbus_mmio_map_overlap(sbdev, index, addr, priority);
>    +    memory_region_add_subregion_overlap(get_system_memory(),
>    +                                        addr,
>    +                                        subregion, priority);
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/misc/allwinner-r40-dramc.c | 13 ++++++-------
>   1 file changed, 6 insertions(+), 7 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>




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

* Re: [PATCH v2 09/12] hw/s390x/css-bridge: Realize sysbus device before accessing it
  2023-10-19  7:16 ` [PATCH v2 09/12] hw/s390x/css-bridge: Realize " Philippe Mathieu-Daudé
@ 2023-10-19  7:46   ` Thomas Huth
  2023-10-20 14:21   ` Eric Farman
  1 sibling, 0 replies; 24+ messages in thread
From: Thomas Huth @ 2023-10-19  7:46 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Jiaxun Yang, Marcel Apfelbaum,
	qemu-arm, Mark Cave-Ayland, Eric Farman, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/2023 09.16, Philippe Mathieu-Daudé wrote:
> qbus_new() should not be called on unrealized device.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/s390x/css-bridge.c | 7 +++----
>   1 file changed, 3 insertions(+), 4 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>



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

* Re: [PATCH v2 00/12] hw: Strengthen SysBus & QBus API
  2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
                   ` (12 preceding siblings ...)
  2023-10-19  7:32 ` [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Thomas Huth
@ 2023-10-19 21:40 ` Philippe Mathieu-Daudé
  13 siblings, 0 replies; 24+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19 21:40 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Eric Farman,
	Paolo Bonzini, qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On 19/10/23 09:15, Philippe Mathieu-Daudé wrote:

> Philippe Mathieu-Daudé (12):
>    hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region
>    hw/i386/intel_iommu: Do not use SysBus API to map local MMIO region
>    hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init ->
>      realize
>    hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO
>      region
>    hw/pci-host/bonito: Do not use SysBus API to map local MMIO region
>    hw/acpi: Realize ACPI_GED sysbus device before accessing it
>    hw/arm/virt: Realize ARM_GICV2M sysbus device before accessing it
>    hw/isa: Realize ISA BUS sysbus device before accessing it
>    hw/s390x/css-bridge: Realize sysbus device before accessing it

Patches 1-9 queued to hw-misc.


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

* Re: [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region
  2023-10-19  7:15 ` [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
@ 2023-10-20  3:33   ` Zhao Liu
  0 siblings, 0 replies; 24+ messages in thread
From: Zhao Liu @ 2023-10-20  3:33 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Daniel P. Berrangé, Ilya Leoshkevich,
	Halil Pasic, Huacai Chen, Beniamino Galvani, Thomas Huth,
	Jiaxun Yang, Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland,
	Eric Farman, Paolo Bonzini, qemu-s390x, Strahinja Jankovic,
	Richard Henderson, Markus Armbruster, Song Gao, Eduardo Habkost,
	Peter Xu, Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On Thu, Oct 19, 2023 at 09:15:59AM +0200, Philippe Mathieu-Daudé wrote:
> Date: Thu, 19 Oct 2023 09:15:59 +0200
> From: Philippe Mathieu-Daudé <philmd@linaro.org>
> Subject: [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map
>  local MMIO region
> X-Mailer: git-send-email 2.41.0
> 
> There is no point in exposing an internal MMIO region via
> SysBus and directly mapping it in the very same device.
> 
> Just map it without using the SysBus API.
> 
> Transformation done using the following coccinelle script:
> 
>   @@
>   expression sbdev;
>   expression index;
>   expression addr;
>   expression subregion;
>   @@
>   -    sysbus_init_mmio(sbdev, subregion);
>        ... when != sbdev
>   -    sysbus_mmio_map(sbdev, index, addr);
>   +    memory_region_add_subregion(get_system_memory(), addr, subregion);
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>  hw/i386/amd_iommu.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>

-Zhao

> 
> diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
> index 8d0f2f99dd..7965415b47 100644
> --- a/hw/i386/amd_iommu.c
> +++ b/hw/i386/amd_iommu.c
> @@ -1579,9 +1579,8 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
>      /* set up MMIO */
>      memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
>                            AMDVI_MMIO_SIZE);
> -
> -    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio);
> -    sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, AMDVI_BASE_ADDR);
> +    memory_region_add_subregion(get_system_memory(), AMDVI_BASE_ADDR,
> +                                &s->mmio);
>      pci_setup_iommu(bus, amdvi_host_dma_iommu, s);
>      amdvi_init(s);
>  }
> -- 
> 2.41.0
> 
> 
> 


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

* Re: [PATCH v2 02/12] hw/i386/intel_iommu: Do not use SysBus API to map local MMIO region
  2023-10-19  7:16 ` [PATCH v2 02/12] hw/i386/intel_iommu: " Philippe Mathieu-Daudé
@ 2023-10-20  3:35   ` Zhao Liu
  0 siblings, 0 replies; 24+ messages in thread
From: Zhao Liu @ 2023-10-20  3:35 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Daniel P. Berrangé, Ilya Leoshkevich,
	Halil Pasic, Huacai Chen, Beniamino Galvani, Thomas Huth,
	Jiaxun Yang, Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland,
	Eric Farman, Paolo Bonzini, qemu-s390x, Strahinja Jankovic,
	Richard Henderson, Markus Armbruster, Song Gao, Eduardo Habkost,
	Peter Xu, Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On Thu, Oct 19, 2023 at 09:16:00AM +0200, Philippe Mathieu-Daudé wrote:
> Date: Thu, 19 Oct 2023 09:16:00 +0200
> From: Philippe Mathieu-Daudé <philmd@linaro.org>
> Subject: [PATCH v2 02/12] hw/i386/intel_iommu: Do not use SysBus API to map
>  local MMIO region
> X-Mailer: git-send-email 2.41.0
> 
> There is no point in exposing an internal MMIO region via
> SysBus and directly mapping it in the very same device.
> 
> Just map it without using the SysBus API.
> 
> Transformation done using the following coccinelle script:
> 
>   @@
>   expression sbdev;
>   expression index;
>   expression addr;
>   expression subregion;
>   @@
>   -    sysbus_init_mmio(sbdev, subregion);
>        ... when != sbdev
>   -    sysbus_mmio_map(sbdev, index, addr);
>   +    memory_region_add_subregion(get_system_memory(), addr, subregion);
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>  hw/i386/intel_iommu.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

Oh, I missed you've queued this series...

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>

> 
> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
> index 2c832ab68b..e4f6cedcb1 100644
> --- a/hw/i386/intel_iommu.c
> +++ b/hw/i386/intel_iommu.c
> @@ -4134,6 +4134,8 @@ static void vtd_realize(DeviceState *dev, Error **errp)
>      qemu_mutex_init(&s->iommu_lock);
>      memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
>                            "intel_iommu", DMAR_REG_SIZE);
> +    memory_region_add_subregion(get_system_memory(),
> +                                Q35_HOST_BRIDGE_IOMMU_ADDR, &s->csrmem);
>  
>      /* Create the shared memory regions by all devices */
>      memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar",
> @@ -4148,15 +4150,12 @@ static void vtd_realize(DeviceState *dev, Error **errp)
>      memory_region_add_subregion_overlap(&s->mr_nodmar,
>                                          VTD_INTERRUPT_ADDR_FIRST,
>                                          &s->mr_ir, 1);
> -
> -    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
>      /* No corresponding destroy */
>      s->iotlb = g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal,
>                                       g_free, g_free);
>      s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
>                                        g_free, g_free);
>      vtd_init(s);
> -    sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
>      pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
>      /* Pseudo address space under root PCI bus. */
>      x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
> -- 
> 2.41.0
> 
> 
> 


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

* Re: [PATCH v2 09/12] hw/s390x/css-bridge: Realize sysbus device before accessing it
  2023-10-19  7:16 ` [PATCH v2 09/12] hw/s390x/css-bridge: Realize " Philippe Mathieu-Daudé
  2023-10-19  7:46   ` Thomas Huth
@ 2023-10-20 14:21   ` Eric Farman
  1 sibling, 0 replies; 24+ messages in thread
From: Eric Farman @ 2023-10-20 14:21 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Daniel P. Berrangé, Ilya Leoshkevich, Halil Pasic,
	Huacai Chen, Beniamino Galvani, Thomas Huth, Jiaxun Yang,
	Marcel Apfelbaum, qemu-arm, Mark Cave-Ayland, Paolo Bonzini,
	qemu-s390x, Strahinja Jankovic, Richard Henderson,
	Markus Armbruster, Song Gao, Eduardo Habkost, Peter Xu,
	Sergio Lopez, Christian Borntraeger, David Hildenbrand,
	Peter Maydell, Michael S. Tsirkin, Jason Wang

On Thu, 2023-10-19 at 09:16 +0200, Philippe Mathieu-Daudé wrote:
> qbus_new() should not be called on unrealized device.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  hw/s390x/css-bridge.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)

Reviewed-by: Eric Farman <farman@linux.ibm.com>


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

end of thread, other threads:[~2023-10-20 14:33 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-19  7:15 [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Philippe Mathieu-Daudé
2023-10-19  7:15 ` [PATCH v2 01/12] hw/i386/amd_iommu: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
2023-10-20  3:33   ` Zhao Liu
2023-10-19  7:16 ` [PATCH v2 02/12] hw/i386/intel_iommu: " Philippe Mathieu-Daudé
2023-10-20  3:35   ` Zhao Liu
2023-10-19  7:16 ` [PATCH v2 03/12] hw/misc/allwinner-dramc: Move sysbus_mmio_map call from init -> realize Philippe Mathieu-Daudé
2023-10-19  7:34   ` Thomas Huth
2023-10-19  7:16 ` [PATCH v2 04/12] hw/misc/allwinner-dramc: Do not use SysBus API to map local MMIO region Philippe Mathieu-Daudé
2023-10-19  7:44   ` Thomas Huth
2023-10-19  7:16 ` [PATCH v2 05/12] hw/pci-host/bonito: " Philippe Mathieu-Daudé
2023-10-19  7:26   ` Thomas Huth
2023-10-19  7:38     ` Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 06/12] hw/acpi: Realize ACPI_GED sysbus device before accessing it Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 07/12] hw/arm/virt: Realize ARM_GICV2M " Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 08/12] hw/isa: Realize ISA BUS " Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 09/12] hw/s390x/css-bridge: Realize " Philippe Mathieu-Daudé
2023-10-19  7:46   ` Thomas Huth
2023-10-20 14:21   ` Eric Farman
2023-10-19  7:16 ` [PATCH v2 10/12] hw/qdev: Ensure parent device is not realized before adding bus Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 11/12] hw/sysbus: Ensure device is not realized before adding MMIO region Philippe Mathieu-Daudé
2023-10-19  7:16 ` [PATCH v2 12/12] hw/sysbus: Ensure device is realized before mapping it Philippe Mathieu-Daudé
2023-10-19  7:32 ` [PATCH v2 00/12] hw: Strengthen SysBus & QBus API Thomas Huth
2023-10-19  7:35   ` Philippe Mathieu-Daudé
2023-10-19 21:40 ` Philippe Mathieu-Daudé

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).