]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
PCI: Add sysfs attribute for device power state
authorMaximilian Luz <luzmaximilian@gmail.com>
Mon, 20 Oct 2025 16:25:16 +0000 (12:25 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Oct 2025 13:01:24 +0000 (14:01 +0100)
[ Upstream commit 80a129afb75cba8434fc5071bd6919172442315c ]

While PCI power states D0-D3hot can be queried from user-space via lspci,
D3cold cannot.  lspci cannot provide an accurate value when the device is
in D3cold as it has to restore the device to D0 before it can access its
power state via the configuration space, leading to it reporting D0 or
another on-state. Thus lspci cannot be used to diagnose power consumption
issues for devices that can enter D3cold or to ensure that devices properly
enter D3cold at all.

Add a new sysfs device attribute for the PCI power state, showing the
current power state as seen by the kernel.

[bhelgaas: drop READ_ONCE(), see discussion at the link]
Link: https://lore.kernel.org/r/20201102141520.831630-1-luzmaximilian@gmail.com
Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Stable-dep-of: 48991e493507 ("PCI/sysfs: Ensure devices are powered for config reads")
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/ABI/testing/sysfs-bus-pci
drivers/pci/pci-sysfs.c

index da33ab66ddfe73399b75e3681093d5c7deb5097a..9d499a126e87f71e12724c2ea780049acbc9cf89 100644 (file)
@@ -377,3 +377,12 @@ Contact:   Heiner Kallweit <hkallweit1@gmail.com>
 Description:   If ASPM is supported for an endpoint, these files can be
                used to disable or enable the individual power management
                states. Write y/1/on to enable, n/0/off to disable.
+
+What:          /sys/bus/pci/devices/.../power_state
+Date:          November 2020
+Contact:       Linux PCI developers <linux-pci@vger.kernel.org>
+Description:
+               This file contains the current PCI power state of the device.
+               The value comes from the PCI kernel device state and can be one
+               of: "unknown", "error", "D0", D1", "D2", "D3hot", "D3cold".
+               The file is read only.
index d27bc5a5d2f8657938d8274acad210253c1ece14..5a9d942198586c4cfba7fbbde5200558feb98429 100644 (file)
@@ -124,6 +124,15 @@ static ssize_t cpulistaffinity_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(cpulistaffinity);
 
+static ssize_t power_state_show(struct device *dev,
+                               struct device_attribute *attr, char *buf)
+{
+       struct pci_dev *pdev = to_pci_dev(dev);
+
+       return sprintf(buf, "%s\n", pci_power_name(pdev->current_state));
+}
+static DEVICE_ATTR_RO(power_state);
+
 /* show resources */
 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
                             char *buf)
@@ -603,6 +612,7 @@ static ssize_t driver_override_show(struct device *dev,
 static DEVICE_ATTR_RW(driver_override);
 
 static struct attribute *pci_dev_attrs[] = {
+       &dev_attr_power_state.attr,
        &dev_attr_resource.attr,
        &dev_attr_vendor.attr,
        &dev_attr_device.attr,