diff mbox series

[v2] Export PBEC Data register into sysfs

Message ID 20240904030035.284423-1-kobayashi.da-06@fujitsu.com (mailing list archive)
State Superseded
Delegated to: Krzysztof Wilczyński
Headers show
Series [v2] Export PBEC Data register into sysfs | expand

Commit Message

Kobayashi,Daisuke Sept. 4, 2024, 3 a.m. UTC
This proposal aims to add a feature that outputs PCIe device power 
consumption information to sysfs.

Add support for PBEC (Power Budgeting Extended Capability) output 
to the PCIe driver. PBEC is defined in the 
PCIe specification(PCIe r6.0, sec 7.8.1) and is
a standard method for obtaining device power consumption information.

PCIe devices can significantly impact the overall power consumption of
a system. However, obtaining PCIe device power consumption information 
has traditionally been difficult. 

The PBEC Data register changes depending on the value of the PBEC Data 
Select register. To obtain all PBEC Data register values defined in the 
device, obtain the value of the PBEC Data register while changing the 
value of the PBEC Data Select register.

Signed-off-by: "Kobayashi,Daisuke" <kobayashi.da-06@fujitsu.com>
---
 drivers/pci/pci-sysfs.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Comments

Krzysztof Wilczyński Sept. 6, 2024, 6:59 a.m. UTC | #1
Hello,

> This proposal aims to add a feature that outputs PCIe device power 
> consumption information to sysfs.
> 
> Add support for PBEC (Power Budgeting Extended Capability) output 
> to the PCIe driver. PBEC is defined in the 
> PCIe specification(PCIe r6.0, sec 7.8.1) and is
> a standard method for obtaining device power consumption information.
> 
> PCIe devices can significantly impact the overall power consumption of
> a system. However, obtaining PCIe device power consumption information 
> has traditionally been difficult. 
> 
> The PBEC Data register changes depending on the value of the PBEC Data 
> Select register. To obtain all PBEC Data register values defined in the 
> device, obtain the value of the PBEC Data register while changing the 
> value of the PBEC Data Select register.

Kobayashi-san, we are missing the ABI documentation, which is all that's
left to get this series merged.  Perhaps even this cycle if you post a v3
somewhat soon ish. お願いします。

An example of a series which shows how to do it:

  - https://lore.kernel.org/linux-pci/166336088796.3597940.14973499936692558556.stgit@omen

The sysfs documentation (see section at the bottom):

  - https://www.kernel.org/doc/html/latest/filesystems/sysfs.html

	Krzysztof
Kobayashi,Daisuke Sept. 6, 2024, 8:30 a.m. UTC | #2
> Hello,
> 
> > This proposal aims to add a feature that outputs PCIe device power
> > consumption information to sysfs.
> >
> > Add support for PBEC (Power Budgeting Extended Capability) output to
> > the PCIe driver. PBEC is defined in the PCIe specification(PCIe r6.0,
> > sec 7.8.1) and is a standard method for obtaining device power
> > consumption information.
> >
> > PCIe devices can significantly impact the overall power consumption of
> > a system. However, obtaining PCIe device power consumption information
> > has traditionally been difficult.
> >
> > The PBEC Data register changes depending on the value of the PBEC Data
> > Select register. To obtain all PBEC Data register values defined in
> > the device, obtain the value of the PBEC Data register while changing
> > the value of the PBEC Data Select register.
> 
> Kobayashi-san, we are missing the ABI documentation, which is all that's left
> to get this series merged.  Perhaps even this cycle if you post a v3 somewhat
> soon ish. お願いします。
> 
> An example of a series which shows how to do it:
> 
>   -
> https://lore.kernel.org/linux-pci/166336088796.3597940.149734999366925585
> 56.stgit@omen
> 
> The sysfs documentation (see section at the bottom):
> 
>   - https://www.kernel.org/doc/html/latest/filesystems/sysfs.html
> 
> 	Krzysztof

I understand your point and thank you for providing the example.
I'll be able to post v3 early next week.
I appreciate it. Thank you.
diff mbox series

Patch

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 2321fdfefd7d..468c929a9347 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -182,6 +182,33 @@  static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(resource);
 
+static ssize_t power_budget_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct pci_dev *pci_dev = to_pci_dev(dev);
+	size_t len = 0;
+	int i, pos;
+	u32 data;
+
+	pos = pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_PWR);
+	if (!pos)
+		return -EINVAL;
+
+	for (i = 0; i < 0xff; i++) {
+		pci_write_config_byte(pci_dev, pos + PCI_PWR_DSR, (u8)i);
+		pci_read_config_dword(pci_dev, pos + PCI_PWR_DATA, &data);
+		if (!data) {
+			if (len == 0)
+				return -EINVAL;
+			break;
+		}
+		len += sysfs_emit_at(buf, len, "%04x\n", data);
+	}
+
+	return len;
+}
+static DEVICE_ATTR_RO(power_budget);
+
 static ssize_t max_link_speed_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
 {
@@ -629,6 +656,7 @@  static struct attribute *pcie_dev_attrs[] = {
 	&dev_attr_current_link_width.attr,
 	&dev_attr_max_link_width.attr,
 	&dev_attr_max_link_speed.attr,
+	&dev_attr_power_budget.attr,
 	NULL,
 };