Skip to contentSkip to conversation

hw/ide/ahci: Housekeeping

PATCH In review Philippe Mathieu-Daudé 4 participants 23 messages Feb 13, 2024 – Feb 15, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover hw/ide/ahci: Housekeeping

- Split 'ahci.h' as sysbus / pci
- Inline ahci_get_num_ports()
- Directly use AHCIState::ports instead of SysbusAHCIState::num_ports

Philippe Mathieu-Daudé (9):
hw/i386/q35: Use DEVICE() cast macro with PCIDevice object hw/ide/ahci: Expose AHCIPCIState structure hw/ide/ahci: Rename AHCI PCI function as 'pdev' hw/ide/ahci: Inline ahci_get_num_ports() hw/ide/ahci: Pass AHCI context to ahci_ide_create_devs() hw/ide/ahci: Convert AHCIState::ports to unsigned hw/ide/ahci: Do not pass 'ports' argument to ahci_realize() hw/ide/ahci: Remove SysbusAHCIState::num_ports field hw/ide/ahci: Move SysBus definitions to 'ahci-sysbus.h'

 hw/ide/ahci_internal.h         | 10 +--------
 include/hw/arm/allwinner-a10.h |  2 +-
 include/hw/arm/allwinner-r40.h |  2 +-
 include/hw/arm/xlnx-zynqmp.h   |  2 +-
 include/hw/ide/ahci-pci.h      | 22 ++++++++++++++++++++
 include/hw/ide/ahci-sysbus.h   | 35 +++++++++++++++++++++++++++++++
 include/hw/ide/ahci.h          | 38 +++-------------------------------
 hw/arm/highbank.c              |  2 +-
 hw/arm/sbsa-ref.c              |  1 +
 hw/i386/pc_q35.c               | 19 ++++++++++-------
 hw/ide/ahci-allwinner.c        |  3 +--
 hw/ide/ahci.c                  | 29 +++++++++-----------------
 hw/ide/ich.c                   |  4 +++-
 hw/mips/boston.c               | 14 +++++++------
 14 files changed, 99 insertions(+), 84 deletions(-)
 create mode 100644 include/hw/ide/ahci-pci.h
 create mode 100644 include/hw/ide/ahci-sysbus.h
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/9 hw/i386/q35: Use DEVICE() cast macro with PCIDevice object

QDev API provides the DEVICE() macro to access the 'qdev' parent field of the PCIDevice structure.

---
 hw/i386/pc_q35.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/pc_q35.c +2 -2
index 7ca3f465e0..33a4413708 100644
@@ -309,8 +309,8 @@ static void pc_q35_init(MachineState *machine)
                                                PCI_DEVFN(ICH9_SATA1_DEV,
                                                          ICH9_SATA1_FUNC),
                                                "ich9-ahci");
-        idebus[0] = qdev_get_child_bus(&ahci->qdev, "ide.0");
-        idebus[1] = qdev_get_child_bus(&ahci->qdev, "ide.1");
+        idebus[0] = qdev_get_child_bus(DEVICE(ahci), "ide.0");
+        idebus[1] = qdev_get_child_bus(DEVICE(ahci), "ide.1");
         g_assert(MAX_SATA_PORTS == ahci_get_num_ports(ahci));
         ide_drive_get(hd, ahci_get_num_ports(ahci));
         ahci_ide_create_devs(ahci, hd);
Signature · 1 line
2.41.0
#3 lore raw

PATCH 2/9 hw/ide/ahci: Expose AHCIPCIState structure

In reply to Philippe Mathieu-Daudé

In order to be able to QOM-embed a structure, we need its full definition. Move it from "ahci_internal.h" to the new "hw/ide/ahci-pci.h" header.

---
 hw/ide/ahci_internal.h    |  8 --------
 include/hw/ide/ahci-pci.h | 22 ++++++++++++++++++++++
 include/hw/ide/ahci.h     |  3 ---
 hw/i386/pc_q35.c          |  2 +-
 hw/ide/ahci.c             |  1 +
 hw/ide/ich.c              |  1 +
 hw/mips/boston.c          |  2 +-
 7 files changed, 26 insertions(+), 13 deletions(-)
 create mode 100644 include/hw/ide/ahci-pci.h
hw/ide/ahci_internal.h +0 -8
index c244bbd8be..4dc2805d21 100644
@@ -324,14 +324,6 @@ struct AHCIDevice {
     MemReentrancyGuard mem_reentrancy_guard;
 };
 
-struct AHCIPCIState {
-    /*< private >*/
-    PCIDevice parent_obj;
-    /*< public >*/
-
-    AHCIState ahci;
-};
-
 extern const VMStateDescription vmstate_ahci;
 
 #define VMSTATE_AHCI(_field, _state) {                               \
