Skip to contentSkip to conversation

Q35 and I440FX host bridge QOM cleanup

PATCH Superseded Bernhard Beschow 5 participants 42 messages Jun 11, 2023 – Jun 26, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Q35 and I440FX host bridge QOM cleanup

This series resolves the legacy i440fx_init() function and instantiates the I440FX host bridge the QOM way. As a preparation the Q35 host bridge receives some cleanup as well.

Most of the Q35 patches have been submitted under [1] before. This series incorporates only the changes making the two device models consistent with each other.

The original plan of [1] was to clean up Q35 first and then submit a separate follow-up I440FX QOM'ification series. This series takes a more direct approach by cutting down on the changes in both device models while still allowing both device models to be instantiated the same way. The remaining patches in [1] would still be doable.

The series is structured as follows: The first 5 patches clean up Q35, the next patch massages Q35 to share its property names with I440FX. The rest of the series resolves i440fx_init().

Tesging done:
* make check
* make check-avocado
* Run xl create under Xen with the following config:

    name = "Manjaro"
    type = 'hvm'
    memory = 1536
    apic = 1
    usb = 1
    disk = [ "file:manjaro-kde-21.2.6-220416-linux515.iso,hdc:cdrom,r" ]
    device_model_override = "/usr/bin/qemu-system-x86_64"
    vga = "stdvga"
    sdl = 1

[1] https://patchew.org/QEMU/[email protected]/

Bernhard Beschow (15):
hw/i386/pc_q35: Resolve redundant q35_host variable hw/pci-host/q35: Fix double, contradicting .endianness assignment hw/pci-host/q35: Initialize PCMachineState::bus in board code hw/pci/pci_host: Introduce PCI_HOST_BYPASS_IOMMU macro hw/pci-host/q35: Initialize PCI_HOST_BYPASS_IOMMU property from board
code
hw/pci-host/q35: Make some property name macros reusable by i440fx hw/pci-host/i440fx: Replace magic values by existing constants hw/pci-host/i440fx: Have common names for some local variables hw/pci-host/i440fx: Move i440fx_realize() into PCII440FXState section hw/pci-host/i440fx: Make MemoryRegion pointers accessible as
properties
hw/pci-host/i440fx: Add PCI_HOST_PROP_IO_MEM property hw/pci-host/i440fx: Add PCI_HOST_{ABOVE,BELOW}_4G_MEM_SIZE properties hw/pci-host/i440fx: Add I440FX_HOST_PROP_PCI_TYPE property hw/pci-host/i440fx: Resolve i440fx_init()
hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

 include/hw/i386/pc.h         |   4 ++
 include/hw/pci-host/i440fx.h |  16 +----
 include/hw/pci-host/q35.h    |   5 --
 include/hw/pci/pci_host.h    |   2 +
 hw/i386/pc_piix.c            |  48 ++++++++-----
 hw/i386/pc_q35.c             |  31 +++++----
 hw/pci-host/i440fx.c         | 130 +++++++++++++++++++----------------
 hw/pci-host/q35.c            |  13 ++--
 hw/pci/pci_host.c            |   2 +-
 9 files changed, 134 insertions(+), 117 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/15 hw/i386/pc_q35: Resolve redundant q35_host variable

The variable is redundant to "phb" and is never used by its real type.

---
 hw/i386/pc_q35.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
hw/i386/pc_q35.c +13 -14
index 6155427e48..62c5d652b7 100644
@@ -120,8 +120,7 @@ static void pc_q35_init(MachineState *machine)
     PCMachineState *pcms = PC_MACHINE(machine);
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     X86MachineState *x86ms = X86_MACHINE(machine);
-    Q35PCIHost *q35_host;
-    PCIHostState *phb;
+    Object *phb;
     PCIBus *host_bus;
     PCIDevice *lpc;
     DeviceState *lpc_dev;
@@ -207,10 +206,10 @@ static void pc_q35_init(MachineState *machine)
     }
 
     /* create pci host bus */
