Skip to contentSkip to conversation

AHCI cleanup

PATCH In review Bernhard Beschow 2 participants 6 messages Dec 12, 2024 – Dec 13, 2024 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v4 cover AHCI cleanup

This series fixes some runtime overhead when handling interrupts in AHCIState. It then extracts the SysBus implementation into a dedicated file for separation of concerns.

v4:
* Port away from qemu_allocate_irq() while at it

v3:
* Remove extra PCI include in ahci-internal.h
* Extract SysBus implementation into dedicated file

Supersedes: [email protected]

Bernhard Beschow (2):
hw/ide/ahci: Decouple from PCI
hw/ide/ahci: Extract TYPE_SYSBUS_AHCI into dedicated file

 hw/ide/ahci-internal.h    |   1 -
 include/hw/ide/ahci-pci.h |   2 +
 include/hw/ide/ahci.h     |   2 -
 hw/ide/ahci-sysbus.c      |  91 ++++++++++++++++++++++++++++++++
 hw/ide/ahci.c             | 106 ++------------------------------------
 hw/ide/ich.c              |  19 +++++--
 hw/arm/Kconfig            |  10 ++--
 hw/ide/Kconfig            |   4 ++
 hw/ide/meson.build        |   1 +
 9 files changed, 122 insertions(+), 114 deletions(-)
 create mode 100644 hw/ide/ahci-sysbus.c
Signature · 1 line
2.47.1
#1 lore raw

PATCH v4 1/2 hw/ide/ahci: Decouple from PCI

In some adhoc profiling booting Linux VMs, it's observed that ahci_irq_lower() can be a hot path (10000+ triggers until login prompt appears). Even though the parent device never changes, this method re-determines whether the parent device is a PCI device or not using the rather expensive object_dynamic_cast() function. Avoid this overhead by pushing the interrupt handling to the parent device, essentially turning AHCIState into an "IP block".

Note that this change also frees AHCIState from the PCI dependency which wasn't reflected in Kconfig.

---
 hw/ide/ahci-internal.h    |  1 -
 include/hw/ide/ahci-pci.h |  2 ++
 include/hw/ide/ahci.h     |  2 --
 hw/ide/ahci.c             | 39 ++++-----------------------------------
 hw/ide/ich.c              | 19 +++++++++++++++----
 5 files changed, 21 insertions(+), 42 deletions(-)
hw/ide/ahci-internal.h +0 -1
index 7e63ea2310..a318f36811 100644
@@ -25,7 +25,6 @@
 #define HW_IDE_AHCI_INTERNAL_H
 
 #include "hw/ide/ahci.h"
-#include "hw/pci/pci_device.h"
 #include "ide-internal.h"
 
 #define AHCI_MEM_BAR_SIZE         0x1000
include/hw/ide/ahci-pci.h +2 -0
index c2ee616962..face1a9a4a 100644
@@ -9,6 +9,7 @@
 #include "qom/object.h"
 #include "hw/ide/ahci.h"
 #include "hw/pci/pci_device.h"
+#include "hw/irq.h"
 
 #define TYPE_ICH9_AHCI "ich9-ahci"
 OBJECT_DECLARE_SIMPLE_TYPE(AHCIPCIState, ICH9_AHCI)
@@ -17,6 +18,7 @@ struct AHCIPCIState {
     PCIDevice parent_obj;
 
     AHCIState ahci;
+    IRQState irq;
 };
 
 #endif
include/hw/ide/ahci.h +0 -2
index ba31e75ff9..ac0292c634 100644
@@ -37,8 +37,6 @@ typedef struct AHCIControlRegs {
 } AHCIControlRegs;
 
 typedef struct AHCIState {
-    DeviceState *container;
-
     AHCIDevice *dev;
     AHCIControlRegs control_regs;
     MemoryRegion mem;
hw/ide/ahci.c +4 -35
index 0eb24304ee..5836aa924b 100644
@@ -23,8 +23,6 @@
 
 #include "qemu/osdep.h"
 #include "hw/irq.h"
-#include "hw/pci/msi.h"
-#include "hw/pci/pci.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 
@@ -34,8 +32,6 @@
 #include "qemu/module.h"
 #include "sysemu/block-backend.h"
 #include "sysemu/dma.h"
-#include "hw/ide/pci.h"
-#include "hw/ide/ahci-pci.h"
 #include "hw/ide/ahci-sysbus.h"
 #include "ahci-internal.h"
 #include "ide-internal.h"
@@ -179,34 +175,6 @@ static uint32_t ahci_port_read(AHCIState *s, int port, int offset)
     return val;
 }
 