include/hw/ide/ahci-pci.h new file +22 -0
new file mode 100644
index 0000000000..c2ee616962
@@ -0,0 +1,22 @@
+/*
+ * QEMU AHCI Emulation (PCI devices)
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_IDE_AHCI_PCI_H
+#define HW_IDE_AHCI_PCI_H
+
+#include "qom/object.h"
+#include "hw/ide/ahci.h"
+#include "hw/pci/pci_device.h"
+
+#define TYPE_ICH9_AHCI "ich9-ahci"
+OBJECT_DECLARE_SIMPLE_TYPE(AHCIPCIState, ICH9_AHCI)
+
+struct AHCIPCIState {
+    PCIDevice parent_obj;
+
+    AHCIState ahci;
+};
+
+#endif
include/hw/ide/ahci.h +0 -3
index 210e5e734c..6818d02063 100644
@@ -52,9 +52,6 @@ typedef struct AHCIState {
 } AHCIState;
 
 
-#define TYPE_ICH9_AHCI "ich9-ahci"
-OBJECT_DECLARE_SIMPLE_TYPE(AHCIPCIState, ICH9_AHCI)
-
 int32_t ahci_get_num_ports(PCIDevice *dev);
 void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd);
 
hw/i386/pc_q35.c +1 -1
index 33a4413708..ace8d3839a 100644
@@ -47,7 +47,7 @@
 #include "hw/display/ramfb.h"
 #include "hw/firmware/smbios.h"
 #include "hw/ide/pci.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-pci.h"
 #include "hw/intc/ioapic.h"
 #include "hw/southbridge/ich9.h"
 #include "hw/usb.h"
hw/ide/ahci.c +1 -0
index 0eb83a6d46..aa9381a7b2 100644
@@ -36,6 +36,7 @@
 #include "sysemu/dma.h"
 #include "hw/ide/internal.h"
 #include "hw/ide/pci.h"
+#include "hw/ide/ahci-pci.h"
 #include "ahci_internal.h"
 
 #include "trace.h"
hw/ide/ich.c +1 -0
index 49f8eb8a7d..d190012a95 100644
@@ -69,6 +69,7 @@
 #include "hw/isa/isa.h"
 #include "sysemu/dma.h"
 #include "hw/ide/pci.h"
+#include "hw/ide/ahci-pci.h"
 #include "ahci_internal.h"
 
 #define ICH9_MSI_CAP_OFFSET     0x80
hw/mips/boston.c +1 -1
index 4e11ff6cd6..cbcefdd693 100644
@@ -24,7 +24,7 @@
 #include "hw/boards.h"
 #include "hw/char/serial.h"
 #include "hw/ide/pci.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-pci.h"
 #include "hw/loader.h"
 #include "hw/loader-fit.h"
 #include "hw/mips/bootloader.h"
Signature · 1 line
2.41.0
Reviewed-by on v1 2/9
#4 lore raw

Re: [PATCH 2/9] hw/ide/ahci: Expose AHCIPCIState structure

Quoted 14 lines · Philippe Mathieu-Daudé

In order to be able to QOM-embed a structure, we need its full definition. Move it from "ahci_internal.h" to the new "hw/ide/ahci-pci.h" header.

---
  hw/ide/ahci_internal.h    |  8 --------
  include/hw/ide/ahci-pci.h | 22 ++++++++++++++++++++++
  include/hw/ide/ahci.h     |  3 ---
  hw/i386/pc_q35.c          |  2 +-
  hw/ide/ahci.c             |  1 +
  hw/ide/ich.c              |  1 +
  hw/mips/boston.c          |  2 +-
  7 files changed, 26 insertions(+), 13 deletions(-)
Quoted 1 line

create mode 100644 include/hw/ide/ahci-pci.h

r~

#5 lore raw

PATCH 3/9 hw/ide/ahci: Rename AHCI PCI function as 'pdev'

In reply to Philippe Mathieu-Daudé

We want to access AHCIPCIState::ahci field. In order to keep the code simple (avoiding &ahci->ahci), rename the current 'ahci' variable as 'pdev'

---
 hw/i386/pc_q35.c | 15 ++++++++-------
 hw/mips/boston.c | 10 +++++-----
 2 files changed, 13 insertions(+), 12 deletions(-)
hw/i386/pc_q35.c +8 -7
index ace8d3839a..e298f4ff32 100644
@@ -135,7 +135,6 @@ static void pc_q35_init(MachineState *machine)
     GSIState *gsi_state;
     ISABus *isa_bus;
     int i;
-    PCIDevice *ahci;
     ram_addr_t lowmem;
     DriveInfo *hd[MAX_SATA_PORTS];
     MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -304,16 +303,18 @@ static void pc_q35_init(MachineState *machine)
                          0xff0104);
 
     if (pcms->sata_enabled) {
+        PCIDevice *pdev;
+
         /* ahci and SATA device, for q35 1 ahci controller is built-in */
-        ahci = pci_create_simple_multifunction(host_bus,
+        pdev = pci_create_simple_multifunction(host_bus,
                                                PCI_DEVFN(ICH9_SATA1_DEV,
                                                          ICH9_SATA1_FUNC),
                                                "ich9-ahci");
-        idebus[0] = qdev_get_child_bus(DEVICE(ahci), "ide.0");
-        idebus[1] = qdev_get_child_bus(DEVICE(ahci), "ide.1");
-        g_assert(MAX_SATA_PORTS == ahci_get_num_ports(ahci));
-        ide_drive_get(hd, ahci_get_num_ports(ahci));
-        ahci_ide_create_devs(ahci, hd);
+        idebus[0] = qdev_get_child_bus(DEVICE(pdev), "ide.0");
+        idebus[1] = qdev_get_child_bus(DEVICE(pdev), "ide.1");
+        g_assert(MAX_SATA_PORTS == ahci_get_num_ports(pdev));
+        ide_drive_get(hd, ahci_get_num_ports(pdev));
+        ahci_ide_create_devs(pdev, hd);
     } else {
         idebus[0] = idebus[1] = NULL;
     }
hw/mips/boston.c +5 -5
index cbcefdd693..0ec0b98066 100644
@@ -677,7 +677,7 @@ static void boston_mach_init(MachineState *machine)
     MemoryRegion *flash, *ddr_low_alias, *lcd, *platreg;
     MemoryRegion *sys_mem = get_system_memory();
     XilinxPCIEHost *pcie2;
-    PCIDevice *ahci;
+    PCIDevice *pdev;
     DriveInfo *hd[6];
     Chardev *chr;
     int fw_size, fit_err;
@@ -769,11 +769,11 @@ static void boston_mach_init(MachineState *machine)
     qemu_chr_fe_set_handlers(&s->lcd_display, NULL, NULL,
                              boston_lcd_event, NULL, s, NULL, true);
 