-    q35_host = Q35_HOST_DEVICE(qdev_new(TYPE_Q35_HOST_DEVICE));
+    phb = OBJECT(qdev_new(TYPE_Q35_HOST_DEVICE));
 
     if (pcmc->pci_enabled) {
-        pci_hole64_size = object_property_get_uint(OBJECT(q35_host),
+        pci_hole64_size = object_property_get_uint(phb,
                                                    PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                    &error_abort);
     }
@@ -218,23 +217,23 @@ static void pc_q35_init(MachineState *machine)
     /* allocate ram and load rom/bios */
     pc_memory_init(pcms, system_memory, rom_memory, pci_hole64_size);
 
-    object_property_add_child(OBJECT(machine), "q35", OBJECT(q35_host));
-    object_property_set_link(OBJECT(q35_host), MCH_HOST_PROP_RAM_MEM,
+    object_property_add_child(OBJECT(machine), "q35", phb);
+    object_property_set_link(phb, MCH_HOST_PROP_RAM_MEM,
                              OBJECT(machine->ram), NULL);
-    object_property_set_link(OBJECT(q35_host), MCH_HOST_PROP_PCI_MEM,
+    object_property_set_link(phb, MCH_HOST_PROP_PCI_MEM,
                              OBJECT(pci_memory), NULL);
-    object_property_set_link(OBJECT(q35_host), MCH_HOST_PROP_SYSTEM_MEM,
+    object_property_set_link(phb, MCH_HOST_PROP_SYSTEM_MEM,
                              OBJECT(system_memory), NULL);
-    object_property_set_link(OBJECT(q35_host), MCH_HOST_PROP_IO_MEM,
+    object_property_set_link(phb, MCH_HOST_PROP_IO_MEM,
                              OBJECT(system_io), NULL);
-    object_property_set_int(OBJECT(q35_host), PCI_HOST_BELOW_4G_MEM_SIZE,
+    object_property_set_int(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
                             x86ms->below_4g_mem_size, NULL);
-    object_property_set_int(OBJECT(q35_host), PCI_HOST_ABOVE_4G_MEM_SIZE,
+    object_property_set_int(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
                             x86ms->above_4g_mem_size, NULL);
+
     /* pci */
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(q35_host), &error_fatal);
-    phb = PCI_HOST_BRIDGE(q35_host);
-    host_bus = phb->bus;
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
+    host_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pcie.0"));
     /* create ISA bus */
     lpc = pci_new_multifunction(PCI_DEVFN(ICH9_LPC_DEV, ICH9_LPC_FUNC), true,
                                 TYPE_ICH9_LPC_DEVICE);
Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/15 hw/pci-host/q35: Fix double, contradicting .endianness assignment

In reply to Bernhard Beschow

Fixes the following clangd warning (-Winitializer-overrides):

q35.c:297:19: Initializer overrides prior initialization of this subobject q35.c:292:19: previous initialization is here

Settle on native endian which causes the least overhead.

Fixes bafc90bdc594 ("q35: implement TSEG")
---
 hw/pci-host/q35.c | 1 -
 1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
     .valid.max_access_size = 4,
     .impl.min_access_size = 4,
     .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
 /* PCIe MMCFG */
Signature · 1 line
2.41.0
#3 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 6 lines · On Sun, 11 Jun 2023 12:33:59 +0200 Bernhard Beschow <[email protected]> wrote:

Fixes the following clangd warning (-Winitializer-overrides):

q35.c:297:19: Initializer overrides prior initialization of this subobject q35.c:292:19: previous initialization is here

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

Quoted 19 lines
Fixes bafc90bdc594 ("q35: implement TSEG")
---
 hw/pci-host/q35.c | 1 -
 1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
     .valid.max_access_size = 4,
     .impl.min_access_size = 4,
     .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
 /* PCIe MMCFG */
#4 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 6 lines · Igor Mammedov
Quoted 3 lines · On Sun, 11 Jun 2023 12:33:59 +0200 Bernhard Beschow <[email protected]> wrote:

Fixes the following clangd warning (-Winitializer-overrides):

q35.c:297:19: Initializer overrides prior initialization of this

Quoted 9 lines

subobject

Quoted 3 lines

q35.c:292:19: previous initialization is here

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Best regards,
Bernhard

[1]
https://patchew.org/QEMU/[email protected]/[email protected]/#[email protected]

Quoted 22 lines
Quoted 20 lines
Fixes bafc90bdc594 ("q35: implement TSEG")
---
 hw/pci-host/q35.c | 1 -
 1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
     .valid.max_access_size = 4,
     .impl.min_access_size = 4,
     .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
 /* PCIe MMCFG */
#5 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 21 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

On Sun, 11 Jun 2023 12:33:59 +0200
Bernhard Beschow <[email protected]> wrote:

> Fixes the following clangd warning (-Winitializer-overrides):
>
>   q35.c:297:19: Initializer overrides prior initialization of this subobject
>   q35.c:292:19: previous initialization is here
>
> Settle on native endian which causes the least overhead. indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Quoted 6 lines

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Quoted 22 lines
Quoted 20 lines
Fixes bafc90bdc594 ("q35: implement TSEG")

---
  hw/pci-host/q35.c | 1 -

  1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
       .valid.max_access_size = 4,
       .impl.min_access_size = 4,
       .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
   };


  /* PCIe MMCFG */

#6 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 29 lines · Michael S. Tsirkin
Quoted 21 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

Quoted 3 lines · On Sun, 11 Jun 2023 12:33:59 +0200 Bernhard Beschow <[email protected]> wrote:

Fixes the following clangd warning (-Winitializer-overrides):

   q35.c:297:19: Initializer overrides prior initialization of this

subobject

Quoted 3 lines

   q35.c:292:19: previous initialization is here

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Quoted 6 lines

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Quoted 6 lines

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Why don't you leave DEVICE_LITTLE_ENDIAN and remove DEVICE_NATIVE_ENDIAN instead? If this only returns all 1s then it does not matter and also DEVICE_LITTLE_ENDIAN was the last assignment so likely that was effective so far anyway.

Regards,
BALATON Zoltan

Quoted 25 lines
Quoted 22 lines
Quoted 20 lines
Fixes bafc90bdc594 ("q35: implement TSEG")

---
  hw/pci-host/q35.c | 1 -

  1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
       .valid.max_access_size = 4,
       .impl.min_access_size = 4,
       .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
   };


  /* PCIe MMCFG */

#7 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 37 lines · On Tue, 13 Jun 2023 13:07:17 +0200 (CEST) BALATON Zoltan <[email protected]>…
Quoted 29 lines · Michael S. Tsirkin
Quoted 21 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

Quoted 3 lines · On Sun, 11 Jun 2023 12:33:59 +0200 Bernhard Beschow <[email protected]> wrote:

Fixes the following clangd warning (-Winitializer-overrides):

   q35.c:297:19: Initializer overrides prior initialization of this

subobject

Quoted 3 lines

   q35.c:292:19: previous initialization is here

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Quoted 6 lines

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Quoted 6 lines

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Quoted 4 lines

Why don't you leave DEVICE_LITTLE_ENDIAN and remove DEVICE_NATIVE_ENDIAN instead? If this only returns all 1s then it does not matter and also DEVICE_LITTLE_ENDIAN was the last assignment so likely that was effective so far anyway.

I'm in favor of this as well,
the 'optimization' and then piling comments on top to clarify confusion should be justified by usefulness of it (no perf numbers/usecase were present so far). In absence of above, I'd prefer real hw behavior (LE in this case).

Quoted 29 lines

Regards,
BALATON Zoltan

Quoted 25 lines
Quoted 22 lines
Quoted 20 lines
Fixes bafc90bdc594 ("q35: implement TSEG")

---
  hw/pci-host/q35.c | 1 -

  1 file changed, 1 deletion(-)
hw/pci-host/q35.c +0 -1
index fd18920e7f..859c197f25 100644
@@ -290,7 +290,6 @@ static const MemoryRegionOps blackhole_ops = {
       .valid.max_access_size = 4,
       .impl.min_access_size = 4,
       .impl.max_access_size = 4,
-    .endianness = DEVICE_LITTLE_ENDIAN,
   };


  /* PCIe MMCFG */

#8 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 16 lines · Igor Mammedov
Quoted 13 lines · On Tue, 13 Jun 2023 13:07:17 +0200 (CEST) BALATON Zoltan <[email protected]>…
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

On Sun, 11 Jun 2023 12:33:59 +0200
Bernhard Beschow <[email protected]> wrote:

> Fixes the following clangd warning (-Winitializer-overrides):
>
>   q35.c:297:19: Initializer overrides prior initialization of this
subobject
>   q35.c:292:19: previous initialization is here
>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

Meaning trying to optimize this single MR on big-endian is irrelevant :)

Quoted 27 lines
Quoted 18 lines
Quoted 11 lines
Quoted 10 lines

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Quoted 6 lines

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Quoted 4 lines

Why don't you leave DEVICE_LITTLE_ENDIAN and remove DEVICE_NATIVE_ENDIAN instead? If this only returns all 1s then it does not matter and also DEVICE_LITTLE_ENDIAN was the last assignment so likely that was effective so far anyway.

I'm in favor of this as well,
the 'optimization' and then piling comments on top to clarify confusion should be justified by usefulness of it (no perf numbers/usecase were present so far). In absence of above, I'd prefer real hw behavior (LE in this case).

#9 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Igor Mammedov
Quoted 12 lines · On Tue, 13 Jun 2023 13:07:17 +0200 (CEST) BALATON Zoltan <[email protected]>…
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

On Sun, 11 Jun 2023 12:33:59 +0200
Bernhard Beschow <[email protected]> wrote:
> Fixes the following clangd warning (-Winitializer-overrides):
>
>   q35.c:297:19: Initializer overrides prior initialization of this
subobject
>   q35.c:292:19: previous initialization is here
>

Quoted 35 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

Meaning trying to optimize this single MR on big-endian is irrelevant :)

Quoted 27 lines
Quoted 18 lines
Quoted 11 lines
Quoted 10 lines

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Quoted 6 lines

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Quoted 4 lines

Why don't you leave DEVICE_LITTLE_ENDIAN and remove DEVICE_NATIVE_ENDIAN instead? If this only returns all 1s then it does not matter and also DEVICE_LITTLE_ENDIAN was the last assignment so likely that was effective so far anyway.

I'm in favor of this as well,
the 'optimization' and then piling comments on top to clarify confusion should be justified by usefulness of it (no perf numbers/usecase were present so far). In absence of above, I'd prefer real hw behavior (LE in this case).

OK I'm fine with this too. Sorry about leading you astray.

Signature · 1 line
MST
#10 lore raw

Re: [PATCH 02/15] hw/pci-host/q35: Fix double, contradicting .endianness assignment

Am 13. Juni 2023 15:01:40 UTC schrieb "Michael S. Tsirkin" <[email protected]>:

Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 47 lines · Igor Mammedov
Quoted 15 lines
Quoted 12 lines · On Tue, 13 Jun 2023 13:07:17 +0200 (CEST) BALATON Zoltan <[email protected]>…
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Bernhard Beschow

On Mon, Jun 12, 2023 at 3:01 PM Igor Mammedov <[email protected]> wrote:

On Sun, 11 Jun 2023 12:33:59 +0200
Bernhard Beschow <[email protected]> wrote:
> Fixes the following clangd warning (-Winitializer-overrides):
>
>   q35.c:297:19: Initializer overrides prior initialization of this
subobject
>   q35.c:292:19: previous initialization is here
>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Settle on native endian which causes the least overhead.

indeed it doesn't matter which way we read all ones, so that should work. but does it really matter (I mean the overhead/what workload)? If not, I'd prefer explicit LE as it's now to be consistent the the rest of memops on Q35.

Meaning trying to optimize this single MR on big-endian is irrelevant :)

Quoted 23 lines
Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

I got a comment from Michael about this in [1], so I've changed it. I don't mind changing it either way.

Best regards,
Bernhard

[1] https://patchew.org/QEMU/[email protected]/
[email protected]/# [email protected]

Quoted 6 lines

Hmm it's not terribly important, and the optimization is trivial, but yes people tend to copy code, good point. Maybe add a comment? /*
* Note: don't copy this! normally use DEVICE_LITTLE_ENDIAN. This only
* works because we don't allow writes and always read all-ones. */

Why don't you leave DEVICE_LITTLE_ENDIAN and remove DEVICE_NATIVE_ENDIAN instead? If this only returns all 1s then it does not matter and also DEVICE_LITTLE_ENDIAN was the last assignment so likely that was effective so far anyway.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

I'm in favor of this as well,
the 'optimization' and then piling comments on top to clarify confusion should be justified by usefulness of it (no perf numbers/usecase were present so far). In absence of above, I'd prefer real hw behavior (LE in this case).

OK I'm fine with this too. Sorry about leading you astray.

No worries. I'm happy to change it back to LE.

Best regards,
Bernhard

#11 lore raw

PATCH 3/15 hw/pci-host/q35: Initialize PCMachineState::bus in board code

In reply to Bernhard Beschow

The Q35 PCI host currently sets the PC machine's PCI bus attribute through global state, thereby assuming the machine to be a PC machine. The Q35 machine code already holds on to Q35's pci bus attribute, so can easily set its own property while preserving encapsulation.

---
 hw/i386/pc_q35.c  | 4 +++-
 hw/pci-host/q35.c | 1 -
 2 files changed, 3 insertions(+), 2 deletions(-)
hw/i386/pc_q35.c +3 -1
index 62c5d652b7..29b46d3e1c 100644
@@ -230,10 +230,12 @@ static void pc_q35_init(MachineState *machine)
                             x86ms->below_4g_mem_size, NULL);
     object_property_set_int(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
                             x86ms->above_4g_mem_size, NULL);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
 
     /* pci */
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
     host_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pcie.0"));
+    pcms->bus = host_bus;
+
     /* create ISA bus */
     lpc = pci_new_multifunction(PCI_DEVFN(ICH9_LPC_DEV, ICH9_LPC_FUNC), true,
                                 TYPE_ICH9_LPC_DEVICE);
hw/pci-host/q35.c +0 -1
index 859c197f25..23b689dba3 100644
@@ -66,7 +66,6 @@ static void q35_host_realize(DeviceState *dev, Error **errp)
                                 s->mch.pci_address_space,
                                 s->mch.address_space_io,
                                 0, TYPE_PCIE_BUS);
-    PC_MACHINE(qdev_get_machine())->bus = pci->bus;
     pci->bypass_iommu =
         PC_MACHINE(qdev_get_machine())->default_bus_bypass_iommu;
     qdev_realize(DEVICE(&s->mch), BUS(pci->bus), &error_fatal);
