Skip to contentSkip to conversation

pc: make ACPI pcihp more reusable

PATCH In review Igor Mammedov 2 participants 7 messages Feb 3, 2014

lore
Share & tools
lore
#0 lore raw

v4 cover pc: make ACPI pcihp more reusable

changes since v3:
- keep "acpi-pci-hotplug-with-bridge-support" property
and pass its value to acpi_pcihp_init() to init internal
.legacy_piix field.
changes since v2:
- rename 'use_acpi_pci_hotplug' field to 'legacy_piix'
and corresponding properties
- drop excessive checks for legacy mode
- rework legacy vmstate handling to use AcpiPciHpPciStatus[0] structure
which reduced acpi/piix4.c by another 50 LOC
- move legacy initialization to pcihp.c
changes since v1:
- add BSEL property to root bus when in compatibility mode
as result dropped "hw:acpi:pcihp: assume root PCI bus if bus has no ACPI_PCIHP_PROP_BSEL property"
- rebasing on top of "pcihp: reduce number of device check events"
added patch 3/4.

Reuses new pcihp code for handling legacy PCI hotplug in acpi/piix4_pm, which significantly reduces code duplication between piix4_pm and pcihp reducing codebase by ~200 LOC.

It also allows cleaner rebase of generic hotplug refactoring, by not increasing code duplication since it will need to be done only for pcihp.

Git tree for testing based on mst's PCI tree:
https://github.com/imammedo/qemu/commits/pcihp_cleanup_v4

Igor Mammedov (5):
pcihp: replace enable|disable_device() with oneliners
pcihp: make PCI hotplug mmio handlers indifferent to PCI_HOTPLUG_ADDR
pcihp: make pci_read() mmio calback compatible with legacy ACPI
hotplug
pcihp: remove unused AcpiPciHpPciStatus.device_present field hw:piix4:acpi: reuse pcihp code for legacy PCI hotplug

 hw/acpi/pcihp.c         |   68 ++++++++-------
 hw/acpi/piix4.c         |  210 ++++-------------------------------------------
 include/hw/acpi/pcihp.h |    5 +-
 3 files changed, 55 insertions(+), 228 deletions(-)
#1 lore raw

PATCH v4 1/5 pcihp: replace enable|disable_device() with oneliners

enable_device() and disable_device() functions aren't reused anywere, so replace them with respective oneliners at call sites.

---
 hw/acpi/pcihp.c |   14 ++------------
 1 files changed, 2 insertions(+), 12 deletions(-)
hw/acpi/pcihp.c +2 -12
index 4345f5d..464739a 100644
@@ -177,16 +177,6 @@ void acpi_pcihp_reset(AcpiPciHpState *s)
     acpi_pcihp_update(s);
 }
 