-    ahci = pci_create_simple_multifunction(&PCI_BRIDGE(&pcie2->root)->sec_bus,
+    pdev = pci_create_simple_multifunction(&PCI_BRIDGE(&pcie2->root)->sec_bus,
                                            PCI_DEVFN(0, 0), TYPE_ICH9_AHCI);
-    g_assert(ARRAY_SIZE(hd) == ahci_get_num_ports(ahci));
-    ide_drive_get(hd, ahci_get_num_ports(ahci));
-    ahci_ide_create_devs(ahci, hd);
+    g_assert(ARRAY_SIZE(hd) == ahci_get_num_ports(pdev));
+    ide_drive_get(hd, ahci_get_num_ports(pdev));
+    ahci_ide_create_devs(pdev, hd);
 
     if (machine->firmware) {
         fw_size = load_image_targphys(machine->firmware,
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH 3/9] hw/ide/ahci: Rename AHCI PCI function as 'pdev'

Quoted 9 lines · Philippe Mathieu-Daudé

We want to access AHCIPCIState::ahci field. In order to keep the code simple (avoiding &ahci->ahci), rename the current 'ahci' variable as 'pdev'

---
  hw/i386/pc_q35.c | 15 ++++++++-------
  hw/mips/boston.c | 10 +++++-----
  2 files changed, 13 insertions(+), 12 deletions(-)
#7 lore raw

Re: [PATCH 3/9] hw/ide/ahci: Rename AHCI PCI function as 'pdev'

Quoted 11 lines · Richard Henderson
Quoted 9 lines · Philippe Mathieu-Daudé

We want to access AHCIPCIState::ahci field. In order to keep the code simple (avoiding &ahci->ahci), rename the current 'ahci' variable as 'pdev'

---
  hw/i386/pc_q35.c | 15 ++++++++-------
  hw/mips/boston.c | 10 +++++-----

  2 files changed, 13 insertions(+), 12 deletions(-)

I'll handle this empty reply as a R-b tag :) Thanks!

#8 lore raw

PATCH 4/9 hw/ide/ahci: Inline ahci_get_num_ports()

In reply to Philippe Mathieu-Daudé

Introduce the 'ich9' variable and inline ahci_get_num_ports().

---
 include/hw/ide/ahci.h | 1 -
 hw/i386/pc_q35.c      | 6 ++++--
 hw/ide/ahci.c         | 8 --------
 hw/mips/boston.c      | 6 ++++--
 4 files changed, 8 insertions(+), 13 deletions(-)
include/hw/ide/ahci.h +0 -1
index 6818d02063..dbef377f3d 100644
@@ -52,7 +52,6 @@ typedef struct AHCIState {
 } AHCIState;
 
 
-int32_t ahci_get_num_ports(PCIDevice *dev);
 void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd);
 
 #define TYPE_SYSBUS_AHCI "sysbus-ahci"
hw/i386/pc_q35.c +4 -2
index e298f4ff32..c50e3bfc42 100644
@@ -304,16 +304,18 @@ static void pc_q35_init(MachineState *machine)
 
     if (pcms->sata_enabled) {
         PCIDevice *pdev;
+        AHCIPCIState *ich9;
 
         /* ahci and SATA device, for q35 1 ahci controller is built-in */
         pdev = pci_create_simple_multifunction(host_bus,
                                                PCI_DEVFN(ICH9_SATA1_DEV,
                                                          ICH9_SATA1_FUNC),
                                                "ich9-ahci");
+        ich9 = ICH9_AHCI(pdev);
         idebus[0] = qdev_get_child_bus(DEVICE(pdev), "ide.0");
         idebus[1] = qdev_get_child_bus(DEVICE(pdev), "ide.1");
-        g_assert(MAX_SATA_PORTS == ahci_get_num_ports(pdev));
-        ide_drive_get(hd, ahci_get_num_ports(pdev));
+        g_assert(MAX_SATA_PORTS == ich9->ahci.ports);
+        ide_drive_get(hd, ich9->ahci.ports);
         ahci_ide_create_devs(pdev, hd);
     } else {
         idebus[0] = idebus[1] = NULL;
hw/ide/ahci.c +0 -8
index aa9381a7b2..8b97c6b0e7 100644
@@ -1896,14 +1896,6 @@ static void sysbus_ahci_register_types(void)
 
 type_init(sysbus_ahci_register_types)
 
-int32_t ahci_get_num_ports(PCIDevice *dev)
-{
-    AHCIPCIState *d = ICH9_AHCI(dev);
-    AHCIState *ahci = &d->ahci;
-
-    return ahci->ports;
-}
-
 void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd)
 {
     AHCIPCIState *d = ICH9_AHCI(dev);
hw/mips/boston.c +4 -2
index 0ec0b98066..a6c7bc18ff 100644
@@ -678,6 +678,7 @@ static void boston_mach_init(MachineState *machine)
     MemoryRegion *sys_mem = get_system_memory();
     XilinxPCIEHost *pcie2;
     PCIDevice *pdev;
+    AHCIPCIState *ich9;
     DriveInfo *hd[6];
     Chardev *chr;
     int fw_size, fit_err;
@@ -771,8 +772,9 @@ static void boston_mach_init(MachineState *machine)
 
     pdev = pci_create_simple_multifunction(&PCI_BRIDGE(&pcie2->root)->sec_bus,
                                            PCI_DEVFN(0, 0), TYPE_ICH9_AHCI);
-    g_assert(ARRAY_SIZE(hd) == ahci_get_num_ports(pdev));
-    ide_drive_get(hd, ahci_get_num_ports(pdev));
+    ich9 = ICH9_AHCI(pdev);
+    g_assert(ARRAY_SIZE(hd) == ich9->ahci.ports);
+    ide_drive_get(hd, ich9->ahci.ports);
     ahci_ide_create_devs(pdev, hd);
 
     if (machine->firmware) {
Signature · 1 line
2.41.0
Reviewed-by on v1 4/9
#9 lore raw

Re: [PATCH 4/9] hw/ide/ahci: Inline ahci_get_num_ports()

Quoted 9 lines · Philippe Mathieu-Daudé

Introduce the 'ich9' variable and inline ahci_get_num_ports().

---
  include/hw/ide/ahci.h | 1 -
  hw/i386/pc_q35.c      | 6 ++++--
  hw/ide/ahci.c         | 8 --------
  hw/mips/boston.c      | 6 ++++--
  4 files changed, 8 insertions(+), 13 deletions(-)

As far as it goes. But it certainly highlights that

Quoted 2 lines
hw/i386/pc_q35.c +2 -0
+        g_assert(MAX_SATA_PORTS == ich9->ahci.ports);
+        ide_drive_get(hd, ich9->ahci.ports);

....

hw/mips/boston.c static void boston_mach_init(MachineState *machine) · line 777

Quoted 2 lines
hw/mips/boston.c +2 -0
+    g_assert(ARRAY_SIZE(hd) == ich9->ahci.ports);
+    ide_drive_get(hd, ich9->ahci.ports);

ports is always a constant. Or perhaps that's only from this PCI usage?

r~

#10 lore raw

Re: [PATCH 4/9] hw/ide/ahci: Inline ahci_get_num_ports()

Quoted 15 lines · Richard Henderson
Quoted 9 lines · Philippe Mathieu-Daudé

Introduce the 'ich9' variable and inline ahci_get_num_ports().

---
  include/hw/ide/ahci.h | 1 -
  hw/i386/pc_q35.c      | 6 ++++--
  hw/ide/ahci.c         | 8 --------
  hw/mips/boston.c      | 6 ++++--

  4 files changed, 8 insertions(+), 13 deletions(-)

As far as it goes.  But it certainly highlights that

Quoted 7 lines
Quoted 2 lines

+        g_assert(MAX_SATA_PORTS == ich9->ahci.ports); +        ide_drive_get(hd, ich9->ahci.ports);

....

Quoted 2 lines

+    g_assert(ARRAY_SIZE(hd) == ich9->ahci.ports); +    ide_drive_get(hd, ich9->ahci.ports);

ports is always a constant.  Or perhaps that's only from this PCI usage?

I'm just moving code around :)

I'm not sure about this assert, but TBH I consider ide_drive_get() as legacy API (pre -blockdev, only 7 uses in the tree, half from old boards).

I'll let that constant cleanup for later.

Regards,

Phil.

#11 lore raw

PATCH 5/9 hw/ide/ahci: Pass AHCI context to ahci_ide_create_devs()

In reply to Philippe Mathieu-Daudé

Since ahci_ide_create_devs() is not PCI specific, pass it an AHCIState argument instead of PCIDevice.

---
 include/hw/ide/ahci.h | 2 +-
 hw/i386/pc_q35.c      | 2 +-
 hw/ide/ahci.c         | 5 +----
 hw/mips/boston.c      | 2 +-
 4 files changed, 4 insertions(+), 7 deletions(-)
include/hw/ide/ahci.h +1 -1
index dbef377f3d..8cd55b1333 100644
@@ -52,7 +52,7 @@ typedef struct AHCIState {
 } AHCIState;
 
 
-void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd);
+void ahci_ide_create_devs(AHCIState *ahci, DriveInfo **hd);
 
 #define TYPE_SYSBUS_AHCI "sysbus-ahci"
 OBJECT_DECLARE_SIMPLE_TYPE(SysbusAHCIState, SYSBUS_AHCI)
hw/i386/pc_q35.c +1 -1
index c50e3bfc42..7f4f51fcdf 100644
@@ -316,7 +316,7 @@ static void pc_q35_init(MachineState *machine)
         idebus[1] = qdev_get_child_bus(DEVICE(pdev), "ide.1");
         g_assert(MAX_SATA_PORTS == ich9->ahci.ports);
         ide_drive_get(hd, ich9->ahci.ports);
-        ahci_ide_create_devs(pdev, hd);
+        ahci_ide_create_devs(&ich9->ahci, hd);
     } else {
         idebus[0] = idebus[1] = NULL;
     }
hw/ide/ahci.c +1 -4
index 8b97c6b0e7..bac1871a31 100644
@@ -1896,10 +1896,8 @@ static void sysbus_ahci_register_types(void)
 
 type_init(sysbus_ahci_register_types)
 
-void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd)
+void ahci_ide_create_devs(AHCIState *ahci, DriveInfo **hd)
 {
-    AHCIPCIState *d = ICH9_AHCI(dev);
-    AHCIState *ahci = &d->ahci;
     int i;
 
     for (i = 0; i < ahci->ports; i++) {
@@ -1908,5 +1906,4 @@ void ahci_ide_create_devs(PCIDevice *dev, DriveInfo **hd)
         }
         ide_bus_create_drive(&ahci->dev[i].port, 0, hd[i]);
     }