Signature · 1 line
2.41.0
Reviewed-by on v1 3/15
#12 lore raw

Re: [PATCH 03/15] hw/pci-host/q35: Initialize PCMachineState::bus in board code

Quoted 10 lines · Bernhard Beschow

The Q35 PCI host currently sets the PC machine's PCI bus attribute through global state, thereby assuming the machine to be a PC machine. The Q35 machine code already holds on to Q35's pci bus attribute, so can easily set its own property while preserving encapsulation.

---
  hw/i386/pc_q35.c  | 4 +++-
  hw/pci-host/q35.c | 1 -
  2 files changed, 3 insertions(+), 2 deletions(-)
Reviewed-by on v1 3/15
#13 lore raw

Re: [PATCH 03/15] hw/pci-host/q35: Initialize PCMachineState::bus in board code

In reply to Bernhard Beschow
Quoted 6 lines · On Sun, 11 Jun 2023 12:34:00 +0200 Bernhard Beschow <[email protected]> wrote:

The Q35 PCI host currently sets the PC machine's PCI bus attribute through global state, thereby assuming the machine to be a PC machine. The Q35 machine code already holds on to Q35's pci bus attribute, so can easily set its own property while preserving encapsulation.

Quoted 35 lines
---
 hw/i386/pc_q35.c  | 4 +++-
 hw/pci-host/q35.c | 1 -
 2 files changed, 3 insertions(+), 2 deletions(-)
hw/i386/pc_q35.c +3 -1
index 62c5d652b7..29b46d3e1c 100644
@@ -230,10 +230,12 @@ static void pc_q35_init(MachineState *machine)
                             x86ms->below_4g_mem_size, NULL);
     object_property_set_int(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
                             x86ms->above_4g_mem_size, NULL);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
 
     /* pci */
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
     host_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pcie.0"));
+    pcms->bus = host_bus;
+
     /* create ISA bus */
     lpc = pci_new_multifunction(PCI_DEVFN(ICH9_LPC_DEV, ICH9_LPC_FUNC), true,
                                 TYPE_ICH9_LPC_DEVICE);
hw/pci-host/q35.c +0 -1
index 859c197f25..23b689dba3 100644
@@ -66,7 +66,6 @@ static void q35_host_realize(DeviceState *dev, Error **errp)
                                 s->mch.pci_address_space,
                                 s->mch.address_space_io,
                                 0, TYPE_PCIE_BUS);
-    PC_MACHINE(qdev_get_machine())->bus = pci->bus;
     pci->bypass_iommu =
         PC_MACHINE(qdev_get_machine())->default_bus_bypass_iommu;
     qdev_realize(DEVICE(&s->mch), BUS(pci->bus), &error_fatal);
#14 lore raw

PATCH 4/15 hw/pci/pci_host: Introduce PCI_HOST_BYPASS_IOMMU macro

In reply to Bernhard Beschow

Introduce a macro to avoid copy and pasting strings which can easily cause typos.

---
 include/hw/pci/pci_host.h | 2 ++
 hw/pci/pci_host.c         | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/hw/pci/pci_host.h +2 -0
index c6f4eb4585..e52d8ec2cd 100644
@@ -31,6 +31,8 @@
 #include "hw/sysbus.h"
 #include "qom/object.h"
 
+#define PCI_HOST_BYPASS_IOMMU "bypass-iommu"
+
 #define TYPE_PCI_HOST_BRIDGE "pci-host-bridge"
 OBJECT_DECLARE_TYPE(PCIHostState, PCIHostBridgeClass, PCI_HOST_BRIDGE)
 
hw/pci/pci_host.c +1 -1
index dfd185bbb4..7af8afdcbe 100644
@@ -232,7 +232,7 @@ const VMStateDescription vmstate_pcihost = {
 static Property pci_host_properties_common[] = {
     DEFINE_PROP_BOOL("x-config-reg-migration-enabled", PCIHostState,
                      mig_enabled, true),
-    DEFINE_PROP_BOOL("bypass-iommu", PCIHostState, bypass_iommu, false),
+    DEFINE_PROP_BOOL(PCI_HOST_BYPASS_IOMMU, PCIHostState, bypass_iommu, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.41.0
Reviewed-by on v1 4/15
#15 lore raw

Re: [PATCH 04/15] hw/pci/pci_host: Introduce PCI_HOST_BYPASS_IOMMU macro

Quoted 5 lines · On Sun, 11 Jun 2023 12:34:01 +0200 Bernhard Beschow <[email protected]> wrote:

Introduce a macro to avoid copy and pasting strings which can easily cause typos.

Quoted 31 lines
---
 include/hw/pci/pci_host.h | 2 ++
 hw/pci/pci_host.c         | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/hw/pci/pci_host.h +2 -0
index c6f4eb4585..e52d8ec2cd 100644
@@ -31,6 +31,8 @@
 #include "hw/sysbus.h"
 #include "qom/object.h"
 
+#define PCI_HOST_BYPASS_IOMMU "bypass-iommu"
+
 #define TYPE_PCI_HOST_BRIDGE "pci-host-bridge"
 OBJECT_DECLARE_TYPE(PCIHostState, PCIHostBridgeClass, PCI_HOST_BRIDGE)
 
hw/pci/pci_host.c +1 -1
index dfd185bbb4..7af8afdcbe 100644
@@ -232,7 +232,7 @@ const VMStateDescription vmstate_pcihost = {
 static Property pci_host_properties_common[] = {
     DEFINE_PROP_BOOL("x-config-reg-migration-enabled", PCIHostState,
                      mig_enabled, true),
-    DEFINE_PROP_BOOL("bypass-iommu", PCIHostState, bypass_iommu, false),
+    DEFINE_PROP_BOOL(PCI_HOST_BYPASS_IOMMU, PCIHostState, bypass_iommu, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
#16 lore raw

PATCH 5/15 hw/pci-host/q35: Initialize PCI_HOST_BYPASS_IOMMU property from board code

In reply to Bernhard Beschow

The Q35 PCI host already has a PCI_HOST_BYPASS_IOMMU property. However, the host initializes this property itself by accessing global machine state, thereby assuming it to be a PC machine. Avoid this by having board code set this property.

---
 hw/i386/pc_q35.c  | 2 ++
 hw/pci-host/q35.c | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)
hw/i386/pc_q35.c +2 -0
index 29b46d3e1c..5220b535b2 100644
@@ -230,6 +230,8 @@ static void pc_q35_init(MachineState *machine)
                             x86ms->below_4g_mem_size, NULL);
     object_property_set_int(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
                             x86ms->above_4g_mem_size, NULL);
+    object_property_set_bool(phb, PCI_HOST_BYPASS_IOMMU,
+                             pcms->default_bus_bypass_iommu, NULL);
     sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
 
     /* pci */
hw/pci-host/q35.c +1 -2
index 23b689dba3..7980ddde69 100644
@@ -66,8 +66,7 @@ static void q35_host_realize(DeviceState *dev, Error **errp)
                                 s->mch.pci_address_space,
                                 s->mch.address_space_io,
                                 0, TYPE_PCIE_BUS);
-    pci->bypass_iommu =
-        PC_MACHINE(qdev_get_machine())->default_bus_bypass_iommu;
+
     qdev_realize(DEVICE(&s->mch), BUS(pci->bus), &error_fatal);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 5/15
#17 lore raw

Re: [PATCH 05/15] hw/pci-host/q35: Initialize PCI_HOST_BYPASS_IOMMU property from board code

Quoted 10 lines · Bernhard Beschow

The Q35 PCI host already has a PCI_HOST_BYPASS_IOMMU property. However, the host initializes this property itself by accessing global machine state, thereby assuming it to be a PC machine. Avoid this by having board code set this property.

---
  hw/i386/pc_q35.c  | 2 ++
  hw/pci-host/q35.c | 3 +--
  2 files changed, 3 insertions(+), 2 deletions(-)

:)

Reviewed-by on v1 5/15
#18 lore raw

Re: [PATCH 05/15] hw/pci-host/q35: Initialize PCI_HOST_BYPASS_IOMMU property from board code

In reply to Bernhard Beschow
Quoted 6 lines · On Sun, 11 Jun 2023 12:34:02 +0200 Bernhard Beschow <[email protected]> wrote:

The Q35 PCI host already has a PCI_HOST_BYPASS_IOMMU property. However, the host initializes this property itself by accessing global machine state, thereby assuming it to be a PC machine. Avoid this by having board code set this property.

Quoted 32 lines
---
 hw/i386/pc_q35.c  | 2 ++
 hw/pci-host/q35.c | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)
hw/i386/pc_q35.c +2 -0
index 29b46d3e1c..5220b535b2 100644
@@ -230,6 +230,8 @@ static void pc_q35_init(MachineState *machine)
                             x86ms->below_4g_mem_size, NULL);
     object_property_set_int(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
                             x86ms->above_4g_mem_size, NULL);
+    object_property_set_bool(phb, PCI_HOST_BYPASS_IOMMU,
+                             pcms->default_bus_bypass_iommu, NULL);
     sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
 
     /* pci */
hw/pci-host/q35.c +1 -2
index 23b689dba3..7980ddde69 100644
@@ -66,8 +66,7 @@ static void q35_host_realize(DeviceState *dev, Error **errp)
                                 s->mch.pci_address_space,
                                 s->mch.address_space_io,
                                 0, TYPE_PCIE_BUS);
