[PATCH 2/3] PCI/ACPI: Add API for specifying aux power in D3cold

From: Mario Limonciello
Date: Fri Nov 10 2023 - 15:42:33 EST


The PCI firmware specification includes support for a _DSM function
to negotiate aux power to be provided to the device while it is in
D3cold.

"The Request D3cold Aux Power Limit function describes how a device
driver conveys its auxiliary power requirements to the host platform
when the device is in D3cold. The system firmware responds with a value
indicating whether the request can be supported. The power request is
specific to the Auxiliary power supply; main power may be removed while
in D3cold. A device must not draw any more power than what has been
negotiated via this mechanism after entering D3cold"

Add API for callers to utilize this _DSM.

Link: https://members.pcisig.com/wg/PCI-SIG/document/15350
PCI Firmware specification 3.3, section 4.6.10
Signed-off-by: Mario Limonciello <mario.limonciello@xxxxxxx>
---
drivers/pci/pci-acpi.c | 54 ++++++++++++++++++++++++++++++++++++++++
include/linux/pci-acpi.h | 7 ++++++
2 files changed, 61 insertions(+)

diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c
index bea72e807817..257858a6719e 100644
--- a/drivers/pci/pci-acpi.c
+++ b/drivers/pci/pci-acpi.c
@@ -1348,6 +1348,60 @@ static struct acpi_device *acpi_pci_find_companion(struct device *dev)
return adev;
}

+/**
+ * pci_acpi_request_aux_power_for_d3cold - Request auxiliary power for D3cold
+ * @pdev: PCI device to request power for
+ * @arg: mW requested
+ *
+ * This function requests auxiliary power for a PCI device in D3cold state
+ * when main power is removed above the value guaranteed by PCI specification.
+ *
+ * Return:
+ * 0 on success
+ * -ENODEV when the device does not support the _DSM
+ * -EIO on ACPI call failure
+ * -EACCESS when the request was denied
+ * Positive value corresponding to platform requested retry interval in seconds
+ */
+int pci_acpi_request_aux_power_for_d3cold(struct pci_dev *pdev, int arg)
+{
+ struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
+ union acpi_object *obj;
+ union acpi_object argv4 = {
+ .integer.type = ACPI_TYPE_INTEGER,
+ .integer.value = arg,
+ };
+ int val;
+
+ if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid,
+ pci_acpi_dsm_rev,
+ 1 << DSM_PCI_REQUEST_D3COLD_AUX_POWER))
+ return -ENODEV;
+
+ obj = acpi_evaluate_dsm_typed(adev->handle, &pci_acpi_dsm_guid,
+ pci_acpi_dsm_rev,
+ DSM_PCI_REQUEST_D3COLD_AUX_POWER,
+ &argv4, ACPI_TYPE_INTEGER);
+ if (!obj)
+ return -EIO;
+
+ val = obj->integer.value;
+ ACPI_FREE(obj);
+
+ switch (val) {
+ case PCI_D3COLD_AUX_DENIED:
+ return -EACCES;
+ case PCI_D3COLD_AUX_GRANTED:
+ case PCI_D3COLD_AUX_NO_MAIN_POWER_REMOVAL:
+ return 0;
+ default:
+ break;
+ }
+
+ return val;
+}
+EXPORT_SYMBOL_GPL(pci_acpi_request_aux_power_for_d3cold);
+
/**
* pci_acpi_optimize_delay - optimize PCI D3 and D3cold delay from ACPI
* @pdev: the PCI device whose delay is to be updated
diff --git a/include/linux/pci-acpi.h b/include/linux/pci-acpi.h
index 7966ef8f14b3..bc6372bcb7d6 100644
--- a/include/linux/pci-acpi.h
+++ b/include/linux/pci-acpi.h
@@ -122,6 +122,13 @@ extern const int pci_acpi_dsm_rev;
#define DSM_PCI_DEVICE_NAME 0x07
#define DSM_PCI_POWER_ON_RESET_DELAY 0x08
#define DSM_PCI_DEVICE_READINESS_DURATIONS 0x09
+#define DSM_PCI_REQUEST_D3COLD_AUX_POWER 0x0A
+
+#define PCI_D3COLD_AUX_DENIED 0
+#define PCI_D3COLD_AUX_GRANTED 1
+#define PCI_D3COLD_AUX_NO_MAIN_POWER_REMOVAL 2
+
+int pci_acpi_request_aux_power_for_d3cold(struct pci_dev *pdev, int arg);

#ifdef CONFIG_PCIE_EDR
void pci_acpi_add_edr_notifier(struct pci_dev *pdev);
--
2.34.1