-
 }
hw/mips/boston.c +1 -1
index a6c7bc18ff..1b44fb354c 100644
@@ -775,7 +775,7 @@ static void boston_mach_init(MachineState *machine)
     ich9 = ICH9_AHCI(pdev);
     g_assert(ARRAY_SIZE(hd) == ich9->ahci.ports);
     ide_drive_get(hd, ich9->ahci.ports);
-    ahci_ide_create_devs(pdev, hd);
+    ahci_ide_create_devs(&ich9->ahci, hd);
 
     if (machine->firmware) {
         fw_size = load_image_targphys(machine->firmware,
Signature · 1 line
2.41.0
Reviewed-by on v1 5/9
#12 lore raw

Re: [PATCH 5/9] hw/ide/ahci: Pass AHCI context to ahci_ide_create_devs()

Quoted 10 lines · Philippe Mathieu-Daudé

Since ahci_ide_create_devs() is not PCI specific, pass it an AHCIState argument instead of PCIDevice.

---
  include/hw/ide/ahci.h | 2 +-
  hw/i386/pc_q35.c      | 2 +-
  hw/ide/ahci.c         | 5 +----
  hw/mips/boston.c      | 2 +-
  4 files changed, 4 insertions(+), 7 deletions(-)

r~

#13 lore raw

PATCH 6/9 hw/ide/ahci: Convert AHCIState::ports to unsigned

In reply to Philippe Mathieu-Daudé

AHCIState::ports should be unsigned. Besides, we never check it for negative value. It is unlikely it was ever used with more than INT32_MAX ports, so it is safe to convert it to unsigned.

---
 include/hw/ide/ahci.h | 2 +-
 hw/ide/ahci.c         | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/hw/ide/ahci.h +1 -1
index 8cd55b1333..604d3a0994 100644
@@ -46,7 +46,7 @@ typedef struct AHCIState {
     MemoryRegion idp;       /* Index-Data Pair I/O port space */
     unsigned idp_offset;    /* Offset of index in I/O port space */
     uint32_t idp_index;     /* Current IDP index */
-    int32_t ports;
+    uint32_t ports;
     qemu_irq irq;
     AddressSpace *as;
 } AHCIState;
hw/ide/ahci.c +2 -2
index bac1871a31..2c3306dae4 100644
@@ -1819,7 +1819,7 @@ const VMStateDescription vmstate_ahci = {
     .version_id = 1,
     .post_load = ahci_state_post_load,
     .fields = (const VMStateField[]) {
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(dev, AHCIState, ports,
+        VMSTATE_STRUCT_VARRAY_POINTER_UINT32(dev, AHCIState, ports,
                                      vmstate_ahci_device, AHCIDevice),
         VMSTATE_UINT32(control_regs.cap, AHCIState),
         VMSTATE_UINT32(control_regs.ghc, AHCIState),
@@ -1827,7 +1827,7 @@ const VMStateDescription vmstate_ahci = {
         VMSTATE_UINT32(control_regs.impl, AHCIState),
         VMSTATE_UINT32(control_regs.version, AHCIState),
         VMSTATE_UINT32(idp_index, AHCIState),
-        VMSTATE_INT32_EQUAL(ports, AHCIState, NULL),
+        VMSTATE_UINT32_EQUAL(ports, AHCIState, NULL),
         VMSTATE_END_OF_LIST()
     },
 };
Signature · 1 line
2.41.0
Reviewed-by on v1 6/9
#14 lore raw

Re: [PATCH 6/9] hw/ide/ahci: Convert AHCIState::ports to unsigned

Quoted 10 lines · Philippe Mathieu-Daudé

AHCIState::ports should be unsigned. Besides, we never check it for negative value. It is unlikely it was ever used with more than INT32_MAX ports, so it is safe to convert it to unsigned.

---
  include/hw/ide/ahci.h | 2 +-
  hw/ide/ahci.c         | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)

r~

#15 lore raw

PATCH 7/9 hw/ide/ahci: Do not pass 'ports' argument to ahci_realize()

In reply to Philippe Mathieu-Daudé

Explicitly set AHCIState::ports before calling ahci_realize().

---
 hw/ide/ahci_internal.h | 2 +-
 hw/ide/ahci.c          | 9 +++++----
 hw/ide/ich.c           | 3 ++-
 3 files changed, 8 insertions(+), 6 deletions(-)
hw/ide/ahci_internal.h +1 -1
index 4dc2805d21..4e13329bb2 100644
@@ -377,7 +377,7 @@ typedef struct SDBFIS {
     uint32_t payload;
 } QEMU_PACKED SDBFIS;
 
-void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports);
+void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as);
 void ahci_init(AHCIState *s, DeviceState *qdev);
 void ahci_uninit(AHCIState *s);
 
hw/ide/ahci.c +5 -4
index 2c3306dae4..33f7e83687 100644
@@ -1614,14 +1614,14 @@ void ahci_init(AHCIState *s, DeviceState *qdev)
                           "ahci-idp", 32);
 }
 