-    pci->bypass_iommu =
-        PC_MACHINE(qdev_get_machine())->default_bus_bypass_iommu;
+
     qdev_realize(DEVICE(&s->mch), BUS(pci->bus), &error_fatal);
 }
 
#19 lore raw

PATCH 6/15 hw/pci-host/q35: Make some property name macros reusable by i440fx

In reply to Bernhard Beschow
---
 include/hw/i386/pc.h      | 4 ++++
 include/hw/pci-host/q35.h | 5 -----
 hw/i386/pc_q35.c          | 8 ++++----
 hw/pci-host/q35.c         | 8 ++++----
 4 files changed, 12 insertions(+), 13 deletions(-)
include/hw/i386/pc.h +4 -0
index c661e9cc80..812613cc07 100644
@@ -145,6 +145,10 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level);
 
 void pc_guest_info_init(PCMachineState *pcms);
 
+#define PCI_HOST_PROP_RAM_MEM          "ram-mem"
+#define PCI_HOST_PROP_PCI_MEM          "pci-mem"
+#define PCI_HOST_PROP_SYSTEM_MEM       "system-mem"
+#define PCI_HOST_PROP_IO_MEM           "io-mem"
 #define PCI_HOST_PROP_PCI_HOLE_START   "pci-hole-start"
 #define PCI_HOST_PROP_PCI_HOLE_END     "pci-hole-end"
 #define PCI_HOST_PROP_PCI_HOLE64_START "pci-hole64-start"