-static void ahci_irq_raise(AHCIState *s)
-{
-    DeviceState *dev_state = s->container;
-    PCIDevice *pci_dev = (PCIDevice *) object_dynamic_cast(OBJECT(dev_state),
-                                                           TYPE_PCI_DEVICE);
-
-    trace_ahci_irq_raise(s);
-
-    if (pci_dev && msi_enabled(pci_dev)) {
-        msi_notify(pci_dev, 0);
-    } else {
-        qemu_irq_raise(s->irq);
-    }
-}
-
-static void ahci_irq_lower(AHCIState *s)
-{
-    DeviceState *dev_state = s->container;
-    PCIDevice *pci_dev = (PCIDevice *) object_dynamic_cast(OBJECT(dev_state),
-                                                           TYPE_PCI_DEVICE);
-
-    trace_ahci_irq_lower(s);
-
-    if (!pci_dev || !msi_enabled(pci_dev)) {
-        qemu_irq_lower(s->irq);
-    }
-}
-
 static void ahci_check_irq(AHCIState *s)
 {
     int i;
@@ -222,9 +190,11 @@ static void ahci_check_irq(AHCIState *s)
     trace_ahci_check_irq(s, old_irq, s->control_regs.irqstatus);
     if (s->control_regs.irqstatus &&
         (s->control_regs.ghc & HOST_CTL_IRQ_EN)) {
-            ahci_irq_raise(s);
+        trace_ahci_irq_raise(s);
+        qemu_irq_raise(s->irq);
     } else {
-        ahci_irq_lower(s);
+        trace_ahci_irq_lower(s);
+        qemu_irq_lower(s->irq);
     }
 }
 
