Skip to contentSkip to conversation

seabios: acpi: Add _STA for PCI hotplug slots

PATCH In review Alex Williamson 4 participants 8 messages Feb 24, 2012 – Mar 5, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seabios: acpi: Add _STA for PCI hotplug slots

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

---

 src/acpi-dsdt.dsl  |   36 ++-
 src/acpi-dsdt.hex  |  124 ++++++----
 src/acpi.c         |   27 ++
 src/ssdt-pcihp.dsl |    3 
 src/ssdt-pcihp.hex |  658 ++++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 686 insertions(+), 162 deletions(-)
src/acpi-dsdt.dsl +24 -12
index 7082b65..6b87086 100644
@@ -119,17 +119,15 @@ DefinitionBlock (
                prt_slot3(0x001f),
             })
 
-            OperationRegion(PCST, SystemIO, 0xae00, 0x08)
+            OperationRegion(PCST, SystemIO, 0xae00, 0x0c)
             Field (PCST, DWordAcc, NoLock, WriteAsZeros)
             {
-                PCIU, 32,
-                PCID, 32,
-            }
-
-            OperationRegion(SEJ, SystemIO, 0xae08, 0x04)
-            Field (SEJ, DWordAcc, NoLock, WriteAsZeros)
-            {
-                B0EJ, 32,
+                // PCI Up/ACK
+                PUPA, 32,
+                // PCI Down
+                PDWN, 32,
+                // PCI Present/Eject
+                PPEJ, 32,
             }
 
             Name (_CRS, ResourceTemplate ()
@@ -462,10 +460,20 @@ DefinitionBlock (
         /* Methods called by hotplug devices */
         Method (PCEJ, 1, NotSerialized) {
             // _EJ0 method - eject callback
-            Store(ShiftLeft(1, Arg0), B0EJ)
+            Store(ShiftLeft(1, Arg0), PPEJ)
             Return (0x0)
         }
 
+        Method (PSTA, 1, NotSerialized) {
+            Store(ShiftLeft(1, Arg0), PUPA)
+            Store(PPEJ, Local0)
+            If (And(Local0, ShiftLeft(1, Arg0))) {
+                Return(0x0F)
+            } Else {
+                Return(Zero)
+            }
+        }
+
 	/* Hotplug notification method supplied by SSDT */
 	External (\_SB.PCI0.PCNT, MethodObj)
 
@@ -473,12 +481,16 @@ DefinitionBlock (
         Method(PCNF, 0) {
             // Local0 = iterator
             Store (Zero, Local0)
+            // Local1 = slots marked "up"
+            Store (PUPA, Local1)
+            // Local2 = slots marked "down"
+            Store (PDWN, Local2)
             While (LLess(Local0, 31)) {
                 Increment(Local0)
-                If (And(PCIU, ShiftLeft(1, Local0))) {
+                If (And(Local1, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 1)
                 }
-                If (And(PCID, ShiftLeft(1, Local0))) {
+                If (And(Local2, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 3)
                 }
             }
src/acpi-dsdt.hex +74 -50

203 lines not on this page · raw message

src/acpi.c +23 -4
index 107469f..056270b 100644
@@ -486,13 +486,14 @@ build_ssdt(void)
 
 #include "ssdt-pcihp.hex"
 
-#define PCI_RMV_BASE 0xae0c
+#define PCI_HOTPLUG 0xae0c
+#define PCI_PRES_EJ 0xae08
 
 extern void link_time_assertion(void);
 
 static void* build_pcihp(void)
 {
-    u32 rmvc_pcrm;
+    u32 hotpluggable, present;
     int i;
 
     u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
@@ -504,7 +505,7 @@ static void* build_pcihp(void)
         link_time_assertion();
     }
 
-    rmvc_pcrm = inl(PCI_RMV_BASE);
+    hotpluggable = inl(PCI_HOTPLUG);
     for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
         /* Slot is in byte 2 in _ADR */
         u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
@@ -514,11 +515,29 @@ static void* build_pcihp(void)
             free(ssdt);
             return NULL;
         }
-        if (!(rmvc_pcrm & (0x1 << slot))) {
+        if (!(hotpluggable & (0x1 << slot))) {
             memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
         }
     }
 
+    /* Runtime patching of STA.  If running on system that
+     * doesn't support the Present/Eject field, replace _STA
+     * with STA_ */
+    if (ARRAY_SIZE(aml_sta_name) != ARRAY_SIZE(aml_adr_dword)) {
+        link_time_assertion();
+    }
+
+    present = inl(PCI_PRES_EJ);
+    for (i = 0; present == 0 && i < ARRAY_SIZE(aml_sta_name); ++i) {
+        /* Sanity check */
+        if (memcmp(ssdp_pcihp_aml + aml_sta_name[i], "_STA", 4)) {
+            warn_internalerror();
+            free(ssdt);
+            return NULL;
+        }
+        memcpy(ssdt + aml_sta_name[i], "STA_", 4);
+    }
+
     return ssdt;
 }
 
src/ssdt-pcihp.dsl +3 -0
index 4b435b8..376476a 100644
@@ -10,6 +10,7 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
     /* Objects supplied by DSDT */
     External (\_SB.PCI0, DeviceObj)
     External (\_SB.PCI0.PCEJ, MethodObj)
+    External (\_SB.PCI0.PSTA, MethodObj)
 
     Scope(\_SB.PCI0) {
         /* Bulk generated PCI hotplug devices */
@@ -23,6 +24,8 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
            Name (_ADR, 0x##slot##0000)                  \
            ACPI_EXTRACT_METHOD_STRING aml_ej0_name      \
            Method (_EJ0, 1) { Return(PCEJ(0x##slot)) }  \
+           ACPI_EXTRACT_METHOD_STRING aml_sta_name      \
+           Method (_STA, 0) { Return(PSTA(0x##slot)) }  \
            Name (_SUN, 0x##slot)                        \
         }
 
src/ssdt-pcihp.hex +562 -96

1,053 lines not on this page · raw message

#1 lore raw
Quoted 15 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

Thanks. The patch looks fine to me. However, since I'm not all that familiar with the details of the qemu/kvm support for this, I'd like to see an Ack from one of the qemu or kvm maintainers.

-Kevin

#2 lore raw
In reply to Alex Williamson
Quoted 5 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

Quoted 9 lines

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

Interesting. Isn't the UP register sufficient for _STA?

Assuming we want to implement _STA - for which the only motivation seems the handshake hack below.

Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

This part looks a bit like a hack.
_STA is not intended as an acknowledgement - it's a query for state. ACPI spec 5.0 requires that _STA is called before _INI, but ACPI 1.0b doesn't. Did you try some 1.0 OSPMs (e.g. XP) to see what they do?

I also think I see how this can cause a race, see below.

Quoted 2 lines

Your description of the qemu patches made me think that all you really want is detect an OS without OSPM. If that is the case, I would suggest adding an _INI method at top level as a simpler and more robust procedure.

Otherwise, how about implementing _PS0
(and probably _PS3) to manage slot power? Maybe this what you are really after, and it seems like a better interface than 'acknowledge' which does not seem to make sense for real hardware.

Quoted 25 lines

---

 src/acpi-dsdt.dsl  |   36 ++-
 src/acpi-dsdt.hex  |  124 ++++++----
 src/acpi.c         |   27 ++
 src/ssdt-pcihp.dsl |    3 
 src/ssdt-pcihp.hex |  658 ++++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 686 insertions(+), 162 deletions(-)
src/acpi-dsdt.dsl +1 -5
index 7082b65..6b87086 100644
@@ -119,17 +119,15 @@ DefinitionBlock (
                prt_slot3(0x001f),
             })
 
-            OperationRegion(PCST, SystemIO, 0xae00, 0x08)
+            OperationRegion(PCST, SystemIO, 0xae00, 0x0c)
             Field (PCST, DWordAcc, NoLock, WriteAsZeros)
             {
-                PCIU, 32,
-                PCID, 32,
-            }
-

src/acpi-dsdt.dsl DefinitionBlock ( · line 130

Quoted 10 lines
src/acpi-dsdt.dsl +6 -4
-            OperationRegion(SEJ, SystemIO, 0xae08, 0x04)
-            Field (SEJ, DWordAcc, NoLock, WriteAsZeros)
-            {
-                B0EJ, 32,
+                // PCI Up/ACK
+                PUPA, 32,
+                // PCI Down
+                PDWN, 32,
+                // PCI Present/Eject
+                PPEJ, 32,

Note on the comment: this only affects bus0 not all of PCI.

Quoted 4 lines
src/acpi-dsdt.dsl +0 -0
             }
 
             Name (_CRS, ResourceTemplate ()

src/acpi-dsdt.dsl DefinitionBlock ( · line 468

Quoted 10 lines
src/acpi-dsdt.dsl +3 -1
@@ -462,10 +460,20 @@ DefinitionBlock (
         /* Methods called by hotplug devices */
         Method (PCEJ, 1, NotSerialized) {
             // _EJ0 method - eject callback
-            Store(ShiftLeft(1, Arg0), B0EJ)
+            Store(ShiftLeft(1, Arg0), PPEJ)
             Return (0x0)
         }
 
+        Method (PSTA, 1, NotSerialized) {
+            Store(ShiftLeft(1, Arg0), PUPA)

So this looks wrong to me.

Specifically _STA is also called at the end after _EJ0. If the device is ejected then insterted, you get a window where _STA is called and hardware will think insertion was acknowledged, while in fact ejection was acknowledged.

I also think a request for the OS to rescan the bus will trigger _STA calls. Same race can get triggered.

Quoted 20 lines
src/acpi-dsdt.dsl +12 -0
+            Store(PPEJ, Local0)
+            If (And(Local0, ShiftLeft(1, Arg0))) {
+                Return(0x0F)
+            } Else {
+                Return(Zero)
+            }
+        }
+
 	/* Hotplug notification method supplied by SSDT */
 	External (\_SB.PCI0.PCNT, MethodObj)
 
@@ -473,12 +481,16 @@ DefinitionBlock (
         Method(PCNF, 0) {
             // Local0 = iterator
             Store (Zero, Local0)
+            // Local1 = slots marked "up"
+            Store (PUPA, Local1)
+            // Local2 = slots marked "down"
+            Store (PDWN, Local2)
             While (LLess(Local0, 31)) {

src/acpi-dsdt.dsl DefinitionBlock ( · line 496

Quoted 10 lines
src/acpi-dsdt.dsl +2 -2
                 Increment(Local0)
-                If (And(PCIU, ShiftLeft(1, Local0))) {
+                If (And(Local1, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 1)
                 }
-                If (And(PCID, ShiftLeft(1, Local0))) {
+                If (And(Local2, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 3)
                 }
             }

Nothing wrong here but should be a separate patch?

src/acpi-dsdt.hex static unsigned char AmlCode[] = { · line 8

Quoted 12 lines
src/acpi-dsdt.hex +1 -1
index 5dc7bb4..6d99f53 100644
@@ -3,12 +3,12 @@ static unsigned char AmlCode[] = {
 0x53,
 0x44,
 0x54,
-0xd3,
+0xeb,
 0x10,
 0x0,

...

I'd rather not see this part on list.

Quoted 87 lines
src/acpi.c +23 -4
index 107469f..056270b 100644
@@ -486,13 +486,14 @@ build_ssdt(void)
 
 #include "ssdt-pcihp.hex"
 
-#define PCI_RMV_BASE 0xae0c
+#define PCI_HOTPLUG 0xae0c
+#define PCI_PRES_EJ 0xae08
 
 extern void link_time_assertion(void);
 
 static void* build_pcihp(void)
 {
-    u32 rmvc_pcrm;
+    u32 hotpluggable, present;
     int i;
 
     u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
@@ -504,7 +505,7 @@ static void* build_pcihp(void)
         link_time_assertion();
     }
 
-    rmvc_pcrm = inl(PCI_RMV_BASE);
+    hotpluggable = inl(PCI_HOTPLUG);
     for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
         /* Slot is in byte 2 in _ADR */
         u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
@@ -514,11 +515,29 @@ static void* build_pcihp(void)
             free(ssdt);
             return NULL;
         }
-        if (!(rmvc_pcrm & (0x1 << slot))) {
+        if (!(hotpluggable & (0x1 << slot))) {
             memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
         }
     }
 
+    /* Runtime patching of STA.  If running on system that
+     * doesn't support the Present/Eject field, replace _STA
+     * with STA_ */
+    if (ARRAY_SIZE(aml_sta_name) != ARRAY_SIZE(aml_adr_dword)) {
+        link_time_assertion();
+    }
+
+    present = inl(PCI_PRES_EJ);
+    for (i = 0; present == 0 && i < ARRAY_SIZE(aml_sta_name); ++i) {
+        /* Sanity check */
+        if (memcmp(ssdp_pcihp_aml + aml_sta_name[i], "_STA", 4)) {
+            warn_internalerror();
+            free(ssdt);
+            return NULL;
+        }
+        memcpy(ssdt + aml_sta_name[i], "STA_", 4);
+    }
+
     return ssdt;
 }
 
src/ssdt-pcihp.dsl +3 -0
index 4b435b8..376476a 100644
@@ -10,6 +10,7 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
     /* Objects supplied by DSDT */
     External (\_SB.PCI0, DeviceObj)
     External (\_SB.PCI0.PCEJ, MethodObj)
+    External (\_SB.PCI0.PSTA, MethodObj)
 
     Scope(\_SB.PCI0) {
         /* Bulk generated PCI hotplug devices */
@@ -23,6 +24,8 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
            Name (_ADR, 0x##slot##0000)                  \
            ACPI_EXTRACT_METHOD_STRING aml_ej0_name      \
            Method (_EJ0, 1) { Return(PCEJ(0x##slot)) }  \
+           ACPI_EXTRACT_METHOD_STRING aml_sta_name      \
+           Method (_STA, 0) { Return(PSTA(0x##slot)) }  \
            Name (_SUN, 0x##slot)                        \
         }
 
src/ssdt-pcihp.hex +0 -0
index b15ad5a..f060c94 100644

src/ssdt-pcihp.hex line 7

Quoted 7 lines
src/ssdt-pcihp.hex +0 -5
@@ -1,80 +1,113 @@
 static unsigned short aml_adr_dword[] = {
 0x3e,
-0x62,
-0x88,
-0xae,
-0xd4,
-0xfa,

....

I'd rather not see this part in the patches on list.

#3 lore raw
Quoted 16 lines · Michael S. Tsirkin
Quoted 5 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

Quoted 9 lines

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

Quoted 1 line

Interesting. Isn't the UP register sufficient for _STA?

No, UP only reports the current slot being added, so we'd only be able to report a "present" value for that slot and not static or previously added slots.

Quoted 11 lines

Assuming we want to implement _STA - for which the only motivation seems the handshake hack below.

Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

This part looks a bit like a hack.
_STA is not intended as an acknowledgement - it's a query for state. ACPI spec 5.0 requires that _STA is called before _INI, but ACPI 1.0b doesn't. Did you try some 1.0 OSPMs (e.g. XP) to see what they do?

I did test with XP. Section 6.3 of ACPI spec 1.0b references the _STA method during hotplug. I also found this reference for Windows ACPI procedure for hotplug/unplug:

http://www.microsoft.com/china/whdc/system/pnppwr/hotadd/hotplugpci.mspx#EYH

I agree, _STA is not intended as an acknowledgment, but that doesn't mean we can't use it as one. The OSPM can call _STA at any point in time, however calling it after we've done a notify for device check is about the best indication we can get that the OSPM is processing it. It doesn't hurt anything if _STA is called spuriously.

Quoted 10 lines

I also think I see how this can cause a race, see below.

Quoted 2 lines

Your description of the qemu patches made me think that all you really want is detect an OS without OSPM. If that is the case, I would suggest adding an _INI method at top level as a simpler and more robust procedure.

No, having OSPM is a prerequisite, but does not imply supporting hotplug.

Quoted 5 lines

Otherwise, how about implementing _PS0
(and probably _PS3) to manage slot power? Maybe this what you are really after, and it seems like a better interface than 'acknowledge' which does not seem to make sense for real hardware.

I tried this, _PS0/3 also requires _STA. Implementing both caused interrupts to stop working on Linux guests. Note that _PS0/3 is even less closely associated with device removal in 1.0b than _STA even though the MSFT document references it.

Quoted 29 lines
Quoted 29 lines

---

 src/acpi-dsdt.dsl  |   36 ++-
 src/acpi-dsdt.hex  |  124 ++++++----
 src/acpi.c         |   27 ++
 src/ssdt-pcihp.dsl |    3 
 src/ssdt-pcihp.hex |  658 ++++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 686 insertions(+), 162 deletions(-)
src/acpi-dsdt.dsl +1 -9
index 7082b65..6b87086 100644
@@ -119,17 +119,15 @@ DefinitionBlock (
                prt_slot3(0x001f),
             })
 
-            OperationRegion(PCST, SystemIO, 0xae00, 0x08)
+            OperationRegion(PCST, SystemIO, 0xae00, 0x0c)
             Field (PCST, DWordAcc, NoLock, WriteAsZeros)
             {
-                PCIU, 32,
-                PCID, 32,
-            }
-
-            OperationRegion(SEJ, SystemIO, 0xae08, 0x04)
-            Field (SEJ, DWordAcc, NoLock, WriteAsZeros)
-            {
-                B0EJ, 32,
Quoted 8 lines

src/acpi-dsdt.dsl DefinitionBlock ( · line 130

Quoted 6 lines
src/acpi-dsdt.dsl +6 -0
+                // PCI Up/ACK
+                PUPA, 32,
+                // PCI Down
+                PDWN, 32,
+                // PCI Present/Eject
+                PPEJ, 32,

Note on the comment: this only affects bus0 not all of PCI.

As has always been the case.

Quoted 17 lines
Quoted 4 lines
src/acpi-dsdt.dsl +0 -0
             }
 
             Name (_CRS, ResourceTemplate ()

src/acpi-dsdt.dsl DefinitionBlock ( · line 468

Quoted 10 lines
src/acpi-dsdt.dsl +3 -1
@@ -462,10 +460,20 @@ DefinitionBlock (
         /* Methods called by hotplug devices */
         Method (PCEJ, 1, NotSerialized) {
             // _EJ0 method - eject callback
-            Store(ShiftLeft(1, Arg0), B0EJ)
+            Store(ShiftLeft(1, Arg0), PPEJ)
             Return (0x0)
         }
 
+        Method (PSTA, 1, NotSerialized) {
+            Store(ShiftLeft(1, Arg0), PUPA)

So this looks wrong to me.

Quoted 5 lines

Specifically _STA is also called at the end after _EJ0. If the device is ejected then insterted, you get a window where _STA is called and hardware will think insertion was acknowledged, while in fact ejection was acknowledged.

The qemu patch doesn't allow an insertion while an eject is pending.

Quoted 2 lines

I also think a request for the OS to rescan the bus will trigger _STA calls. Same race can get triggered.

Spurious _STA calls don't matter, they'll clear a bit that wasn't set in the UP register anyway. If there's a race with the hotplug SCI, ie. we've set UP, but OSPM performs a rescan, they'll noticed _STA now reports the device is present and I think that should lead to the proper result.

Quoted 25 lines
Quoted 25 lines
src/acpi-dsdt.dsl +13 -1
+            Store(PPEJ, Local0)
+            If (And(Local0, ShiftLeft(1, Arg0))) {
+                Return(0x0F)
+            } Else {
+                Return(Zero)
+            }
+        }
+
 	/* Hotplug notification method supplied by SSDT */
 	External (\_SB.PCI0.PCNT, MethodObj)
 
@@ -473,12 +481,16 @@ DefinitionBlock (
         Method(PCNF, 0) {
             // Local0 = iterator
             Store (Zero, Local0)
+            // Local1 = slots marked "up"
+            Store (PUPA, Local1)
+            // Local2 = slots marked "down"
+            Store (PDWN, Local2)
             While (LLess(Local0, 31)) {
                 Increment(Local0)
-                If (And(PCIU, ShiftLeft(1, Local0))) {
+                If (And(Local1, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 1)
Quoted 8 lines

src/acpi-dsdt.dsl DefinitionBlock ( · line 496

Quoted 6 lines
src/acpi-dsdt.dsl +1 -1
                 }
-                If (And(PCID, ShiftLeft(1, Local0))) {
+                If (And(Local2, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 3)
                 }
             }

Nothing wrong here but should be a separate patch?

It was pretty trivial, but I can split it if needed.

Quoted 15 lines
Quoted 12 lines
src/acpi-dsdt.hex +1 -1
index 5dc7bb4..6d99f53 100644
@@ -3,12 +3,12 @@ static unsigned char AmlCode[] = {
 0x53,
 0x44,
 0x54,
-0xd3,
+0xeb,
 0x10,
 0x0,

...

Quoted 1 line

I'd rather not see this part on list.

Then it should be .gitignore'd. I'm just following the lead of previous patches in this space.

Quoted 96 lines
Quoted 94 lines
src/acpi.c +23 -4
index 107469f..056270b 100644
@@ -486,13 +486,14 @@ build_ssdt(void)
 
 #include "ssdt-pcihp.hex"
 
-#define PCI_RMV_BASE 0xae0c
+#define PCI_HOTPLUG 0xae0c
+#define PCI_PRES_EJ 0xae08
 
 extern void link_time_assertion(void);
 
 static void* build_pcihp(void)
 {
-    u32 rmvc_pcrm;
+    u32 hotpluggable, present;
     int i;
 
     u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
@@ -504,7 +505,7 @@ static void* build_pcihp(void)
         link_time_assertion();
     }
 
-    rmvc_pcrm = inl(PCI_RMV_BASE);
+    hotpluggable = inl(PCI_HOTPLUG);
     for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
         /* Slot is in byte 2 in _ADR */
         u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
@@ -514,11 +515,29 @@ static void* build_pcihp(void)
             free(ssdt);
             return NULL;
         }
-        if (!(rmvc_pcrm & (0x1 << slot))) {
+        if (!(hotpluggable & (0x1 << slot))) {
             memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
         }
     }
 
+    /* Runtime patching of STA.  If running on system that
+     * doesn't support the Present/Eject field, replace _STA
+     * with STA_ */
+    if (ARRAY_SIZE(aml_sta_name) != ARRAY_SIZE(aml_adr_dword)) {
+        link_time_assertion();
+    }
+
+    present = inl(PCI_PRES_EJ);
+    for (i = 0; present == 0 && i < ARRAY_SIZE(aml_sta_name); ++i) {
+        /* Sanity check */
+        if (memcmp(ssdp_pcihp_aml + aml_sta_name[i], "_STA", 4)) {
+            warn_internalerror();
+            free(ssdt);
+            return NULL;
+        }
+        memcpy(ssdt + aml_sta_name[i], "STA_", 4);
+    }
+
     return ssdt;
 }
 
src/ssdt-pcihp.dsl +3 -0
index 4b435b8..376476a 100644
@@ -10,6 +10,7 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
     /* Objects supplied by DSDT */
     External (\_SB.PCI0, DeviceObj)
     External (\_SB.PCI0.PCEJ, MethodObj)
+    External (\_SB.PCI0.PSTA, MethodObj)
 
     Scope(\_SB.PCI0) {
         /* Bulk generated PCI hotplug devices */
@@ -23,6 +24,8 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
            Name (_ADR, 0x##slot##0000)                  \
            ACPI_EXTRACT_METHOD_STRING aml_ej0_name      \
            Method (_EJ0, 1) { Return(PCEJ(0x##slot)) }  \
+           ACPI_EXTRACT_METHOD_STRING aml_sta_name      \
+           Method (_STA, 0) { Return(PSTA(0x##slot)) }  \
            Name (_SUN, 0x##slot)                        \
         }
 
src/ssdt-pcihp.hex +0 -5
index b15ad5a..f060c94 100644
@@ -1,80 +1,113 @@
 static unsigned short aml_adr_dword[] = {
 0x3e,
-0x62,
-0x88,
-0xae,
-0xd4,
-0xfa,

....

Quoted 1 line

I'd rather not see this part in the patches on list.

Same. Thanks,

Alex

#4 lore raw
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

Quoted 5 lines
Quoted 1 line

Interesting. Isn't the UP register sufficient for _STA?

No, UP only reports the current slot being added, so we'd only be able to report a "present" value for that slot and not static or previously added slots.

It's probably a bug in qemu - for example, if two slots are added quickly we just lose the notification about the first one, right?

The fix might involve making e.g. the UP register write 1 to clear, but it needs to be cleared on Notify, not on _STA.

Quoted 16 lines
Quoted 11 lines

Assuming we want to implement _STA - for which the only motivation seems the handshake hack below.

Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

This part looks a bit like a hack.
_STA is not intended as an acknowledgement - it's a query for state. ACPI spec 5.0 requires that _STA is called before _INI, but ACPI 1.0b doesn't. Did you try some 1.0 OSPMs (e.g. XP) to see what they do?

I did test with XP. Section 6.3 of ACPI spec 1.0b references the _STA method during hotplug. I also found this reference for Windows ACPI procedure for hotplug/unplug:

Quoted 6 lines

http://www.microsoft.com/china/whdc/system/pnppwr/hotadd/hotplugpci.mspx#EYH

I agree, _STA is not intended as an acknowledgment, but that doesn't mean we can't use it as one. The OSPM can call _STA at any point in time, however calling it after we've done a notify for device check is about the best indication we can get that the OSPM is processing it.

How about an actual access to the slot? The event that we send is just a change event. Guest accesses the slot to see whether any devices
are present. No ACPI or interface changes are needed to detect this.

Quoted 1 line

It doesn't hurt anything if _STA is called spuriously.

It makes its use as an acknowledgment unreliable:
_STA called does *not* mean OSPM saw your hotplug
event.

Quoted 5 lines
Quoted 5 lines

I also think I see how this can cause a race, see below.

Quoted 2 lines
Quoted 8 lines
Quoted 5 lines

Your description of the qemu patches made me think that all you really want is detect an OS without OSPM. If that is the case, I would suggest adding an _INI method at top level as a simpler and more robust procedure.

No, having OSPM is a prerequisite, but does not imply supporting hotplug.

It does not? What are the OSPMs that ignore hotplug? Hotplug has been defined since ACPI 1.0 so this seems strange. But it will me much easier to discuss whether a specific hack is efficient if you define the specific bug this handshake tries to work around.

Quoted 7 lines
Quoted 5 lines

Otherwise, how about implementing _PS0
(and probably _PS3) to manage slot power? Maybe this what you are really after, and it seems like a better interface than 'acknowledge' which does not seem to make sense for real hardware.

I tried this, _PS0/3 also requires _STA.

Interesting. Why does it require _STA?

Quoted 2 lines

Implementing both caused
interrupts to stop working on Linux guests.

So there's some bug then? Let's fix?

Quoted 3 lines

Note that _PS0/3 is even
less closely associated with device removal in 1.0b than _STA even though the MSFT document references it.

ACPI spec references it :)
It seems clear that _PS0 must be called before device is used, otherwise the slot has no power.

There's also _OST - linux doesn't implement it but it seems modern windows versions do.

Quoted 38 lines
Quoted 29 lines
Quoted 29 lines

---

 src/acpi-dsdt.dsl  |   36 ++-
 src/acpi-dsdt.hex  |  124 ++++++----
 src/acpi.c         |   27 ++
 src/ssdt-pcihp.dsl |    3 
 src/ssdt-pcihp.hex |  658 ++++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 686 insertions(+), 162 deletions(-)
src/acpi-dsdt.dsl +1 -9
index 7082b65..6b87086 100644
@@ -119,17 +119,15 @@ DefinitionBlock (
                prt_slot3(0x001f),
             })
 
-            OperationRegion(PCST, SystemIO, 0xae00, 0x08)
+            OperationRegion(PCST, SystemIO, 0xae00, 0x0c)
             Field (PCST, DWordAcc, NoLock, WriteAsZeros)
             {
-                PCIU, 32,
-                PCID, 32,
-            }
-
-            OperationRegion(SEJ, SystemIO, 0xae08, 0x04)
-            Field (SEJ, DWordAcc, NoLock, WriteAsZeros)
-            {
-                B0EJ, 32,
Quoted 8 lines

src/acpi-dsdt.dsl DefinitionBlock ( · line 130

Quoted 6 lines
src/acpi-dsdt.dsl +6 -0
+                // PCI Up/ACK
+                PUPA, 32,
+                // PCI Down
+                PDWN, 32,
+                // PCI Present/Eject
+                PPEJ, 32,

Note on the comment: this only affects bus0 not all of PCI.

Quoted 1 line

As has always been the case.

But your comment implies otherwise :)

Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
src/acpi-dsdt.dsl +0 -0
             }
 
             Name (_CRS, ResourceTemplate ()

src/acpi-dsdt.dsl DefinitionBlock ( · line 468

Quoted 10 lines
src/acpi-dsdt.dsl +3 -1
@@ -462,10 +460,20 @@ DefinitionBlock (
         /* Methods called by hotplug devices */
         Method (PCEJ, 1, NotSerialized) {
             // _EJ0 method - eject callback
-            Store(ShiftLeft(1, Arg0), B0EJ)
+            Store(ShiftLeft(1, Arg0), PPEJ)
             Return (0x0)
         }
 
+        Method (PSTA, 1, NotSerialized) {
+            Store(ShiftLeft(1, Arg0), PUPA)

So this looks wrong to me.

Quoted 5 lines

Specifically _STA is also called at the end after _EJ0. If the device is ejected then insterted, you get a window where _STA is called and hardware will think insertion was acknowledged, while in fact ejection was acknowledged.

The qemu patch doesn't allow an insertion while an eject is pending.

Quoted 8 lines
Quoted 2 lines

I also think a request for the OS to rescan the bus will trigger _STA calls. Same race can get triggered.

Spurious _STA calls don't matter, they'll clear a bit that wasn't set in the UP register anyway. If there's a race with the hotplug SCI, ie. we've set UP, but OSPM performs a rescan, they'll noticed _STA now reports the device is present and I think that should lead to the proper result.

To be more explicit:

_EJ0

host adds new device

_STA triggered to check _EJ0 success

You now think OSPM acknowledged new device with _STA but it didn't. OSPM thinks that _EJ0 failed but it didn't.

Quoted 51 lines
Quoted 25 lines
Quoted 25 lines
src/acpi-dsdt.dsl +13 -1
+            Store(PPEJ, Local0)
+            If (And(Local0, ShiftLeft(1, Arg0))) {
+                Return(0x0F)
+            } Else {
+                Return(Zero)
+            }
+        }
+
 	/* Hotplug notification method supplied by SSDT */
 	External (\_SB.PCI0.PCNT, MethodObj)
 
@@ -473,12 +481,16 @@ DefinitionBlock (
         Method(PCNF, 0) {
             // Local0 = iterator
             Store (Zero, Local0)
+            // Local1 = slots marked "up"
+            Store (PUPA, Local1)
+            // Local2 = slots marked "down"
+            Store (PDWN, Local2)
             While (LLess(Local0, 31)) {
                 Increment(Local0)
-                If (And(PCIU, ShiftLeft(1, Local0))) {
+                If (And(Local1, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 1)
Quoted 8 lines

src/acpi-dsdt.dsl DefinitionBlock ( · line 496

Quoted 6 lines
src/acpi-dsdt.dsl +1 -1
                 }
-                If (And(PCID, ShiftLeft(1, Local0))) {
+                If (And(Local2, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 3)
                 }
             }

Nothing wrong here but should be a separate patch?

It was pretty trivial, but I can split it if needed.

Quoted 15 lines
Quoted 12 lines
src/acpi-dsdt.hex +1 -1
index 5dc7bb4..6d99f53 100644
@@ -3,12 +3,12 @@ static unsigned char AmlCode[] = {
 0x53,
 0x44,
 0x54,
-0xd3,
+0xeb,
 0x10,
 0x0,

...

Quoted 3 lines
Quoted 1 line

I'd rather not see this part on list.

Then it should be .gitignore'd.

No, it's in git so people without iasl can build the bios.

Quoted 2 lines

I'm just following the lead of previous
patches in this space.

People tend to forget these blobs (I do sometimes) but it makes review awkward.

Quoted 103 lines
Quoted 96 lines
Quoted 94 lines
src/acpi.c +23 -4
index 107469f..056270b 100644
@@ -486,13 +486,14 @@ build_ssdt(void)
 
 #include "ssdt-pcihp.hex"
 
-#define PCI_RMV_BASE 0xae0c
+#define PCI_HOTPLUG 0xae0c
+#define PCI_PRES_EJ 0xae08
 
 extern void link_time_assertion(void);
 
 static void* build_pcihp(void)
 {
-    u32 rmvc_pcrm;
+    u32 hotpluggable, present;
     int i;
 
     u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
@@ -504,7 +505,7 @@ static void* build_pcihp(void)
         link_time_assertion();
     }
 
-    rmvc_pcrm = inl(PCI_RMV_BASE);
+    hotpluggable = inl(PCI_HOTPLUG);
     for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
         /* Slot is in byte 2 in _ADR */
         u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
@@ -514,11 +515,29 @@ static void* build_pcihp(void)
             free(ssdt);
             return NULL;
         }
-        if (!(rmvc_pcrm & (0x1 << slot))) {
+        if (!(hotpluggable & (0x1 << slot))) {
             memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
         }
     }
 
+    /* Runtime patching of STA.  If running on system that
+     * doesn't support the Present/Eject field, replace _STA
+     * with STA_ */
+    if (ARRAY_SIZE(aml_sta_name) != ARRAY_SIZE(aml_adr_dword)) {
+        link_time_assertion();
+    }
+
+    present = inl(PCI_PRES_EJ);
+    for (i = 0; present == 0 && i < ARRAY_SIZE(aml_sta_name); ++i) {
+        /* Sanity check */
+        if (memcmp(ssdp_pcihp_aml + aml_sta_name[i], "_STA", 4)) {
+            warn_internalerror();
+            free(ssdt);
+            return NULL;
+        }
+        memcpy(ssdt + aml_sta_name[i], "STA_", 4);
+    }
+
     return ssdt;
 }
 
src/ssdt-pcihp.dsl +3 -0
index 4b435b8..376476a 100644
@@ -10,6 +10,7 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
     /* Objects supplied by DSDT */
     External (\_SB.PCI0, DeviceObj)
     External (\_SB.PCI0.PCEJ, MethodObj)
+    External (\_SB.PCI0.PSTA, MethodObj)
 
     Scope(\_SB.PCI0) {
         /* Bulk generated PCI hotplug devices */
@@ -23,6 +24,8 @@ DefinitionBlock ("ssdt-pcihp.aml", "SSDT", 0x01, "BXPC", "BXSSDTPCIHP", 0x1)
            Name (_ADR, 0x##slot##0000)                  \
            ACPI_EXTRACT_METHOD_STRING aml_ej0_name      \
            Method (_EJ0, 1) { Return(PCEJ(0x##slot)) }  \
+           ACPI_EXTRACT_METHOD_STRING aml_sta_name      \
+           Method (_STA, 0) { Return(PSTA(0x##slot)) }  \
            Name (_SUN, 0x##slot)                        \
         }
 
src/ssdt-pcihp.hex +0 -5
index b15ad5a..f060c94 100644
@@ -1,80 +1,113 @@
 static unsigned short aml_adr_dword[] = {
 0x3e,
-0x62,
-0x88,
-0xae,
-0xd4,
-0xfa,

....

Quoted 1 line

I'd rather not see this part in the patches on list.

Same. Thanks,

Alex

#6 lore raw
In reply to Alex Williamson
Quoted 16 lines · Alex Williamson
Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

To summarize my previous messages, my notes are
- not clear that we want to implement _STA: yes we can tell hypervisor what did _STA report to OSPM but this won't be needed without _STA
- assuming we do, it seems clear that we want hypervisor
to know what it is that we told OSPM about slot status
- the specific interface used for the above is fairly tricky so it needs documentation explaining how both sides cooperate

Signature · 1 line
MST
#7 lore raw
Quoted 24 lines · Michael S. Tsirkin
Quoted 16 lines · Alex Williamson
Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

Quoted 8 lines

To summarize my previous messages, my notes are
- not clear that we want to implement _STA: yes we can tell hypervisor what did _STA report to OSPM but this won't be needed without _STA
- assuming we do, it seems clear that we want hypervisor
to know what it is that we told OSPM about slot status
- the specific interface used for the above is fairly tricky so it needs documentation explaining how both sides cooperate

Why have this up, down things at all? What's wrong with how CPU hotplug works. It has only one HW register that returns a single bitmask that has 1 for available cpu and 0 for non available. AML can figure out what changed by having local copy of the old register's value to compare new value with.

--
Gleb.

#5 lore raw
In reply to Michael S. Tsirkin
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Alex Williamson

When a Status method is provided on a slot, the OSPM evaluates _STA in response to the device check notify on the slot. This allows some degree of a handshake between the platform and the OSPM that the hotplug has been acknowledged.

In order to implement _STA, we need to know which slots have devices. A slot with device returns 0x0F, a slot without a device returns Zero. We get this information from Qemu using the 0xae08 I/O port register. This was previously the read-side of the register written to commit a device eject and always returned 0 on read. It now returns a bitmap of present slots, so we know that reading 0 means we have and old Qemu and dynamically modify our SSDT to rename the _STA methods. This is necessary to allow backwards compatibility.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Interesting. Isn't the UP register sufficient for _STA?

No, UP only reports the current slot being added, so we'd only be able to report a "present" value for that slot and not static or previously added slots.

It's probably a bug in qemu - for example, if two slots are added quickly we just lose the notification about the first one, right?

The current design up UP/DOWN is that they're transient. A bit in UP is set to cause a hot-add notification, a bit in DOWN is set to cause a hot remove.

Quoted 2 lines

The fix might involve making e.g. the UP register write 1 to clear, but it needs to be cleared on Notify, not on _STA.

The trouble with that, for my purposes, is that any OSPM will trigger the Notify, that's just part of GPE processing. Only hotplug capable OSPMs will respond to the Notify with a _STA check.

Quoted 18 lines
Quoted 18 lines
Quoted 11 lines

Assuming we want to implement _STA - for which the only motivation seems the handshake hack below.

Quoted 2 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

This part looks a bit like a hack.
_STA is not intended as an acknowledgement - it's a query for state. ACPI spec 5.0 requires that _STA is called before _INI, but ACPI 1.0b doesn't. Did you try some 1.0 OSPMs (e.g. XP) to see what they do?

I did test with XP. Section 6.3 of ACPI spec 1.0b references the _STA method during hotplug. I also found this reference for Windows ACPI procedure for hotplug/unplug:

http://www.microsoft.com/china/whdc/system/pnppwr/hotadd/hotplugpci.mspx#EYH

Quoted 8 lines
Quoted 4 lines

I agree, _STA is not intended as an acknowledgment, but that doesn't mean we can't use it as one. The OSPM can call _STA at any point in time, however calling it after we've done a notify for device check is about the best indication we can get that the OSPM is processing it.

How about an actual access to the slot? The event that we send is just a change event. Guest accesses the slot to see whether any devices
are present. No ACPI or interface changes are needed to detect this.

That's a possibility.

Quoted 5 lines
Quoted 1 line

It doesn't hurt anything if _STA is called spuriously.

It makes its use as an acknowledgment unreliable:
_STA called does *not* mean OSPM saw your hotplug
event.

It seems to work well though. In practice, the OSPM doesn't go around randomly calling _STA on devices to poll if the status has changed. It does it in response to events, device check being one of those events. However, only an OSPM that intends to do something about a device check is going to call _STA in response to it. So the behavior I see is that a non-hotplug capable guest sits quietly and does nothing after the SCI is injected and GPE Notify is triggered, while hotplug capable guests respond by checking _STA.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

I also think I see how this can cause a race, see below.

Quoted 2 lines

Your description of the qemu patches made me think that all you really want is detect an OS without OSPM. If that is the case, I would suggest adding an _INI method at top level as a simpler and more robust procedure.

Quoted 4 lines
Quoted 2 lines

No, having OSPM is a prerequisite, but does not imply supporting hotplug.

It does not? What are the OSPMs that ignore hotplug?

Linux: modprobe -r acpiphp (or just don't load it automatically)

Quoted 4 lines

Hotplug has been defined since ACPI 1.0 so this seems strange. But it will me much easier to discuss whether a specific hack is efficient if you define the specific bug this handshake tries to work around.

Well, there's the one above, not all guests support PCI hotplug. If you hot add a device, then remember acpiphp isn't loaded, the device is held hostage, unusable by the guest, un-removable by qemu. The only solution is to reboot the guest. There are clearly some bugs in the hotplug code blindly overwriting UP/DOWN bits that break hotplugging multiple devices or hotplugging at such a rate that we stomp on ourselves. That can result in trying to add a device, immediately removing it, clearing UP, setting DOWN, confusing the guest and again ending up with a device held hostage. That's also fixed by these patches. I also thought that since this adds a virtual "end" to a hot add, and we already have a beginning and end to a hot remove, we could think about adding parameters to devie_add/del to wait for a user specified time and report success/failure. Failure to add can then immediately remove the device.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

Otherwise, how about implementing _PS0
(and probably _PS3) to manage slot power? Maybe this what you are really after, and it seems like a better interface than 'acknowledge' which does not seem to make sense for real hardware.

I tried this, _PS0/3 also requires _STA.

Interesting. Why does it require _STA?

Does the OSPM know to call _PS0/3 unless _STA reports the current state? The other serious problem that I remembered here is that Windows only powers up a slot if it has a driver. So if you hotplug a virio-net-pci device but don't have the drivers, Windows has ownership of the device, but doesn't power it up. _STA detects this, using _PS0 as an ack would not.

Quoted 4 lines
Quoted 2 lines

Implementing both caused
interrupts to stop working on Linux guests.

So there's some bug then? Let's fix?

Given the above, I didn't see any reason to pursue it.

Quoted 7 lines
Quoted 3 lines

Note that _PS0/3 is even
less closely associated with device removal in 1.0b than _STA even though the MSFT document references it.

ACPI spec references it :)
It seems clear that _PS0 must be called before device is used, otherwise the slot has no power.

See problem above.

Quoted 2 lines

There's also _OST - linux doesn't implement it but it seems modern windows versions do.

If we us _OST then we have a solution that only works on very, very new guests... that's not much of a solution.

Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 25 lines

---

 src/acpi-dsdt.dsl  |   36 ++-
 src/acpi-dsdt.hex  |  124 ++++++----
 src/acpi.c         |   27 ++
 src/ssdt-pcihp.dsl |    3 
 src/ssdt-pcihp.hex |  658 ++++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 686 insertions(+), 162 deletions(-)
src/acpi-dsdt.dsl +1 -5
index 7082b65..6b87086 100644
@@ -119,17 +119,15 @@ DefinitionBlock (
                prt_slot3(0x001f),
             })
 
-            OperationRegion(PCST, SystemIO, 0xae00, 0x08)
+            OperationRegion(PCST, SystemIO, 0xae00, 0x0c)
             Field (PCST, DWordAcc, NoLock, WriteAsZeros)
             {
-                PCIU, 32,
-                PCID, 32,
-            }
-

src/acpi-dsdt.dsl DefinitionBlock ( · line 130

Quoted 10 lines
src/acpi-dsdt.dsl +6 -4
-            OperationRegion(SEJ, SystemIO, 0xae08, 0x04)
-            Field (SEJ, DWordAcc, NoLock, WriteAsZeros)
-            {
-                B0EJ, 32,
+                // PCI Up/ACK
+                PUPA, 32,
+                // PCI Down
+                PDWN, 32,
+                // PCI Present/Eject
+                PPEJ, 32,

Note on the comment: this only affects bus0 not all of PCI.

Quoted 3 lines
Quoted 1 line

As has always been the case.

But your comment implies otherwise :)

Hmm, hard to imagine how a 32bit register can signal anything more than a single bus.

Quoted 39 lines
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
src/acpi-dsdt.dsl +0 -0
             }
 
             Name (_CRS, ResourceTemplate ()

src/acpi-dsdt.dsl DefinitionBlock ( · line 468

Quoted 10 lines
src/acpi-dsdt.dsl +3 -1
@@ -462,10 +460,20 @@ DefinitionBlock (
         /* Methods called by hotplug devices */
         Method (PCEJ, 1, NotSerialized) {
             // _EJ0 method - eject callback
-            Store(ShiftLeft(1, Arg0), B0EJ)
+            Store(ShiftLeft(1, Arg0), PPEJ)
             Return (0x0)
         }
 
+        Method (PSTA, 1, NotSerialized) {
+            Store(ShiftLeft(1, Arg0), PUPA)

So this looks wrong to me.

Quoted 5 lines

Specifically _STA is also called at the end after _EJ0. If the device is ejected then insterted, you get a window where _STA is called and hardware will think insertion was acknowledged, while in fact ejection was acknowledged.

The qemu patch doesn't allow an insertion while an eject is pending.

Quoted 8 lines
Quoted 2 lines

I also think a request for the OS to rescan the bus will trigger _STA calls. Same race can get triggered.

Spurious _STA calls don't matter, they'll clear a bit that wasn't set in the UP register anyway. If there's a race with the hotplug SCI, ie. we've set UP, but OSPM performs a rescan, they'll noticed _STA now reports the device is present and I think that should lead to the proper result.

To be more explicit:

_EJ0

Quoted 7 lines

host adds new device

_STA triggered to check _EJ0 success

You now think OSPM acknowledged new device with _STA but it didn't. OSPM thinks that _EJ0 failed but it didn't.

Hmm, I wonder if we should clear DOWN from the qemu side of _STA too instead of from _EJ0.

Quoted 52 lines
Quoted 52 lines
Quoted 26 lines
Quoted 26 lines
src/acpi-dsdt.dsl +13 -1
+            Store(PPEJ, Local0)
+            If (And(Local0, ShiftLeft(1, Arg0))) {
+                Return(0x0F)
+            } Else {
+                Return(Zero)
+            }
+        }
+
 	/* Hotplug notification method supplied by SSDT */
 	External (\_SB.PCI0.PCNT, MethodObj)
 
@@ -473,12 +481,16 @@ DefinitionBlock (
         Method(PCNF, 0) {
             // Local0 = iterator
             Store (Zero, Local0)
+            // Local1 = slots marked "up"
+            Store (PUPA, Local1)
+            // Local2 = slots marked "down"
+            Store (PDWN, Local2)
             While (LLess(Local0, 31)) {
                 Increment(Local0)
-                If (And(PCIU, ShiftLeft(1, Local0))) {
+                If (And(Local1, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 1)
Quoted 8 lines

src/acpi-dsdt.dsl DefinitionBlock ( · line 496

Quoted 6 lines
src/acpi-dsdt.dsl +1 -1
                 }
-                If (And(PCID, ShiftLeft(1, Local0))) {
+                If (And(Local2, ShiftLeft(1, Local0))) {
                     PCNT(Local0, 3)
                 }
             }

Nothing wrong here but should be a separate patch?

It was pretty trivial, but I can split it if needed.

Quoted 15 lines
Quoted 12 lines
src/acpi-dsdt.hex +1 -1
index 5dc7bb4..6d99f53 100644
@@ -3,12 +3,12 @@ static unsigned char AmlCode[] = {
 0x53,
 0x44,
 0x54,
-0xd3,
+0xeb,
 0x10,
 0x0,

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I'd rather not see this part on list.

Then it should be .gitignore'd.

No, it's in git so people without iasl can build the bios.

Then should we also commit binaries to git so people without gcc can "build" the bios?

Quoted 4 lines
Quoted 2 lines

I'm just following the lead of previous
patches in this space.

People tend to forget these blobs (I do sometimes) but it makes review awkward.

Noted.

[adding in follow-up]
...

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

The _STA method also writes the slot identifier to I/O port register 0xae00 as an acknowledgment of the hotplug request.

To summarize my previous messages, my notes are
- not clear that we want to implement _STA: yes we can tell hypervisor what did _STA report to OSPM but this won't be needed without _STA

/me tries to find the beginning of the circular logic.

Quoted 2 lines

- assuming we do, it seems clear that we want hypervisor to know what it is that we told OSPM about slot status

_STA always reports which slots have devices at the time it's called.

Quoted 2 lines

- the specific interface used for the above is fairly tricky so it needs documentation explaining how both sides cooperate

The field written from _STA is documented as an "ACK" and it's pretty clear that a write to that field clears the pending UP. There's also a pretty good comment in the commit log. It seems better documented than most of our ACPI interface. I can add more documentation, but this doesn't seem like your primary concern.

Perhaps Gleb is right that we should redesign the whole thing, removing UP/DOWN. Maybe we can come up with better synchronization logic that way. I'm not sure if a change like that can be made with any kind of backwards compatibility like this has though. I think we'd still depend on _STA for some kind of coordination too, so in the end, I'm not sure it's any better. Thanks,

Alex