include/hw/pci-host/q35.h +0 -5
index e89329c51e..1d98bbfe0d 100644
@@ -74,11 +74,6 @@ struct Q35PCIHost {
  * gmch part
  */
 
-#define MCH_HOST_PROP_RAM_MEM "ram-mem"
-#define MCH_HOST_PROP_PCI_MEM "pci-mem"
-#define MCH_HOST_PROP_SYSTEM_MEM "system-mem"
-#define MCH_HOST_PROP_IO_MEM "io-mem"
-
 /* PCI configuration */
 #define MCH_HOST_BRIDGE                        "MCH"
 
hw/i386/pc_q35.c +4 -4
index 5220b535b2..8bfe388c9e 100644
@@ -218,13 +218,13 @@ static void pc_q35_init(MachineState *machine)
     pc_memory_init(pcms, system_memory, rom_memory, pci_hole64_size);
 
     object_property_add_child(OBJECT(machine), "q35", phb);
-    object_property_set_link(phb, MCH_HOST_PROP_RAM_MEM,
+    object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
                              OBJECT(machine->ram), NULL);
-    object_property_set_link(phb, MCH_HOST_PROP_PCI_MEM,
+    object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
                              OBJECT(pci_memory), NULL);
-    object_property_set_link(phb, MCH_HOST_PROP_SYSTEM_MEM,
+    object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
                              OBJECT(system_memory), NULL);
-    object_property_set_link(phb, MCH_HOST_PROP_IO_MEM,
+    object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
                              OBJECT(system_io), NULL);
     object_property_set_int(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
                             x86ms->below_4g_mem_size, NULL);
hw/pci-host/q35.c +4 -4
index 7980ddde69..ded3f6e4f4 100644
@@ -240,19 +240,19 @@ static void q35_host_initfn(Object *obj)
     object_property_add_uint64_ptr(obj, PCIE_HOST_MCFG_SIZE,
                                    &pehb->size, OBJ_PROP_FLAG_READ);
 
-    object_property_add_link(obj, MCH_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,
+    object_property_add_link(obj, PCI_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,
                              (Object **) &s->mch.ram_memory,
                              qdev_prop_allow_set_link_before_realize, 0);
 
-    object_property_add_link(obj, MCH_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
+    object_property_add_link(obj, PCI_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
                              (Object **) &s->mch.pci_address_space,
                              qdev_prop_allow_set_link_before_realize, 0);
 
-    object_property_add_link(obj, MCH_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
+    object_property_add_link(obj, PCI_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
                              (Object **) &s->mch.system_memory,
                              qdev_prop_allow_set_link_before_realize, 0);
 
-    object_property_add_link(obj, MCH_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
+    object_property_add_link(obj, PCI_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
                              (Object **) &s->mch.address_space_io,
                              qdev_prop_allow_set_link_before_realize, 0);
 }
Signature · 1 line
2.41.0
#20 lore raw

PATCH 7/15 hw/pci-host/i440fx: Replace magic values by existing constants

In reply to Bernhard Beschow
---
 hw/pci-host/i440fx.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/pci-host/i440fx.c +4 -4
index 61e7b97ff4..daa4d11104 100644
@@ -277,8 +277,8 @@ PCIBus *i440fx_init(const char *pci_type,
 
     /* if *disabled* show SMRAM to all CPUs */
     memory_region_init_alias(&f->smram_region, OBJECT(d), "smram-region",
-                             f->pci_address_space, 0xa0000, 0x20000);
-    memory_region_add_subregion_overlap(f->system_memory, 0xa0000,
+                             f->pci_address_space, SMRAM_C_BASE, SMRAM_C_SIZE);
+    memory_region_add_subregion_overlap(f->system_memory, SMRAM_C_BASE,
                                         &f->smram_region, 1);
     memory_region_set_enabled(&f->smram_region, true);
 
@@ -286,9 +286,9 @@ PCIBus *i440fx_init(const char *pci_type,
     memory_region_init(&f->smram, OBJECT(d), "smram", 4 * GiB);
     memory_region_set_enabled(&f->smram, true);
     memory_region_init_alias(&f->low_smram, OBJECT(d), "smram-low",
-                             f->ram_memory, 0xa0000, 0x20000);
+                             f->ram_memory, SMRAM_C_BASE, SMRAM_C_SIZE);
     memory_region_set_enabled(&f->low_smram, true);
-    memory_region_add_subregion(&f->smram, 0xa0000, &f->low_smram);
+    memory_region_add_subregion(&f->smram, SMRAM_C_BASE, &f->low_smram);
     object_property_add_const_link(qdev_get_machine(), "smram",
                                    OBJECT(&f->smram));
 
Signature · 1 line
2.41.0
Reviewed-by on v1 7/15
#22 lore raw

Re: [PATCH 07/15] hw/pci-host/i440fx: Replace magic values by existing constants

In reply to Bernhard Beschow
Quoted 1 line · On Sun, 11 Jun 2023 12:34:04 +0200 Bernhard Beschow <[email protected]> wrote:
Quoted 31 lines
---
 hw/pci-host/i440fx.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/pci-host/i440fx.c +4 -4
index 61e7b97ff4..daa4d11104 100644
@@ -277,8 +277,8 @@ PCIBus *i440fx_init(const char *pci_type,
 
     /* if *disabled* show SMRAM to all CPUs */
     memory_region_init_alias(&f->smram_region, OBJECT(d), "smram-region",
-                             f->pci_address_space, 0xa0000, 0x20000);
-    memory_region_add_subregion_overlap(f->system_memory, 0xa0000,
+                             f->pci_address_space, SMRAM_C_BASE, SMRAM_C_SIZE);
+    memory_region_add_subregion_overlap(f->system_memory, SMRAM_C_BASE,
                                         &f->smram_region, 1);
     memory_region_set_enabled(&f->smram_region, true);
 
@@ -286,9 +286,9 @@ PCIBus *i440fx_init(const char *pci_type,
     memory_region_init(&f->smram, OBJECT(d), "smram", 4 * GiB);
     memory_region_set_enabled(&f->smram, true);
     memory_region_init_alias(&f->low_smram, OBJECT(d), "smram-low",
-                             f->ram_memory, 0xa0000, 0x20000);
+                             f->ram_memory, SMRAM_C_BASE, SMRAM_C_SIZE);
     memory_region_set_enabled(&f->low_smram, true);
-    memory_region_add_subregion(&f->smram, 0xa0000, &f->low_smram);
+    memory_region_add_subregion(&f->smram, SMRAM_C_BASE, &f->low_smram);
     object_property_add_const_link(qdev_get_machine(), "smram",
                                    OBJECT(&f->smram));
 
#23 lore raw

PATCH 8/15 hw/pci-host/i440fx: Have common names for some local variables

In reply to Bernhard Beschow

PCIHostState is often referred to as phb, own device state usually as s.

---
 hw/pci-host/i440fx.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
hw/pci-host/i440fx.c +12 -14
index daa4d11104..88beaf99c4 100644
@@ -205,28 +205,28 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
 
 static void i440fx_pcihost_initfn(Object *obj)
 {
-    PCIHostState *s = PCI_HOST_BRIDGE(obj);
+    PCIHostState *phb = PCI_HOST_BRIDGE(obj);
 
-    memory_region_init_io(&s->conf_mem, obj, &pci_host_conf_le_ops, s,
+    memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
                           "pci-conf-idx", 4);
-    memory_region_init_io(&s->data_mem, obj, &pci_host_data_le_ops, s,
+    memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
                           "pci-conf-data", 4);
 }
 
 static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
 {
-    PCIHostState *s = PCI_HOST_BRIDGE(dev);
+    PCIHostState *phb = PCI_HOST_BRIDGE(dev);
     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
 
-    memory_region_add_subregion(s->bus->address_space_io, 0xcf8, &s->conf_mem);
+    memory_region_add_subregion(phb->bus->address_space_io, 0xcf8, &phb->conf_mem);
     sysbus_init_ioports(sbd, 0xcf8, 4);
 
-    memory_region_add_subregion(s->bus->address_space_io, 0xcfc, &s->data_mem);
+    memory_region_add_subregion(phb->bus->address_space_io, 0xcfc, &phb->data_mem);
     sysbus_init_ioports(sbd, 0xcfc, 4);
 
     /* register i440fx 0xcf8 port as coalesced pio */
-    memory_region_set_flush_coalesced(&s->data_mem);
-    memory_region_add_coalescing(&s->conf_mem, 0, 4);
+    memory_region_set_flush_coalesced(&phb->data_mem);
+    memory_region_add_coalescing(&phb->conf_mem, 0, 4);
 }
 
 static void i440fx_realize(PCIDevice *dev, Error **errp)
@@ -248,17 +248,16 @@ PCIBus *i440fx_init(const char *pci_type,
                     MemoryRegion *pci_address_space,
                     MemoryRegion *ram_memory)
 {
+    I440FXState *s = I440FX_PCI_HOST_BRIDGE(dev);
+    PCIHostState *phb = PCI_HOST_BRIDGE(dev);
     PCIBus *b;
     PCIDevice *d;
-    PCIHostState *s;
     PCII440FXState *f;
     unsigned i;
-    I440FXState *i440fx;
 
-    s = PCI_HOST_BRIDGE(dev);
     b = pci_root_bus_new(dev, NULL, pci_address_space,
                          address_space_io, 0, TYPE_PCI_BUS);
-    s->bus = b;
+    phb->bus = b;
     object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev));
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
@@ -268,8 +267,7 @@ PCIBus *i440fx_init(const char *pci_type,
     f->pci_address_space = pci_address_space;
     f->ram_memory = ram_memory;
 
-    i440fx = I440FX_PCI_HOST_BRIDGE(dev);
-    range_set_bounds(&i440fx->pci_hole, below_4g_mem_size,
+    range_set_bounds(&s->pci_hole, below_4g_mem_size,
                      IO_APIC_DEFAULT_ADDRESS - 1);
 
     /* setup pci memory mapping */
Signature · 1 line
2.41.0
#25 lore raw

PATCH 9/15 hw/pci-host/i440fx: Move i440fx_realize() into PCII440FXState section

In reply to Bernhard Beschow

i440fx_realize() realizes the PCI device inside the host bridge (PCII440FXState), but is implemented between i440fx_pcihost_realize() and i440fx_init() which deal with the host bridge itself (I440FXState). Since we want to append i440fx_init() to i440fx_pcihost_realize() later let's move i440fx_realize() out of the way.

---
 hw/pci-host/i440fx.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
hw/pci-host/i440fx.c +9 -9
index 88beaf99c4..9df4688b2e 100644
@@ -65,6 +65,15 @@ struct I440FXState {
  */
 #define I440FX_COREBOOT_RAM_SIZE 0x57
 
+static void i440fx_realize(PCIDevice *dev, Error **errp)
+{
+    dev->config[I440FX_SMRAM] = 0x02;
+
+    if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
+        warn_report("i440fx doesn't support emulated iommu");
+    }
+}
+
 static void i440fx_update_memory_mappings(PCII440FXState *d)
 {
     int i;
@@ -229,15 +238,6 @@ static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
     memory_region_add_coalescing(&phb->conf_mem, 0, 4);
 }
 
-static void i440fx_realize(PCIDevice *dev, Error **errp)
-{
-    dev->config[I440FX_SMRAM] = 0x02;
-
-    if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
-        warn_report("i440fx doesn't support emulated iommu");
-    }
-}
-
 PCIBus *i440fx_init(const char *pci_type,
                     DeviceState *dev,
                     MemoryRegion *address_space_mem,
Signature · 1 line
2.41.0
Reviewed-by on v1 9/15
#26 lore raw

Re: [PATCH 09/15] hw/pci-host/i440fx: Move i440fx_realize() into PCII440FXState section

Quoted 5 lines · Bernhard Beschow

i440fx_realize() realizes the PCI device inside the host bridge (PCII440FXState), but is implemented between i440fx_pcihost_realize() and i440fx_init() which deal with the host bridge itself (I440FXState). Since we want to append i440fx_init() to i440fx_pcihost_realize() later let's move i440fx_realize() out of the way.

Yes, i440fx_foo is is PCI PF#0 (physical function) of the i440fx_pcihost_foo (host bridge) device. We could rename i440fx_foo -> i440fx_pf0_foo / i440fx_pcifunc0_foo / i440fx_func0_foo for clarity.

Quoted 4 lines
---
  hw/pci-host/i440fx.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
#27 lore raw

PATCH 10/15 hw/pci-host/i440fx: Make MemoryRegion pointers accessible as properties

In reply to Bernhard Beschow

The goal is to eliminate i440fx_init() which is a legacy init function. This neccessitates the memory regions to be properties, like in Q35, which will be assigned in board code.

Since i440fx needs different PCI devices in Xen mode, and since i440fx shall be self-contained, the PCI device will be created during realization of the host. Thus the pointers need to be moved to the host structure to be usable as properties.

---
 include/hw/pci-host/i440fx.h |  3 ---
 hw/pci-host/i440fx.c         | 44 ++++++++++++++++++++++++++----------
 2 files changed, 32 insertions(+), 15 deletions(-)
include/hw/pci-host/i440fx.h +0 -3
index bf57216c78..e3a550021e 100644
@@ -25,9 +25,6 @@ struct PCII440FXState {
     PCIDevice parent_obj;
     /*< public >*/
 
-    MemoryRegion *system_memory;
-    MemoryRegion *pci_address_space;
-    MemoryRegion *ram_memory;
     PAMMemoryRegion pam_regions[PAM_REGIONS_COUNT];
     MemoryRegion smram_region;
     MemoryRegion smram, low_smram;
hw/pci-host/i440fx.c +32 -12

95 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v1 10/15
#28 lore raw

Re: [PATCH 10/15] hw/pci-host/i440fx: Make MemoryRegion pointers accessible as properties

Quoted 29 lines · Bernhard Beschow

The goal is to eliminate i440fx_init() which is a legacy init function. This neccessitates the memory regions to be properties, like in Q35, which will be assigned in board code.

Since i440fx needs different PCI devices in Xen mode, and since i440fx shall be self-contained, the PCI device will be created during realization of the host. Thus the pointers need to be moved to the host structure to be usable as properties.

---
  include/hw/pci-host/i440fx.h |  3 ---
  hw/pci-host/i440fx.c         | 44 ++++++++++++++++++++++++++----------
  2 files changed, 32 insertions(+), 15 deletions(-)
hw/pci-host/i440fx.c +3 -0
@@ -214,12 +220,25 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
 
  static void i440fx_pcihost_initfn(Object *obj)
  {
+    I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
      PCIHostState *phb = PCI_HOST_BRIDGE(obj);
 
      memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
                            "pci-conf-idx", 4);
      memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
                            "pci-conf-data", 4);
+
+    object_property_add_link(obj, PCI_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,

hw/pci-host/i440fx.c static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v, · line 241

Quoted 10 lines
hw/pci-host/i440fx.c +10 -0
+                             (Object **) &s->ram_memory,
+                             qdev_prop_allow_set_link_before_realize, 0);
+
+    object_property_add_link(obj, PCI_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
+                             (Object **) &s->pci_address_space,
+                             qdev_prop_allow_set_link_before_realize, 0);
+
+    object_property_add_link(obj, PCI_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
+                             (Object **) &s->system_memory,
+                             qdev_prop_allow_set_link_before_realize, 0);

I wonder why we can't simply use device_class_set_props() in i440fx_pcihost_class_init().

Anyhow,

#29 lore raw

PATCH 11/15 hw/pci-host/i440fx: Add PCI_HOST_PROP_IO_MEM property

In reply to Bernhard Beschow

Introduce the property in anticipation of QOM'ification; Q35 has the same property.

---
 hw/pci-host/i440fx.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/pci-host/i440fx.c +10 -4
index 050200cc46..67eeffb421 100644
@@ -27,7 +27,6 @@
 #include "qemu/range.h"
 #include "hw/i386/pc.h"
 #include "hw/pci/pci.h"
-#include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_host.h"
 #include "hw/pci-host/i440fx.h"
 #include "hw/qdev-properties.h"
@@ -51,6 +50,7 @@ struct I440FXState {
     /*< public >*/
 
     MemoryRegion *system_memory;
+    MemoryRegion *address_space_io;
     MemoryRegion *pci_address_space;
     MemoryRegion *ram_memory;
     Range pci_hole;
@@ -239,17 +239,22 @@ static void i440fx_pcihost_initfn(Object *obj)
     object_property_add_link(obj, PCI_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
                              (Object **) &s->system_memory,
                              qdev_prop_allow_set_link_before_realize, 0);
+
+    object_property_add_link(obj, PCI_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
+                             (Object **) &s->address_space_io,
+                             qdev_prop_allow_set_link_before_realize, 0);
 }
 
 static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
 {
+    I440FXState *s = I440FX_PCI_HOST_BRIDGE(dev);
     PCIHostState *phb = PCI_HOST_BRIDGE(dev);
     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
 
-    memory_region_add_subregion(phb->bus->address_space_io, 0xcf8, &phb->conf_mem);
+    memory_region_add_subregion(s->address_space_io, 0xcf8, &phb->conf_mem);
     sysbus_init_ioports(sbd, 0xcf8, 4);
 
-    memory_region_add_subregion(phb->bus->address_space_io, 0xcfc, &phb->data_mem);
+    memory_region_add_subregion(s->address_space_io, 0xcfc, &phb->data_mem);
     sysbus_init_ioports(sbd, 0xcfc, 4);
 
     /* register i440fx 0xcf8 port as coalesced pio */
@@ -275,11 +280,12 @@ PCIBus *i440fx_init(const char *pci_type,
     unsigned i;
 
     s->system_memory = address_space_mem;
+    s->address_space_io = address_space_io;
     s->pci_address_space = pci_address_space;
     s->ram_memory = ram_memory;
 
     b = pci_root_bus_new(dev, NULL, s->pci_address_space,
-                         address_space_io, 0, TYPE_PCI_BUS);
+                         s->address_space_io, 0, TYPE_PCI_BUS);
     phb->bus = b;
     object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev));
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
Signature · 1 line
2.41.0
#30 lore raw

Re: [PATCH 11/15] hw/pci-host/i440fx: Add PCI_HOST_PROP_IO_MEM property

Quoted 10 lines · Bernhard Beschow

Introduce the property in anticipation of QOM'ification; Q35 has the same property.

---
  hw/pci-host/i440fx.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)

hw/pci-host/i440fx.c struct I440FXState { · line 53

Quoted 4 lines
hw/pci-host/i440fx.c +1 -0
@@ -51,6 +50,7 @@ struct I440FXState {
      /*< public >*/
 
      MemoryRegion *system_memory;
+    MemoryRegion *address_space_io;

"io_mem" like the property? (this is not an AddressSpace)

hw/pci-host/i440fx.c struct I440FXState { · line 54

Quoted 1 line
hw/pci-host/i440fx.c +0 -0
      MemoryRegion *pci_address_space;

(pre-existing misleading name)

Quoted 2 lines
hw/pci-host/i440fx.c +0 -0
      MemoryRegion *ram_memory;
      Range pci_hole;
#31 lore raw

Re: [PATCH 11/15] hw/pci-host/i440fx: Add PCI_HOST_PROP_IO_MEM property

Am 12. Juni 2023 10:31:48 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 10 lines · Bernhard Beschow
Quoted 7 lines

Introduce the property in anticipation of QOM'ification; Q35 has the same property.

---
  hw/pci-host/i440fx.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
Quoted 6 lines

hw/pci-host/i440fx.c struct I440FXState { · line 52

Quoted 4 lines
hw/pci-host/i440fx.c +1 -0
@@ -51,6 +50,7 @@ struct I440FXState {
      /*< public >*/
        MemoryRegion *system_memory;
+    MemoryRegion *address_space_io;

"io_mem" like the property? (this is not an AddressSpace)

Agreed. I'd name it "io_memory" to be consistent with above attribute.

Best regards,
Bernhard

Quoted 8 lines

hw/pci-host/i440fx.c struct I440FXState { · line 54

Quoted 2 lines
hw/pci-host/i440fx.c +0 -0
      MemoryRegion *pci_address_space;

(pre-existing misleading name)

Quoted 2 lines
hw/pci-host/i440fx.c +0 -0
      MemoryRegion *ram_memory;
      Range pci_hole;
#32 lore raw

PATCH 12/15 hw/pci-host/i440fx: Add PCI_HOST_{ABOVE, BELOW}_4G_MEM_SIZE properties

In reply to Bernhard Beschow

Introduce the properties in anticipation of QOM'ification; Q35 has the same properties.

Note that we want to avoid a "ram size" property in the QOM interface since it seems redundant to both properties introduced in this change. Thus the removal of the ram_size parameter. We assume the invariant of both properties to sum up to "ram size" which is already asserted in pc_memory_init(). Under Xen the invariant seems to hold as well, so we now also check it there.

---
 include/hw/pci-host/i440fx.h |  1 -
 hw/i386/pc_piix.c            |  5 ++++-
 hw/pci-host/i440fx.c         | 12 ++++++++++--
 3 files changed, 14 insertions(+), 4 deletions(-)
include/hw/pci-host/i440fx.h +0 -1
index e3a550021e..7e38456ebb 100644
@@ -36,7 +36,6 @@ PCIBus *i440fx_init(const char *pci_type,
                     DeviceState *dev,
                     MemoryRegion *address_space_mem,
                     MemoryRegion *address_space_io,
-                    ram_addr_t ram_size,
                     ram_addr_t below_4g_mem_size,
                     ram_addr_t above_4g_mem_size,
                     MemoryRegion *pci_memory,
hw/i386/pc_piix.c +4 -1
index 42af03dbb4..f9141536cf 100644
@@ -227,6 +227,9 @@ static void pc_init1(MachineState *machine,
     if (!xen_enabled()) {
         pc_memory_init(pcms, system_memory, rom_memory, hole64_size);
     } else {
+        assert(machine->ram_size == x86ms->below_4g_mem_size +
+                                    x86ms->above_4g_mem_size);
+
         pc_system_flash_cleanup_unused(pcms);
         if (machine->kernel_filename != NULL) {
             /* For xen HVM direct kernel boot, load linux here */
@@ -242,7 +245,7 @@ static void pc_init1(MachineState *machine,
 
         pci_bus = i440fx_init(pci_type,
                               i440fx_host,
-                              system_memory, system_io, machine->ram_size,
+                              system_memory, system_io,
                               x86ms->below_4g_mem_size,
                               x86ms->above_4g_mem_size,
                               pci_memory, ram_memory);
hw/pci-host/i440fx.c +10 -2
index 67eeffb421..3c44f24d2a 100644
@@ -54,6 +54,8 @@ struct I440FXState {
     MemoryRegion *pci_address_space;
     MemoryRegion *ram_memory;
     Range pci_hole;
+    uint64_t below_4g_mem_size;
+    uint64_t above_4g_mem_size;
     uint64_t pci_hole64_size;
     bool pci_hole64_fix;
     uint32_t short_root_bus;
@@ -266,7 +268,6 @@ PCIBus *i440fx_init(const char *pci_type,
                     DeviceState *dev,
                     MemoryRegion *address_space_mem,
                     MemoryRegion *address_space_io,
-                    ram_addr_t ram_size,
                     ram_addr_t below_4g_mem_size,
                     ram_addr_t above_4g_mem_size,
                     MemoryRegion *pci_address_space,
@@ -283,6 +284,8 @@ PCIBus *i440fx_init(const char *pci_type,
     s->address_space_io = address_space_io;
     s->pci_address_space = pci_address_space;
     s->ram_memory = ram_memory;
+    s->below_4g_mem_size = below_4g_mem_size;
+    s->above_4g_mem_size = above_4g_mem_size;
 
     b = pci_root_bus_new(dev, NULL, s->pci_address_space,
                          s->address_space_io, 0, TYPE_PCI_BUS);
@@ -293,7 +296,7 @@ PCIBus *i440fx_init(const char *pci_type,
     d = pci_create_simple(b, 0, pci_type);
     f = I440FX_PCI_DEVICE(d);
 
-    range_set_bounds(&s->pci_hole, below_4g_mem_size,
+    range_set_bounds(&s->pci_hole, s->below_4g_mem_size,
                      IO_APIC_DEFAULT_ADDRESS - 1);
 
     /* setup pci memory mapping */
@@ -324,6 +327,7 @@ PCIBus *i440fx_init(const char *pci_type,
                  PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
     }
 
+    ram_addr_t ram_size = s->below_4g_mem_size + s->above_4g_mem_size;
     ram_size = ram_size / 8 / 1024 / 1024;
     if (ram_size > 255) {
         ram_size = 255;
@@ -383,6 +387,10 @@ static Property i440fx_props[] = {
     DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, I440FXState,
                      pci_hole64_size, I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT),
     DEFINE_PROP_UINT32("short_root_bus", I440FXState, short_root_bus, 0),
+    DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MEM_SIZE, I440FXState,
+                     below_4g_mem_size, 0),
+    DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, I440FXState,
+                     above_4g_mem_size, 0),
     DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState, pci_hole64_fix, true),
     DEFINE_PROP_END_OF_LIST(),
 };
Signature · 1 line
2.41.0
#33 lore raw

PATCH 13/15 hw/pci-host/i440fx: Add I440FX_HOST_PROP_PCI_TYPE property

In reply to Bernhard Beschow

I440FX needs a different PCI device model if the "igd-passthru" property is enabled. The type name is currently passed as a parameter to i440fx_init(). This parameter will be replaced by a property assignment once i440fx_init() gets resolved.

---
 include/hw/pci-host/i440fx.h | 2 ++
 hw/pci-host/i440fx.c         | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/hw/pci-host/i440fx.h +2 -0
index 7e38456ebb..2d7bae5a45 100644
@@ -15,6 +15,8 @@
 #include "hw/pci-host/pam.h"
 #include "qom/object.h"
 
+#define I440FX_HOST_PROP_PCI_TYPE "pci-type"
+
 #define TYPE_I440FX_PCI_HOST_BRIDGE "i440FX-pcihost"
 #define TYPE_I440FX_PCI_DEVICE "i440FX"
 
hw/pci-host/i440fx.c +5 -1
index 3c44f24d2a..44ff3f7ea0 100644
@@ -59,6 +59,8 @@ struct I440FXState {
     uint64_t pci_hole64_size;
     bool pci_hole64_fix;
     uint32_t short_root_bus;
+
+    char *pci_type;
 };
 
 #define I440FX_PAM      0x59
@@ -286,6 +288,7 @@ PCIBus *i440fx_init(const char *pci_type,
     s->ram_memory = ram_memory;
     s->below_4g_mem_size = below_4g_mem_size;
     s->above_4g_mem_size = above_4g_mem_size;
+    s->pci_type = (char *)pci_type;
 
     b = pci_root_bus_new(dev, NULL, s->pci_address_space,
                          s->address_space_io, 0, TYPE_PCI_BUS);
@@ -293,7 +296,7 @@ PCIBus *i440fx_init(const char *pci_type,
     object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev));
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
-    d = pci_create_simple(b, 0, pci_type);
+    d = pci_create_simple(b, 0, s->pci_type);
     f = I440FX_PCI_DEVICE(d);
 
     range_set_bounds(&s->pci_hole, s->below_4g_mem_size,
@@ -392,6 +395,7 @@ static Property i440fx_props[] = {
     DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, I440FXState,
                      above_4g_mem_size, 0),
     DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState, pci_hole64_fix, true),
+    DEFINE_PROP_STRING(I440FX_HOST_PROP_PCI_TYPE, I440FXState, pci_type),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.41.0
#34 lore raw

PATCH 14/15 hw/pci-host/i440fx: Resolve i440fx_init()

In reply to Bernhard Beschow

i440fx_init() is a legacy init function. The previous patches worked towards TYPE_I440FX_PCI_HOST_BRIDGE to be instantiated the QOM way. Do this now by transforming the parameters passed to i440fx_init() into property assignments.

---
 include/hw/pci-host/i440fx.h | 10 ----------
 hw/i386/pc_piix.c            | 30 +++++++++++++++++++++---------
 hw/pci-host/i440fx.c         | 34 +++++-----------------------------
 3 files changed, 26 insertions(+), 48 deletions(-)
include/hw/pci-host/i440fx.h +0 -10
index 2d7bae5a45..c988f70890 100644
@@ -34,14 +34,4 @@ struct PCII440FXState {
 
 #define TYPE_IGD_PASSTHROUGH_I440FX_PCI_DEVICE "igd-passthrough-i440FX"
 
-PCIBus *i440fx_init(const char *pci_type,
-                    DeviceState *dev,
-                    MemoryRegion *address_space_mem,
-                    MemoryRegion *address_space_io,
-                    ram_addr_t below_4g_mem_size,
-                    ram_addr_t above_4g_mem_size,
-                    MemoryRegion *pci_memory,
-                    MemoryRegion *ram_memory);
-
-
 #endif
hw/i386/pc_piix.c +21 -9
index f9141536cf..22173b122b 100644
@@ -126,7 +126,7 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    DeviceState *i440fx_host;
+    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -201,8 +201,8 @@ static void pc_init1(MachineState *machine,
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = qdev_new(host_type);
-        hole64_size = object_property_get_uint(OBJECT(i440fx_host),
+        i440fx_host = OBJECT(qdev_new(host_type));
+        hole64_size = object_property_get_uint(i440fx_host,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);
     } else {
@@ -243,12 +243,24 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        pci_bus = i440fx_init(pci_type,
-                              i440fx_host,
-                              system_memory, system_io,
-                              x86ms->below_4g_mem_size,
-                              x86ms->above_4g_mem_size,
-                              pci_memory, ram_memory);
+        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
+        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
+                                pci_type, &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
         pci_bus_map_irqs(pci_bus,
                          xen_enabled() ? xen_pci_slot_get_pirq
                                        : pc_pci_slot_get_pirq);
hw/pci-host/i440fx.c +5 -29
index 44ff3f7ea0..e1708bf2c9 100644
@@ -251,9 +251,14 @@ static void i440fx_pcihost_initfn(Object *obj)
 
 static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
 {
+    ERRP_GUARD();
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(dev);
     PCIHostState *phb = PCI_HOST_BRIDGE(dev);
     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    PCIBus *b;
+    PCIDevice *d;
+    PCII440FXState *f;
+    unsigned i;
 
     memory_region_add_subregion(s->address_space_io, 0xcf8, &phb->conf_mem);
     sysbus_init_ioports(sbd, 0xcf8, 4);
@@ -264,37 +269,10 @@ static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
     /* register i440fx 0xcf8 port as coalesced pio */
     memory_region_set_flush_coalesced(&phb->data_mem);
     memory_region_add_coalescing(&phb->conf_mem, 0, 4);
-}
-
-PCIBus *i440fx_init(const char *pci_type,
-                    DeviceState *dev,
-                    MemoryRegion *address_space_mem,
-                    MemoryRegion *address_space_io,
-                    ram_addr_t below_4g_mem_size,
-                    ram_addr_t above_4g_mem_size,
-                    MemoryRegion *pci_address_space,
-                    MemoryRegion *ram_memory)
-{
-    I440FXState *s = I440FX_PCI_HOST_BRIDGE(dev);
-    PCIHostState *phb = PCI_HOST_BRIDGE(dev);
-    PCIBus *b;
-    PCIDevice *d;
-    PCII440FXState *f;
-    unsigned i;
-
-    s->system_memory = address_space_mem;
-    s->address_space_io = address_space_io;
-    s->pci_address_space = pci_address_space;
-    s->ram_memory = ram_memory;
-    s->below_4g_mem_size = below_4g_mem_size;
-    s->above_4g_mem_size = above_4g_mem_size;
-    s->pci_type = (char *)pci_type;
 
     b = pci_root_bus_new(dev, NULL, s->pci_address_space,
                          s->address_space_io, 0, TYPE_PCI_BUS);
     phb->bus = b;
-    object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev));
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
     d = pci_create_simple(b, 0, s->pci_type);
     f = I440FX_PCI_DEVICE(d);
@@ -338,8 +316,6 @@ PCIBus *i440fx_init(const char *pci_type,
     d->config[I440FX_COREBOOT_RAM_SIZE] = ram_size;
 
     i440fx_update_memory_mappings(f);
-
-    return b;
 }
 
 static void i440fx_class_init(ObjectClass *klass, void *data)
Signature · 1 line
2.41.0
#35 lore raw

PATCH 15/15 hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

In reply to Bernhard Beschow

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +29 -28
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);
     } else {
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;
     }
 
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);
 
Signature · 1 line
2.41.0
Reviewed-by on v1 15/15
#36 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

Quoted 6 lines · Bernhard Beschow

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Quoted 8 lines

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
  hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
  1 file changed, 29 insertions(+), 28 deletions(-)
#37 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

In reply to Bernhard Beschow
Quoted 56 lines · On Sun, 11 Jun 2023 12:34:12 +0200 Bernhard Beschow <[email protected]> wrote:

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +20 -3
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 232

Quoted 10 lines
hw/i386/pc_piix.c +8 -0
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);

before patch memory region links were set after the original regions were initialized by pc_memory_init(), but after this patch you 1st set links and only later pc_memory_init(). I doesn't look to me as a safe thing to do.

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 237

Quoted 8 lines
hw/i386/pc_piix.c +1 -1
     } else {
 
 
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;

is it possible to turn these into initializers, and get rid of 'else' branch?

Quoted 40 lines
     }
hw/i386/pc_piix.c +0 -24
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);
 
#38 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

Quoted 66 lines · On Mon, 12 Jun 2023 16:51:55 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 63 lines · On Sun, 11 Jun 2023 12:34:12 +0200 Bernhard Beschow <[email protected]> wrote:

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +27 -3
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 232

Quoted 3 lines
hw/i386/pc_piix.c +1 -0
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);  

before patch memory region links were set after the original regions were initialized by pc_memory_init(), but after this patch you 1st set links and only later pc_memory_init(). I doesn't look to me as a safe thing to do.

or maybe it doesn't matter, but still I have hard time convincing myself that it is so.

Quoted 54 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 237

Quoted 9 lines
hw/i386/pc_piix.c +1 -1
     } else {  
 
 
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;  

is it possible to turn these into initializers, and get rid of 'else' branch?

Quoted 40 lines
     }
hw/i386/pc_piix.c +0 -24
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);
 
#39 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

Am 12. Juni 2023 15:21:19 UTC schrieb Igor Mammedov <[email protected]>:

Quoted 78 lines
Quoted 66 lines · On Mon, 12 Jun 2023 16:51:55 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 63 lines · On Sun, 11 Jun 2023 12:34:12 +0200 Bernhard Beschow <[email protected]> wrote:

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +27 -3
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 232

Quoted 3 lines
hw/i386/pc_piix.c +1 -0
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);  

before patch memory region links were set after the original regions were initialized by pc_memory_init(), but after this patch you 1st set links and only later pc_memory_init(). I doesn't look to me as a safe thing to do.

Quoted 2 lines

or maybe it doesn't matter, but still I have hard time convincing myself that it is so.

AFAICS both pc_memory_init() and i440fx_pcihost_realize() rely on memory_region_init*() having been called on these pointers already. All they seem to do is adding their sub regions. The order in which this happens seems to be irrelevant, otherwise we'd see changes in the QOM console calls I guess.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
hw/i386/pc_piix.c +0 -0
     } else {  
Quoted 8 lines
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 237

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;  

is it possible to turn these into initializers, and get rid of 'else' branch?

Sure, this is possible. I'd add another patch before this one.

Best regards,
Bernhard

Quoted 43 lines
Quoted 42 lines
Quoted 41 lines
hw/i386/pc_piix.c +0 -24
     }
 
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);
 
#40 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

Quoted 79 lines · On Mon, 12 Jun 2023 17:49:10 +0000 Bernhard Beschow <[email protected]> wrote:

Am 12. Juni 2023 15:21:19 UTC schrieb Igor Mammedov <[email protected]>:

Quoted 78 lines
Quoted 74 lines · On Mon, 12 Jun 2023 16:51:55 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 56 lines · On Sun, 11 Jun 2023 12:34:12 +0200 Bernhard Beschow <[email protected]> wrote:

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +20 -3
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 232

Quoted 10 lines
hw/i386/pc_piix.c +8 -0
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);    

before patch memory region links were set after the original regions were initialized by pc_memory_init(), but after this patch you 1st set links and only later pc_memory_init(). I doesn't look to me as a safe thing to do.

Quoted 4 lines
Quoted 2 lines

or maybe it doesn't matter, but still I have hard time convincing myself that it is so.

AFAICS both pc_memory_init() and i440fx_pcihost_realize() rely on memory_region_init*() having been called on these pointers already. All they seem to do is adding their sub regions. The order in which this happens seems to be irrelevant, otherwise we'd see changes in the QOM console calls I guess.

that's why I said it might not matter, but ...
the thing is that now mapping into AS happens in reversed order and with overlapped mappings reversed I'm quite unsure if that is correct.

Quoted 63 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
hw/i386/pc_piix.c +0 -0
     } else {    
Quoted 8 lines
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 237

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;    

is it possible to turn these into initializers, and get rid of 'else' branch?

Sure, this is possible. I'd add another patch before this one.

Best regards,
Bernhard

Quoted 43 lines
Quoted 42 lines
Quoted 41 lines
hw/i386/pc_piix.c +0 -24
     }
 
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);
 
#41 lore raw

Re: [PATCH 15/15] hw/i386/pc_piix: Move i440fx' realize near its qdev_new()

Am 13. Juni 2023 09:52:50 UTC schrieb Igor Mammedov <[email protected]>:

Quoted 85 lines
Quoted 82 lines · On Mon, 12 Jun 2023 17:49:10 +0000 Bernhard Beschow <[email protected]> wrote:

Am 12. Juni 2023 15:21:19 UTC schrieb Igor Mammedov <[email protected]>:

Quoted 80 lines
Quoted 66 lines · On Mon, 12 Jun 2023 16:51:55 +0200 Igor Mammedov <[email protected]> wrote:
Quoted 63 lines · On Sun, 11 Jun 2023 12:34:12 +0200 Bernhard Beschow <[email protected]> wrote:

I440FX realization is currently mixed with PIIX3 creation. Furthermore, it is common practice to only set properties between a device's qdev_new() and qdev_realize(). Clean up to resolve both issues.

Since I440FX spawns a PCI bus let's also move the pci_bus initialization there.

Note that when running qemu-system-x86_64 -M pc -S before and after this patch, info mtree in the QEMU console doesn't show any differences except that the ordering is different.

---
 hw/i386/pc_piix.c | 57 ++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
hw/i386/pc_piix.c +27 -3
index 22173b122b..23b9725c94 100644
@@ -126,7 +126,6 @@ static void pc_init1(MachineState *machine,
     MemoryRegion *rom_memory;
     ram_addr_t lowmem;
     uint64_t hole64_size;
-    Object *i440fx_host;
 
     /*
      * Calculate ram split, for memory below and above 4G.  It's a bit
@@ -198,17 +197,43 @@ static void pc_init1(MachineState *machine,
     }
 
     if (pcmc->pci_enabled) {
+        Object *phb;
+
         pci_memory = g_new(MemoryRegion, 1);
         memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);
         rom_memory = pci_memory;
-        i440fx_host = OBJECT(qdev_new(host_type));
-        hole64_size = object_property_get_uint(i440fx_host,
+
+        phb = OBJECT(qdev_new(host_type));
+        object_property_add_child(OBJECT(machine), "i440fx", phb);
+        object_property_set_link(phb, PCI_HOST_PROP_RAM_MEM,
+                                 OBJECT(ram_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_PCI_MEM,
+                                 OBJECT(pci_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_SYSTEM_MEM,
+                                 OBJECT(system_memory), &error_fatal);
+        object_property_set_link(phb, PCI_HOST_PROP_IO_MEM,
+                                 OBJECT(system_io), &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_BELOW_4G_MEM_SIZE,
+                                 x86ms->below_4g_mem_size, &error_fatal);
+        object_property_set_uint(phb, PCI_HOST_ABOVE_4G_MEM_SIZE,
+                                 x86ms->above_4g_mem_size, &error_fatal);
+        object_property_set_str(phb, I440FX_HOST_PROP_PCI_TYPE, pci_type,
+                                &error_fatal);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(phb), &error_fatal);
+
+        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pci.0"));
+        pci_bus_map_irqs(pci_bus,
+                         xen_enabled() ? xen_pci_slot_get_pirq
+                                       : pc_pci_slot_get_pirq);
+        pcms->bus = pci_bus;
+
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 232

Quoted 3 lines
hw/i386/pc_piix.c +1 -0
+        hole64_size = object_property_get_uint(phb,
                                                PCI_HOST_PROP_PCI_HOLE64_SIZE,
                                                &error_abort);    

before patch memory region links were set after the original regions were initialized by pc_memory_init(), but after this patch you 1st set links and only later pc_memory_init(). I doesn't look to me as a safe thing to do.

or maybe it doesn't matter, but still I have hard time convincing myself that it is so.

Quoted 6 lines
Quoted 1 line

AFAICS both pc_memory_init() and i440fx_pcihost_realize() rely on memory_region_init*() having been called on these pointers already. All they seem to do is adding their sub regions. The order in which this happens seems to be irrelevant, otherwise we'd see changes in the QOM console calls I guess.

that's why I said it might not matter, but ...
the thing is that now mapping into AS happens in reversed order and with overlapped mappings reversed I'm quite unsure if that is correct.

Hi Igor,

sorry for the late answer. I think I have missed your reply so far due to KVM forum ;)

The order in which the overlapped mappings are added shouldn't matter as long as different priorities are supplied. AFAIR adding overlapping regions with the same priority would be a programming mistake (in existing code). To rule this out I compared the memory mappings before and after the patch and put the result in the commit message. It was the same for info mtree -f: no difference except the order of the printout.

I might be able to send an updated version of this series later today. If you'd have further comments after it is out we can continue discussing there.

Best regards,
Bernhard

Quoted 65 lines
Quoted 64 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
hw/i386/pc_piix.c +0 -0
     } else {    
Quoted 8 lines
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 237

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
         pci_memory = NULL;
         rom_memory = system_memory;
-        i440fx_host = NULL;
+        pci_bus = NULL;
         hole64_size = 0;    

is it possible to turn these into initializers, and get rid of 'else' branch?

Sure, this is possible. I'd add another patch before this one.

Best regards,
Bernhard

Quoted 43 lines
Quoted 42 lines
Quoted 41 lines
hw/i386/pc_piix.c +0 -24
     }
 
@@ -243,29 +268,6 @@ static void pc_init1(MachineState *machine,
         PIIX3State *piix3;
         PCIDevice *pci_dev;
 
-        object_property_add_child(OBJECT(machine), "i440fx", i440fx_host);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_RAM_MEM,
-                                 OBJECT(ram_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_PCI_MEM,
-                                 OBJECT(pci_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_SYSTEM_MEM,
-                                 OBJECT(system_memory), &error_fatal);
-        object_property_set_link(i440fx_host, PCI_HOST_PROP_IO_MEM,
-                                 OBJECT(system_io), &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_BELOW_4G_MEM_SIZE,
-                                 x86ms->below_4g_mem_size, &error_fatal);
-        object_property_set_uint(i440fx_host, PCI_HOST_ABOVE_4G_MEM_SIZE,
-                                 x86ms->above_4g_mem_size, &error_fatal);
-        object_property_set_str(i440fx_host, I440FX_HOST_PROP_PCI_TYPE,
-                                pci_type, &error_fatal);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(i440fx_host), &error_fatal);
-
-        pci_bus = PCI_BUS(qdev_get_child_bus(DEVICE(i440fx_host), "pci.0"));
-        pci_bus_map_irqs(pci_bus,
-                         xen_enabled() ? xen_pci_slot_get_pirq
-                                       : pc_pci_slot_get_pirq);
-        pcms->bus = pci_bus;
-
         pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
                                                   TYPE_PIIX3_DEVICE);
 
@@ -290,7 +292,6 @@ static void pc_init1(MachineState *machine,
         rtc_state = ISA_DEVICE(object_resolve_path_component(OBJECT(pci_dev),
                                                              "rtc"));
     } else {
-        pci_bus = NULL;
         isa_bus = isa_bus_new(NULL, system_memory, system_io,
                               &error_abort);