@@ -1608,7 +1578,6 @@ static const IDEDMAOps ahci_dma_ops = {
 
 void ahci_init(AHCIState *s, DeviceState *qdev)
 {
-    s->container = qdev;
     /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
     memory_region_init_io(&s->mem, OBJECT(qdev), &ahci_mem_ops, s,
                           "ahci", AHCI_MEM_BAR_SIZE);
hw/ide/ich.c +15 -4
index b311450c12..c99a44df8e 100644
@@ -61,7 +61,6 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/irq.h"
 #include "hw/pci/msi.h"
 #include "hw/pci/pci.h"
 #include "migration/vmstate.h"
@@ -91,6 +90,19 @@ static const VMStateDescription vmstate_ich9_ahci = {
     },
 };
 
+static void pci_ich9_ahci_update_irq(void *opaque, int irq_num, int level)
+{
+    PCIDevice *pci_dev = opaque;
+
+    if (msi_enabled(pci_dev)) {
+        if (level) {
+            msi_notify(pci_dev, 0);
+        }
+    } else {
+        pci_set_irq(pci_dev, level);
+    }
+}
+
 static void pci_ich9_reset(DeviceState *dev)
 {
     AHCIPCIState *d = ICH9_AHCI(dev);
@@ -102,7 +114,9 @@ static void pci_ich9_ahci_init(Object *obj)
 {
     AHCIPCIState *d = ICH9_AHCI(obj);
 
+    qemu_init_irq(&d->irq, pci_ich9_ahci_update_irq, d, 0);
     ahci_init(&d->ahci, DEVICE(obj));
+    d->ahci.irq = &d->irq;
 }
 
 static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
@@ -125,8 +139,6 @@ static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
     /* XXX Software should program this register */
     dev->config[0x90]   = 1 << 6; /* Address Map Register - AHCI mode */
 
-    d->ahci.irq = pci_allocate_irq(dev);
-
     pci_register_bar(dev, ICH9_IDP_BAR, PCI_BASE_ADDRESS_SPACE_IO,
                      &d->ahci.idp);
     pci_register_bar(dev, ICH9_MEM_BAR, PCI_BASE_ADDRESS_SPACE_MEMORY,
@@ -161,7 +173,6 @@ static void pci_ich9_uninit(PCIDevice *dev)
 
     msi_uninit(dev);
     ahci_uninit(&d->ahci);
-    qemu_free_irq(d->ahci.irq);
 }
 
 static void ich_ahci_class_init(ObjectClass *klass, void *data)
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH v4 1/2] hw/ide/ahci: Decouple from PCI

Quoted 26 lines · Bernhard Beschow

In some adhoc profiling booting Linux VMs, it's observed that ahci_irq_lower() can be a hot path (10000+ triggers until login prompt appears). Even though the parent device never changes, this method re-determines whether the parent device is a PCI device or not using the rather expensive object_dynamic_cast() function. Avoid this overhead by pushing the interrupt handling to the parent device, essentially turning AHCIState into an "IP block".

Note that this change also frees AHCIState from the PCI dependency which wasn't reflected in Kconfig.

---
  hw/ide/ahci-internal.h    |  1 -
  include/hw/ide/ahci-pci.h |  2 ++
  include/hw/ide/ahci.h     |  2 --
  hw/ide/ahci.c             | 39 ++++-----------------------------------
  hw/ide/ich.c              | 19 +++++++++++++++----
  5 files changed, 21 insertions(+), 42 deletions(-)
  static void pci_ich9_reset(DeviceState *dev)
  {
      AHCIPCIState *d = ICH9_AHCI(dev);

hw/ide/ich.c static void pci_ich9_ahci_init(Object *obj) · line 119

Quoted 6 lines
hw/ide/ich.c +2 -0
@@ -102,7 +114,9 @@ static void pci_ich9_ahci_init(Object *obj)
  {
      AHCIPCIState *d = ICH9_AHCI(obj);
 
+    qemu_init_irq(&d->irq, pci_ich9_ahci_update_irq, d, 0);
      ahci_init(&d->ahci, DEVICE(obj));
+    d->ahci.irq = &d->irq;

Pre-existing, but we shouldn't set this directly.
Does the IRQState belong to AHCIState?

Quoted 20 lines
hw/ide/ich.c +0 -3
  }
 
  static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
@@ -125,8 +139,6 @@ static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
      /* XXX Software should program this register */
      dev->config[0x90]   = 1 << 6; /* Address Map Register - AHCI mode */
 
-    d->ahci.irq = pci_allocate_irq(dev);
-
      pci_register_bar(dev, ICH9_IDP_BAR, PCI_BASE_ADDRESS_SPACE_IO,
                       &d->ahci.idp);
      pci_register_bar(dev, ICH9_MEM_BAR, PCI_BASE_ADDRESS_SPACE_MEMORY,
@@ -161,7 +173,6 @@ static void pci_ich9_uninit(PCIDevice *dev)
 
      msi_uninit(dev);
      ahci_uninit(&d->ahci);
-    qemu_free_irq(d->ahci.irq);
  }
 
  static void ich_ahci_class_init(ObjectClass *klass, void *data)
#3 lore raw

Re: [PATCH v4 1/2] hw/ide/ahci: Decouple from PCI

Am 13. Dezember 2024 14:41:46 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 23 lines · Bernhard Beschow
Quoted 20 lines

In some adhoc profiling booting Linux VMs, it's observed that ahci_irq_lower() can be a hot path (10000+ triggers until login prompt appears). Even though the parent device never changes, this method re-determines whether the parent device is a PCI device or not using the rather expensive object_dynamic_cast() function. Avoid this overhead by pushing the interrupt handling to the parent device, essentially turning AHCIState into an "IP block".

Note that this change also frees AHCIState from the PCI dependency which wasn't reflected in Kconfig.

---
  hw/ide/ahci-internal.h    |  1 -
  include/hw/ide/ahci-pci.h |  2 ++
  include/hw/ide/ahci.h     |  2 --
  hw/ide/ahci.c             | 39 ++++-----------------------------------
  hw/ide/ich.c              | 19 +++++++++++++++----
  5 files changed, 21 insertions(+), 42 deletions(-)
Quoted 12 lines

hw/ide/ich.c static void pci_ich9_ahci_init(Object *obj) · line 118

Quoted 9 lines
  static void pci_ich9_reset(DeviceState *dev)
  {
      AHCIPCIState *d = ICH9_AHCI(dev);
hw/ide/ich.c +1 -0
@@ -102,7 +114,9 @@ static void pci_ich9_ahci_init(Object *obj)
  {
      AHCIPCIState *d = ICH9_AHCI(obj);
  +    qemu_init_irq(&d->irq, pci_ich9_ahci_update_irq, d, 0);
      ahci_init(&d->ahci, DEVICE(obj));
+    d->ahci.irq = &d->irq;

Pre-existing, but we shouldn't set this directly.
Does the IRQState belong to AHCIState?

AHCIState isn't an Object, and therefore can't have any properties, so we can only set it directly. In the SysBus devices, d->ahci.irq is treated with sysbus_init_irq(), so needs to stay a pointer.

I tried to convert AHCIState into a SysBusDevice in order to access it via these APIs, but that would create migration compatibility problems for the q35 machine which was a rabbit hole I didn't want to get into. So I settled on this solution. Any better proposals?

Best regards,
Bernhard

Quoted 18 lines
Quoted 17 lines
hw/ide/ich.c +0 -2
  }
    static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
@@ -125,8 +139,6 @@ static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
      /* XXX Software should program this register */
      dev->config[0x90]   = 1 << 6; /* Address Map Register - AHCI mode */
  -    d->ahci.irq = pci_allocate_irq(dev);
-
      pci_register_bar(dev, ICH9_IDP_BAR, PCI_BASE_ADDRESS_SPACE_IO,
                       &d->ahci.idp);
      pci_register_bar(dev, ICH9_MEM_BAR, PCI_BASE_ADDRESS_SPACE_MEMORY,
@@ -161,7 +173,6 @@ static void pci_ich9_uninit(PCIDevice *dev)
        msi_uninit(dev);
      ahci_uninit(&d->ahci);
-    qemu_free_irq(d->ahci.irq);
  }
    static void ich_ahci_class_init(ObjectClass *klass, void *data)
#4 lore raw

PATCH v4 2/2 hw/ide/ahci: Extract TYPE_SYSBUS_AHCI into dedicated file

In reply to Bernhard Beschow

Implement in dedicated file, just like TYPE_ICH9_AHCI.

---
 hw/ide/ahci-sysbus.c | 91 ++++++++++++++++++++++++++++++++++++++++++++
 hw/ide/ahci.c        | 67 --------------------------------
 hw/arm/Kconfig       | 10 ++---
 hw/ide/Kconfig       |  4 ++
 hw/ide/meson.build   |  1 +
 5 files changed, 101 insertions(+), 72 deletions(-)
 create mode 100644 hw/ide/ahci-sysbus.c
hw/ide/ahci-sysbus.c new file +91 -0
new file mode 100644
index 0000000000..d43db0923f
@@ -0,0 +1,91 @@
+/*
+ * QEMU AHCI Emulation (MMIO-mapped devices)
+ *
+ * Copyright (c) 2010 [email protected]
+ * Copyright (c) 2010 Roland Elek <[email protected]>
+ * Copyright (c) 2010 Sebastian Herbszt <[email protected]>
+ * Copyright (c) 2010 Alexander Graf <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "exec/address-spaces.h"
+#include "hw/qdev-properties.h"
+#include "migration/vmstate.h"
+
+#include "hw/ide/ahci-sysbus.h"
+#include "ahci-internal.h"
+
+static const VMStateDescription vmstate_sysbus_ahci = {
+    .name = "sysbus-ahci",
+    .fields = (const VMStateField[]) {
+        VMSTATE_AHCI(ahci, SysbusAHCIState),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void sysbus_ahci_reset(DeviceState *dev)
+{
+    SysbusAHCIState *s = SYSBUS_AHCI(dev);
+
+    ahci_reset(&s->ahci);
+}
+
+static void sysbus_ahci_init(Object *obj)
+{
+    SysbusAHCIState *s = SYSBUS_AHCI(obj);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+
+    ahci_init(&s->ahci, DEVICE(obj));
+
+    sysbus_init_mmio(sbd, &s->ahci.mem);
+    sysbus_init_irq(sbd, &s->ahci.irq);
+}
+
+static void sysbus_ahci_realize(DeviceState *dev, Error **errp)
+{
+    SysbusAHCIState *s = SYSBUS_AHCI(dev);
+
+    ahci_realize(&s->ahci, dev, &address_space_memory);
+}
+
+static Property sysbus_ahci_properties[] = {
+    DEFINE_PROP_UINT32("num-ports", SysbusAHCIState, ahci.ports, 1),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void sysbus_ahci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = sysbus_ahci_realize;
+    dc->vmsd = &vmstate_sysbus_ahci;
+    device_class_set_props(dc, sysbus_ahci_properties);
+    device_class_set_legacy_reset(dc, sysbus_ahci_reset);
+    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
+}
+
+static const TypeInfo sysbus_ahci_types[] = {
+    {
+        .name          = TYPE_SYSBUS_AHCI,
+        .parent        = TYPE_SYS_BUS_DEVICE,
+        .instance_size = sizeof(SysbusAHCIState),
+        .instance_init = sysbus_ahci_init,
+        .class_init    = sysbus_ahci_class_init,
+    },
+};
+
+DEFINE_TYPES(sysbus_ahci_types)
hw/ide/ahci.c +0 -67

86 lines not on this page · raw message

hw/arm/Kconfig +5 -5
index 1b25e73578..e779b5af95 100644
@@ -71,7 +71,7 @@ config HIGHBANK
     depends on TCG && ARM
     select A9MPCORE
     select A15MPCORE
-    select AHCI
+    select AHCI_SYSBUS
     select ARM_TIMER # sp804
     select ARM_V7M
     select PL011 if !HAVE_RUST # UART
@@ -192,7 +192,7 @@ config SBSA_REF
     depends on TCG && AARCH64
     imply PCI_DEVICES
     select DEVICE_TREE
-    select AHCI
+    select AHCI_SYSBUS
     select ARM_SMMUV3
     select GPIO_KEY
     select PCI_EXPRESS
@@ -319,7 +319,7 @@ config ARM_V7M
 
 config ALLWINNER_A10
     bool
-    select AHCI
+    select AHCI_SYSBUS
     select ALLWINNER_A10_PIT
     select ALLWINNER_A10_PIC
     select ALLWINNER_A10_CCM
@@ -352,7 +352,7 @@ config ALLWINNER_H3
 config ALLWINNER_R40
     bool
     default y if TCG && ARM
-    select AHCI
+    select AHCI_SYSBUS
     select ALLWINNER_SRAMC
     select ALLWINNER_A10_PIT
     select ALLWINNER_WDT
@@ -422,7 +422,7 @@ config XLNX_ZYNQMP_ARM
     bool
     default y if PIXMAN
     depends on TCG && AARCH64
-    select AHCI
+    select AHCI_SYSBUS
     select ARM_GIC
     select CADENCE
     select CPU_CLUSTER
hw/ide/Kconfig +4 -0
index 2e22b677da..b55507b836 100644
@@ -54,6 +54,10 @@ config AHCI_ICH9
     depends on PCI
     select AHCI
 
+config AHCI_SYSBUS
+    bool
+    select AHCI
+
 config IDE_SII3112
     bool
     select IDE_PCI
hw/ide/meson.build +1 -0
index 90ea861423..ddd7066040 100644
@@ -1,5 +1,6 @@
 system_ss.add(when: 'CONFIG_AHCI', if_true: files('ahci.c'))
 system_ss.add(when: 'CONFIG_AHCI_ICH9', if_true: files('ich.c'))
+system_ss.add(when: 'CONFIG_AHCI_SYSBUS', if_true: files('ahci-sysbus.c'))
 system_ss.add(when: 'CONFIG_ALLWINNER_A10', if_true: files('ahci-allwinner.c'))
 system_ss.add(when: 'CONFIG_IDE_BUS', if_true: files('ide-bus.c'))
 system_ss.add(when: 'CONFIG_IDE_CF', if_true: files('cf.c'))
Signature · 1 line
2.47.1