-void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports)
+void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as)
 {
     qemu_irq *irqs;
     int i;
 
     s->as = as;
-    s->ports = ports;
-    s->dev = g_new0(AHCIDevice, ports);
+    assert(s->ports > 0);
+    s->dev = g_new0(AHCIDevice, s->ports);
     ahci_reg_init(s);
     irqs = qemu_allocate_irqs(ahci_irq_set, s, s->ports);
     for (i = 0; i < s->ports; i++) {
@@ -1862,7 +1862,8 @@ static void sysbus_ahci_realize(DeviceState *dev, Error **errp)
 {
     SysbusAHCIState *s = SYSBUS_AHCI(dev);
 
-    ahci_realize(&s->ahci, dev, &address_space_memory, s->num_ports);
+    s->ahci.ports = s->num_ports;
+    ahci_realize(&s->ahci, dev, &address_space_memory);
 }
 
 static Property sysbus_ahci_properties[] = {
hw/ide/ich.c +2 -1
index d190012a95..122fc7e0ab 100644
@@ -113,7 +113,8 @@ static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
     d = ICH9_AHCI(dev);
     int ret;
 
-    ahci_realize(&d->ahci, DEVICE(dev), pci_get_address_space(dev), 6);
+    d->ahci.ports = 6;
+    ahci_realize(&d->ahci, DEVICE(dev), pci_get_address_space(dev));
 
     pci_config_set_prog_interface(dev->config, AHCI_PROGMODE_MAJOR_REV_1);
 
Signature · 1 line
2.41.0
#17 lore raw

PATCH 8/9 hw/ide/ahci: Remove SysbusAHCIState::num_ports field

In reply to Philippe Mathieu-Daudé

No need to duplicate AHCIState::ports, directly access it.

---
 include/hw/ide/ahci.h | 1 -
 hw/ide/ahci.c         | 3 +--
 2 files changed, 1 insertion(+), 3 deletions(-)
include/hw/ide/ahci.h +0 -1
index 604d3a0994..c0b10c2bb4 100644
@@ -63,7 +63,6 @@ struct SysbusAHCIState {
     /*< public >*/
 
     AHCIState ahci;
-    uint32_t num_ports;
 };
 
 #define TYPE_ALLWINNER_AHCI "allwinner-ahci"
hw/ide/ahci.c +1 -2
index 33f7e83687..041cc87c11 100644
@@ -1862,12 +1862,11 @@ static void sysbus_ahci_realize(DeviceState *dev, Error **errp)
 {
     SysbusAHCIState *s = SYSBUS_AHCI(dev);
 
-    s->ahci.ports = s->num_ports;
     ahci_realize(&s->ahci, dev, &address_space_memory);
 }
 
 static Property sysbus_ahci_properties[] = {
-    DEFINE_PROP_UINT32("num-ports", SysbusAHCIState, num_ports, 1),
+    DEFINE_PROP_UINT32("num-ports", SysbusAHCIState, ahci.ports, 1),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.41.0
#19 lore raw

PATCH 9/9 hw/ide/ahci: Move SysBus definitions to 'ahci-sysbus.h'

In reply to Philippe Mathieu-Daudé

Keep "hw/ide/ahci.h" AHCI-generic.

---
 include/hw/arm/allwinner-a10.h |  2 +-
 include/hw/arm/allwinner-r40.h |  2 +-
 include/hw/arm/xlnx-zynqmp.h   |  2 +-
 include/hw/ide/ahci-sysbus.h   | 35 ++++++++++++++++++++++++++++++++++
 include/hw/ide/ahci.h          | 29 +---------------------------
 hw/arm/highbank.c              |  2 +-
 hw/arm/sbsa-ref.c              |  1 +
 hw/ide/ahci-allwinner.c        |  3 +--
 hw/ide/ahci.c                  |  1 +
 9 files changed, 43 insertions(+), 34 deletions(-)
 create mode 100644 include/hw/ide/ahci-sysbus.h
include/hw/arm/allwinner-a10.h +1 -1
index 2eb83a17ea..67a9a17b86 100644
@@ -5,7 +5,7 @@
 #include "hw/intc/allwinner-a10-pic.h"
 #include "hw/net/allwinner_emac.h"
 #include "hw/sd/allwinner-sdhost.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "hw/usb/hcd-ohci.h"
 #include "hw/usb/hcd-ehci.h"
 #include "hw/rtc/allwinner-rtc.h"
include/hw/arm/allwinner-r40.h +1 -1
index 66c38e7d90..614e74b7ed 100644
@@ -22,7 +22,7 @@
 
 #include "qom/object.h"
 #include "hw/timer/allwinner-a10-pit.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "hw/intc/arm_gic.h"
 #include "hw/sd/allwinner-sdhost.h"
 #include "hw/misc/allwinner-r40-ccu.h"
include/hw/arm/xlnx-zynqmp.h +1 -1
index 96358d51eb..48f7948092 100644
@@ -22,7 +22,7 @@
 #include "hw/net/cadence_gem.h"
 #include "hw/char/cadence_uart.h"
 #include "hw/net/xlnx-zynqmp-can.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "hw/sd/sdhci.h"
 #include "hw/ssi/xilinx_spips.h"
 #include "hw/dma/xlnx_dpdma.h"
include/hw/ide/ahci-sysbus.h new file +35 -0
new file mode 100644
index 0000000000..7ed6cad496
@@ -0,0 +1,35 @@
+/*
+ * QEMU AHCI Emulation (MMIO-mapped devices)
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_IDE_AHCI_SYSBUS_H
+#define HW_IDE_AHCI_SYSBUS_H
+
+#include "qom/object.h"
+#include "hw/sysbus.h"
+#include "hw/ide/ahci.h"
+
+#define TYPE_SYSBUS_AHCI "sysbus-ahci"
+OBJECT_DECLARE_SIMPLE_TYPE(SysbusAHCIState, SYSBUS_AHCI)
+
+struct SysbusAHCIState {
+    SysBusDevice parent_obj;
+
+    AHCIState ahci;
+};
+
+#define TYPE_ALLWINNER_AHCI "allwinner-ahci"
+OBJECT_DECLARE_SIMPLE_TYPE(AllwinnerAHCIState, ALLWINNER_AHCI)
+
+#define ALLWINNER_AHCI_MMIO_OFF  0x80
+#define ALLWINNER_AHCI_MMIO_SIZE 0x80
+
+struct AllwinnerAHCIState {
+    SysbusAHCIState parent_obj;
+
+    MemoryRegion mmio;
+    uint32_t regs[ALLWINNER_AHCI_MMIO_SIZE/4];
+};
+
+#endif
include/hw/ide/ahci.h +1 -28
index c0b10c2bb4..ba31e75ff9 100644
@@ -24,8 +24,7 @@
 #ifndef HW_IDE_AHCI_H
 #define HW_IDE_AHCI_H
 
-#include "hw/sysbus.h"
-#include "qom/object.h"
+#include "exec/memory.h"
 
 typedef struct AHCIDevice AHCIDevice;
 
@@ -54,30 +53,4 @@ typedef struct AHCIState {
 
 void ahci_ide_create_devs(AHCIState *ahci, DriveInfo **hd);
 
-#define TYPE_SYSBUS_AHCI "sysbus-ahci"
-OBJECT_DECLARE_SIMPLE_TYPE(SysbusAHCIState, SYSBUS_AHCI)
-
-struct SysbusAHCIState {
-    /*< private >*/
-    SysBusDevice parent_obj;
-    /*< public >*/
-
-    AHCIState ahci;
-};
-
-#define TYPE_ALLWINNER_AHCI "allwinner-ahci"
-OBJECT_DECLARE_SIMPLE_TYPE(AllwinnerAHCIState, ALLWINNER_AHCI)
-
-#define ALLWINNER_AHCI_MMIO_OFF  0x80
-#define ALLWINNER_AHCI_MMIO_SIZE 0x80
-
-struct AllwinnerAHCIState {
-    /*< private >*/
-    SysbusAHCIState parent_obj;
-    /*< public >*/
-
-    MemoryRegion mmio;
-    uint32_t regs[ALLWINNER_AHCI_MMIO_SIZE/4];
-};
-
 #endif /* HW_IDE_AHCI_H */
hw/arm/highbank.c +1 -1
index 9fdac1cc81..c71b1a8db3 100644
@@ -30,7 +30,7 @@
 #include "hw/boards.h"
 #include "qemu/error-report.h"
 #include "hw/char/pl011.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "hw/cpu/a9mpcore.h"
 #include "hw/cpu/a15mpcore.h"
 #include "qemu/log.h"
hw/arm/sbsa-ref.c +1 -0
index f2adf30337..5d3a574664 100644
@@ -38,6 +38,7 @@
 #include "hw/boards.h"
 #include "hw/ide/internal.h"
 #include "hw/ide/ahci_internal.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "hw/intc/arm_gicv3_common.h"
 #include "hw/intc/arm_gicv3_its_common.h"
 #include "hw/loader.h"
hw/ide/ahci-allwinner.c +1 -2
index b173121006..9620de8ce8 100644
@@ -19,9 +19,8 @@
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "sysemu/dma.h"
-#include "hw/ide/internal.h"
 #include "migration/vmstate.h"
-#include "ahci_internal.h"
+#include "hw/ide/ahci-sysbus.h"
 
 #include "trace.h"
 
hw/ide/ahci.c +1 -0
index 041cc87c11..54c9685495 100644
@@ -37,6 +37,7 @@
 #include "hw/ide/internal.h"
 #include "hw/ide/pci.h"
 #include "hw/ide/ahci-pci.h"
+#include "hw/ide/ahci-sysbus.h"
 #include "ahci_internal.h"
 
 #include "trace.h"
Signature · 1 line
2.41.0
Reviewed-by on v1 9/9
#20 lore raw

Re: [PATCH 9/9] hw/ide/ahci: Move SysBus definitions to 'ahci-sysbus.h'

Quoted 11 lines · Philippe Mathieu-Daudé

Keep "hw/ide/ahci.h" AHCI-generic.

---
  include/hw/arm/allwinner-a10.h |  2 +-
  include/hw/arm/allwinner-r40.h |  2 +-
  include/hw/arm/xlnx-zynqmp.h   |  2 +-
  include/hw/ide/ahci-sysbus.h   | 35 ++++++++++++++++++++++++++++++++++
  include/hw/ide/ahci.h          | 29 +---------------------------
  hw/arm/highbank.c              |  2 +-
  hw/arm/sbsa-ref.c              |  1 +

Obviously, if the actual functional change happens, I'd be happy for sbsa-ref to keep building, so for that aspect:

/
Leif

Quoted 182 lines
  hw/ide/ahci-allwinner.c        |  3 +--
  hw/ide/ahci.c                  |  1 +
  9 files changed, 43 insertions(+), 34 deletions(-)

create mode 100644 include/hw/ide/ahci-sysbus.h

include/hw/arm/allwinner-a10.h +1 -1
index 2eb83a17ea..67a9a17b86 100644
@@ -5,7 +5,7 @@
  #include "hw/intc/allwinner-a10-pic.h"
  #include "hw/net/allwinner_emac.h"
  #include "hw/sd/allwinner-sdhost.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "hw/usb/hcd-ohci.h"
  #include "hw/usb/hcd-ehci.h"
  #include "hw/rtc/allwinner-rtc.h"
include/hw/arm/allwinner-r40.h +1 -1
index 66c38e7d90..614e74b7ed 100644
@@ -22,7 +22,7 @@
 
  #include "qom/object.h"
  #include "hw/timer/allwinner-a10-pit.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "hw/intc/arm_gic.h"
  #include "hw/sd/allwinner-sdhost.h"
  #include "hw/misc/allwinner-r40-ccu.h"
include/hw/arm/xlnx-zynqmp.h +1 -1
index 96358d51eb..48f7948092 100644
@@ -22,7 +22,7 @@
  #include "hw/net/cadence_gem.h"
  #include "hw/char/cadence_uart.h"
  #include "hw/net/xlnx-zynqmp-can.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "hw/sd/sdhci.h"
  #include "hw/ssi/xilinx_spips.h"
  #include "hw/dma/xlnx_dpdma.h"
include/hw/ide/ahci-sysbus.h new file +35 -0
new file mode 100644
index 0000000000..7ed6cad496
@@ -0,0 +1,35 @@
+/*
+ * QEMU AHCI Emulation (MMIO-mapped devices)
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_IDE_AHCI_SYSBUS_H
+#define HW_IDE_AHCI_SYSBUS_H
+
+#include "qom/object.h"
+#include "hw/sysbus.h"
+#include "hw/ide/ahci.h"
+
+#define TYPE_SYSBUS_AHCI "sysbus-ahci"
+OBJECT_DECLARE_SIMPLE_TYPE(SysbusAHCIState, SYSBUS_AHCI)
+
+struct SysbusAHCIState {
+    SysBusDevice parent_obj;
+
+    AHCIState ahci;
+};
+
+#define TYPE_ALLWINNER_AHCI "allwinner-ahci"
+OBJECT_DECLARE_SIMPLE_TYPE(AllwinnerAHCIState, ALLWINNER_AHCI)
+
+#define ALLWINNER_AHCI_MMIO_OFF  0x80
+#define ALLWINNER_AHCI_MMIO_SIZE 0x80
+
+struct AllwinnerAHCIState {
+    SysbusAHCIState parent_obj;
+
+    MemoryRegion mmio;
+    uint32_t regs[ALLWINNER_AHCI_MMIO_SIZE/4];
+};
+
+#endif
include/hw/ide/ahci.h +1 -28
index c0b10c2bb4..ba31e75ff9 100644
@@ -24,8 +24,7 @@
  #ifndef HW_IDE_AHCI_H
  #define HW_IDE_AHCI_H
 
-#include "hw/sysbus.h"
-#include "qom/object.h"
+#include "exec/memory.h"
 
  typedef struct AHCIDevice AHCIDevice;
 
@@ -54,30 +53,4 @@ typedef struct AHCIState {
 
  void ahci_ide_create_devs(AHCIState *ahci, DriveInfo **hd);
 
-#define TYPE_SYSBUS_AHCI "sysbus-ahci"
-OBJECT_DECLARE_SIMPLE_TYPE(SysbusAHCIState, SYSBUS_AHCI)
-
-struct SysbusAHCIState {
-    /*< private >*/
-    SysBusDevice parent_obj;
-    /*< public >*/
-
-    AHCIState ahci;
-};
-
-#define TYPE_ALLWINNER_AHCI "allwinner-ahci"
-OBJECT_DECLARE_SIMPLE_TYPE(AllwinnerAHCIState, ALLWINNER_AHCI)
-
-#define ALLWINNER_AHCI_MMIO_OFF  0x80
-#define ALLWINNER_AHCI_MMIO_SIZE 0x80
-
-struct AllwinnerAHCIState {
-    /*< private >*/
-    SysbusAHCIState parent_obj;
-    /*< public >*/
-
-    MemoryRegion mmio;
-    uint32_t regs[ALLWINNER_AHCI_MMIO_SIZE/4];
-};
-
  #endif /* HW_IDE_AHCI_H */
hw/arm/highbank.c +1 -1
index 9fdac1cc81..c71b1a8db3 100644
@@ -30,7 +30,7 @@
  #include "hw/boards.h"
  #include "qemu/error-report.h"
  #include "hw/char/pl011.h"
-#include "hw/ide/ahci.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "hw/cpu/a9mpcore.h"
  #include "hw/cpu/a15mpcore.h"
  #include "qemu/log.h"
hw/arm/sbsa-ref.c +1 -0
index f2adf30337..5d3a574664 100644
@@ -38,6 +38,7 @@
  #include "hw/boards.h"
  #include "hw/ide/internal.h"
  #include "hw/ide/ahci_internal.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "hw/intc/arm_gicv3_common.h"
  #include "hw/intc/arm_gicv3_its_common.h"
  #include "hw/loader.h"
hw/ide/ahci-allwinner.c +1 -2
index b173121006..9620de8ce8 100644
@@ -19,9 +19,8 @@
  #include "qemu/error-report.h"
  #include "qemu/module.h"
  #include "sysemu/dma.h"
-#include "hw/ide/internal.h"
  #include "migration/vmstate.h"
-#include "ahci_internal.h"
+#include "hw/ide/ahci-sysbus.h"
 
  #include "trace.h"
 
hw/ide/ahci.c +1 -0
index 041cc87c11..54c9685495 100644
@@ -37,6 +37,7 @@
  #include "hw/ide/internal.h"
  #include "hw/ide/pci.h"
  #include "hw/ide/ahci-pci.h"
+#include "hw/ide/ahci-sysbus.h"
  #include "ahci_internal.h"
 
  #include "trace.h"
Reviewed-by on the v1 series
#21 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

- Split 'ahci.h' as sysbus / pci
- Inline ahci_get_num_ports()
- Directly use AHCIState::ports instead of SysbusAHCIState::num_ports

PC part:

feel free to merge with rest of patches.

Quoted 31 lines

Philippe Mathieu-Daudé (9):
hw/i386/q35: Use DEVICE() cast macro with PCIDevice object hw/ide/ahci: Expose AHCIPCIState structure hw/ide/ahci: Rename AHCI PCI function as 'pdev' hw/ide/ahci: Inline ahci_get_num_ports() hw/ide/ahci: Pass AHCI context to ahci_ide_create_devs() hw/ide/ahci: Convert AHCIState::ports to unsigned hw/ide/ahci: Do not pass 'ports' argument to ahci_realize() hw/ide/ahci: Remove SysbusAHCIState::num_ports field hw/ide/ahci: Move SysBus definitions to 'ahci-sysbus.h'

 hw/ide/ahci_internal.h         | 10 +--------
 include/hw/arm/allwinner-a10.h |  2 +-
 include/hw/arm/allwinner-r40.h |  2 +-
 include/hw/arm/xlnx-zynqmp.h   |  2 +-
 include/hw/ide/ahci-pci.h      | 22 ++++++++++++++++++++
 include/hw/ide/ahci-sysbus.h   | 35 +++++++++++++++++++++++++++++++
 include/hw/ide/ahci.h          | 38 +++-------------------------------
 hw/arm/highbank.c              |  2 +-
 hw/arm/sbsa-ref.c              |  1 +
 hw/i386/pc_q35.c               | 19 ++++++++++-------
 hw/ide/ahci-allwinner.c        |  3 +--
 hw/ide/ahci.c                  | 29 +++++++++-----------------
 hw/ide/ich.c                   |  4 +++-
 hw/mips/boston.c               | 14 +++++++------
 14 files changed, 99 insertions(+), 84 deletions(-)
 create mode 100644 include/hw/ide/ahci-pci.h
 create mode 100644 include/hw/ide/ahci-sysbus.h
Signature · 1 line
2.41.0
#22 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

- Split 'ahci.h' as sysbus / pci
- Inline ahci_get_num_ports()
- Directly use AHCIState::ports instead of SysbusAHCIState::num_ports

Quoted 10 lines

Philippe Mathieu-Daudé (9):
hw/i386/q35: Use DEVICE() cast macro with PCIDevice object hw/ide/ahci: Expose AHCIPCIState structure hw/ide/ahci: Rename AHCI PCI function as 'pdev' hw/ide/ahci: Inline ahci_get_num_ports() hw/ide/ahci: Pass AHCI context to ahci_ide_create_devs() hw/ide/ahci: Convert AHCIState::ports to unsigned hw/ide/ahci: Do not pass 'ports' argument to ahci_realize() hw/ide/ahci: Remove SysbusAHCIState::num_ports field hw/ide/ahci: Move SysBus definitions to 'ahci-sysbus.h'

Series queued.