-static void enable_device(AcpiPciHpState *s, unsigned bsel, int slot)
-{
-    s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
-}
-
-static void disable_device(AcpiPciHpState *s, unsigned bsel, int slot)
-{
-    s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
-}
-
 int acpi_pcihp_device_hotplug(AcpiPciHpState *s, PCIDevice *dev,
                               PCIHotplugState state)
 {
@@ -204,9 +194,9 @@ int acpi_pcihp_device_hotplug(AcpiPciHpState *s, PCIDevice *dev,
     }
 
     if (state == PCI_HOTPLUG_ENABLED) {
-        enable_device(s, bsel, slot);
+        s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
     } else {
-        disable_device(s, bsel, slot);
+        s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
     }
 
     return 0;
Signature · 1 line
1.7.1
#2 lore raw

PATCH v4 2/5 pcihp: make PCI hotplug mmio handlers indifferent to PCI_HOTPLUG_ADDR

In reply to Igor Mammedov

... removes dependency of mmio handler on PCI_HOTPLUG_ADDR. It will be needed in case of Q35 where base could be different.

---
 hw/acpi/pcihp.c |   24 ++++++++++++------------
 1 files changed, 12 insertions(+), 12 deletions(-)
hw/acpi/pcihp.c +12 -12
index 464739a..64c8cf2 100644
@@ -48,11 +48,11 @@
 
 #define PCI_HOTPLUG_ADDR 0xae00
 #define PCI_HOTPLUG_SIZE 0x0014
-#define PCI_UP_BASE 0xae00
-#define PCI_DOWN_BASE 0xae04
-#define PCI_EJ_BASE 0xae08
-#define PCI_RMV_BASE 0xae0c
-#define PCI_SEL_BASE 0xae10
+#define PCI_UP_BASE 0x0000
+#define PCI_DOWN_BASE 0x0004
+#define PCI_EJ_BASE 0x0008
+#define PCI_RMV_BASE 0x000c
+#define PCI_SEL_BASE 0x0010
 
 typedef struct AcpiPciHpFind {
     int bsel;
@@ -213,24 +213,24 @@ static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
     }
 
     switch (addr) {
-    case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_UP_BASE:
         val = s->acpi_pcihp_pci_status[bsel].up;
         s->acpi_pcihp_pci_status[bsel].up = 0;
         ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
         break;
-    case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_DOWN_BASE:
         val = s->acpi_pcihp_pci_status[bsel].down;
         ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
         break;
-    case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_EJ_BASE:
         /* No feature defined yet */
         ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
         break;
-    case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_RMV_BASE:
         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
         ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
         break;
-    case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_SEL_BASE:
         val = s->hotplug_select;
         ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
     default:
@@ -245,7 +245,7 @@ static void pci_write(void *opaque, hwaddr addr, uint64_t data,
 {
     AcpiPciHpState *s = opaque;
     switch (addr) {
-    case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_EJ_BASE:
         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
             break;
         }
@@ -253,7 +253,7 @@ static void pci_write(void *opaque, hwaddr addr, uint64_t data,
         ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
                       addr, data);
         break;
-    case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
+    case PCI_SEL_BASE:
         s->hotplug_select = data;
         ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
                       addr, data);
Signature · 1 line
1.7.1
#3 lore raw

PATCH v4 3/5 pcihp: make pci_read() mmio calback compatible with legacy ACPI hotplug

In reply to Igor Mammedov

due to recent change introduced by:
"pcihp: reduce number of device check events"

'up' field is cleared right after it's read. This is incompatible with legacy BIOS ACPI code where PCNF ACPI method reads this field 32 times.

To make pci_read mmio callback compatible with legacy 'up' behavior, pcihp code will need to know in which mode it runs add 'legacy_piix' field to AcpiPciHpState structure and alter register behavior accordingly.

---
v2:
- keep "acpi-pci-hotplug-with-bridge-support" property and tweek pcihp init API to initialize .legacy_piix field

---
 hw/acpi/pcihp.c         |    7 +++++--
 hw/acpi/piix4.c         |    3 ++-
 include/hw/acpi/pcihp.h |    3 ++-
 3 files changed, 9 insertions(+), 4 deletions(-)
hw/acpi/pcihp.c +5 -2
index 64c8cf2..974f01c 100644
@@ -215,7 +215,9 @@ static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
     switch (addr) {
     case PCI_UP_BASE:
         val = s->acpi_pcihp_pci_status[bsel].up;
-        s->acpi_pcihp_pci_status[bsel].up = 0;
+        if (!s->legacy_piix) {
+            s->acpi_pcihp_pci_status[bsel].up = 0;
+        }
         ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
         break;
     case PCI_DOWN_BASE:
@@ -273,9 +275,10 @@ static const MemoryRegionOps acpi_pcihp_io_ops = {
 };
 
 void acpi_pcihp_init(AcpiPciHpState *s, PCIBus *root_bus,
-                     MemoryRegion *address_space_io)
+                     MemoryRegion *address_space_io, bool bridges_enabled)
 {
     s->root= root_bus;
+    s->legacy_piix = !bridges_enabled;
     memory_region_init_io(&s->io, NULL, &acpi_pcihp_io_ops, s,
                           "acpi-pci-hotplug",
                           PCI_HOTPLUG_SIZE);
hw/acpi/piix4.c +2 -1
index 5d55a3c..2aedfe5 100644
@@ -695,7 +695,8 @@ static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
     memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
 
     if (s->use_acpi_pci_hotplug) {
-        acpi_pcihp_init(&s->acpi_pci_hotplug, bus, parent);
+        acpi_pcihp_init(&s->acpi_pci_hotplug, bus, parent,
+                        s->use_acpi_pci_hotplug);
     } else {
         memory_region_init_io(&s->io_pci, OBJECT(s), &piix4_pci_ops, s,
                               "acpi-pci-hotplug", PCI_HOTPLUG_SIZE);
include/hw/acpi/pcihp.h +2 -1
index aa297c2..02d3ce3 100644
@@ -46,10 +46,11 @@ typedef struct AcpiPciHpState {
     uint32_t hotplug_select;
     PCIBus *root;
     MemoryRegion io;
+    bool legacy_piix;
 } AcpiPciHpState;
 
 void acpi_pcihp_init(AcpiPciHpState *, PCIBus *root,
-                     MemoryRegion *address_space_io);
+                     MemoryRegion *address_space_io, bool bridges_enabled);
 
 /* Invoke on device hotplug */
 int acpi_pcihp_device_hotplug(AcpiPciHpState *, PCIDevice *,
Signature · 1 line
1.7.1
#4 lore raw

PATCH v4 4/5 pcihp: remove unused AcpiPciHpPciStatus.device_present field

In reply to Igor Mammedov

Remove now unused 'device_present' field wich was obsoleted by patch "pcihp: reduce number of device check events"

---
 include/hw/acpi/pcihp.h |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
include/hw/acpi/pcihp.h +0 -1
index 02d3ce3..1fd90e1 100644
@@ -35,7 +35,6 @@ typedef struct AcpiPciHpPciStatus {
     uint32_t up;
     uint32_t down;
     uint32_t hotplug_enable;
-    uint32_t device_present;
 } AcpiPciHpPciStatus;
 
 #define ACPI_PCIHP_PROP_BSEL "acpi-pcihp-bsel"
Signature · 1 line
1.7.1
#5 lore raw

PATCH v4 5/5 hw:piix4:acpi: reuse pcihp code for legacy PCI hotplug

In reply to Igor Mammedov

reduces acpi PCI hotplug code duplication by ~200LOC

---
v3:
- remove exessive checks for compat mode
- replace up/down fields of pci_status with corresponding fields form AcpiPciHpPciStatus in vmstate wich allow to remove extra 50 LOC of legacy code
- move legacy initialization logic to pcihp.c
v2:
- replace obsolete 'device_present' with 'up' field
- add/set ACPI_PCIHP_PROP_BSEL to 0 when running in compatibility mode with old machine types.

---
 hw/acpi/pcihp.c         |   23 ++++-
 hw/acpi/piix4.c         |  211 ++++-------------------------------------------
 include/hw/acpi/pcihp.h |    1 +
 3 files changed, 34 insertions(+), 201 deletions(-)
hw/acpi/pcihp.c +18 -5
index 974f01c..1ce6fc2 100644
@@ -46,8 +46,9 @@
 # define ACPI_PCIHP_DPRINTF(format, ...)     do { } while (0)
 #endif
 
-#define PCI_HOTPLUG_ADDR 0xae00
-#define PCI_HOTPLUG_SIZE 0x0014
+#define ACPI_PCIHP_ADDR 0xae00
+#define ACPI_PCIHP_SIZE 0x0014
+#define ACPI_PCIHP_LEGACY_SIZE 0x000f
 #define PCI_UP_BASE 0x0000
 #define PCI_DOWN_BASE 0x0004
 #define PCI_EJ_BASE 0x0008
@@ -277,12 +278,24 @@ static const MemoryRegionOps acpi_pcihp_io_ops = {
 void acpi_pcihp_init(AcpiPciHpState *s, PCIBus *root_bus,
                      MemoryRegion *address_space_io, bool bridges_enabled)
 {
+    uint16_t io_size = ACPI_PCIHP_SIZE;
+
     s->root= root_bus;
     s->legacy_piix = !bridges_enabled;
+
+    if (s->legacy_piix) {
+        unsigned *bus_bsel = g_malloc(sizeof *bus_bsel);
+
+        io_size = ACPI_PCIHP_LEGACY_SIZE;
+
+        *bus_bsel = ACPI_PCIHP_BSEL_DEFAULT;
+        object_property_add_uint32_ptr(OBJECT(root_bus), ACPI_PCIHP_PROP_BSEL,
+                                       bus_bsel, NULL);
+    }
+
     memory_region_init_io(&s->io, NULL, &acpi_pcihp_io_ops, s,
-                          "acpi-pci-hotplug",
-                          PCI_HOTPLUG_SIZE);
-    memory_region_add_subregion(address_space_io, PCI_HOTPLUG_ADDR, &s->io);
+                          "acpi-pci-hotplug", io_size);
+    memory_region_add_subregion(address_space_io, ACPI_PCIHP_ADDR, &s->io);
 }
 
 const VMStateDescription vmstate_acpi_pcihp_pci_status = {
hw/acpi/piix4.c +15 -196

287 lines not on this page · raw message

include/hw/acpi/pcihp.h +1 -0
index 1fd90e1..0a90e4a 100644
@@ -39,6 +39,7 @@ typedef struct AcpiPciHpPciStatus {
 
 #define ACPI_PCIHP_PROP_BSEL "acpi-pcihp-bsel"
 #define ACPI_PCIHP_MAX_HOTPLUG_BUS 256
+#define ACPI_PCIHP_BSEL_DEFAULT 0x0
 
 typedef struct AcpiPciHpState {
     AcpiPciHpPciStatus acpi_pcihp_pci_status[ACPI_PCIHP_MAX_HOTPLUG_BUS];
Signature · 1 line
1.7.1
#6 lore raw
In reply to Igor Mammedov
Quoted 8 lines · Igor Mammedov

changes since v3:
- keep "acpi-pci-hotplug-with-bridge-support" property and pass its value to acpi_pcihp_init() to init internal .legacy_piix field.
changes since v2:
- rename 'use_acpi_pci_hotplug' field to 'legacy_piix' and corresponding properties
- drop excessive checks for legacy mode

Quoted 8 lines

- rework legacy vmstate handling to use AcpiPciHpPciStatus[0] structure
which reduced acpi/piix4.c by another 50 LOC
- move legacy initialization to pcihp.c
changes since v1:
- add BSEL property to root bus when in compatibility mode
as result dropped "hw:acpi:pcihp: assume root PCI bus if bus has no ACPI_PCIHP_PROP_BSEL property"
- rebasing on top of "pcihp: reduce number of device check events"
added patch 3/4.

Looks good to me.
Applied, thanks a lot.

Quoted 25 lines

Reuses new pcihp code for handling legacy PCI hotplug in acpi/piix4_pm, which significantly reduces code duplication between piix4_pm and pcihp reducing codebase by ~200 LOC.

It also allows cleaner rebase of generic hotplug refactoring, by not increasing code duplication since it will need to be done only for pcihp.

Git tree for testing based on mst's PCI tree:
https://github.com/imammedo/qemu/commits/pcihp_cleanup_v4

Igor Mammedov (5):
pcihp: replace enable|disable_device() with oneliners
pcihp: make PCI hotplug mmio handlers indifferent to PCI_HOTPLUG_ADDR
pcihp: make pci_read() mmio calback compatible with legacy ACPI
hotplug
pcihp: remove unused AcpiPciHpPciStatus.device_present field hw:piix4:acpi: reuse pcihp code for legacy PCI hotplug

 hw/acpi/pcihp.c         |   68 ++++++++-------
 hw/acpi/piix4.c         |  210 ++++-------------------------------------------
 include/hw/acpi/pcihp.h |    5 +-
 3 files changed, 55 insertions(+), 228 deletions(-)