Skip to contentSkip to conversation

PCI/ASPM: Add support for LTR _DSM

PATCH In review Bjorn Helgaas 1 participant 4 messages Oct 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover PCI/ASPM: Add support for LTR _DSM

From: Bjorn Helgaas <[email protected]>

This is a v6 of Puranjay's LTR _DSM support. It should be functionally equivalent, but I moved the bulk of the code from pci.c to aspm.c, since it's only used for ASPM. I also combined pci_ltr_encode() with the existing encode_l12_threshold(), since they were doing essentially the same thing.

I'll reply to the patches with a few more comments.

Bjorn Helgaas (1):
PCI/ASPM: Rename encode_l12_threshold(), convert arg to ns

Puranjay Mohan (1):
PCI/ASPM: Add support for LTR _DSM

 drivers/pci/pci-acpi.c   |  35 +++++++++++++
 drivers/pci/pci.h        |   4 ++
 drivers/pci/pcie/aspm.c  | 110 +++++++++++++++++++++++++++++----------
 drivers/pci/probe.c      |   4 ++
 include/linux/pci-acpi.h |   1 +
 include/linux/pci.h      |   2 +
 6 files changed, 129 insertions(+), 27 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v6 1/2 PCI/ASPM: Rename encode_l12_threshold(), convert arg to ns

From: Bjorn Helgaas <[email protected]>

Rename encode_l12_threshold() to pci_lat_encode() and convert its argument from microseconds to nanoseconds so we can share it between LTR_L1.2_THRESHOLD encoding and LTR Max Snoop/No-Snoop Latency encoding.

---
 drivers/pci/pcie/aspm.c | 39 ++++++++++++---------------------------
 1 file changed, 12 insertions(+), 27 deletions(-)
drivers/pci/pcie/aspm.c +12 -27
index 253c30cc1967..beb6e2e4e5d2 100644
@@ -307,6 +307,17 @@ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
 	pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
 }
 
+static void pci_lat_encode(u64 lat, u32 *scale, u32 *val)
+{
+	/* See PCIe r5.0, sec 7.8.3.3 and sec 6.18 */
+	     if (lat < 32)	 { *scale = 0; *val = (lat >>  0) & 0x3ff; }
+	else if (lat < 1024)	 { *scale = 1; *val = (lat >>  5) & 0x3ff; }
+	else if (lat < 32768)	 { *scale = 2; *val = (lat >> 10) & 0x3ff; }
+	else if (lat < 1048576)	 { *scale = 3; *val = (lat >> 15) & 0x3ff; }
+	else if (lat < 33554432) { *scale = 4; *val = (lat >> 20) & 0x3ff; }
+	else			 { *scale = 5; *val = (lat >> 25) & 0x3ff; }
+}
+
 /* Convert L0s latency encoding to ns */
 static u32 calc_l0s_latency(u32 encoding)
 {
@@ -354,32 +365,6 @@ static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
 	return 0;
 }
 
-static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
-{
-	u32 threshold_ns = threshold_us * 1000;
-
-	/* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
-	if (threshold_ns < 32) {
-		*scale = 0;
-		*value = threshold_ns;
-	} else if (threshold_ns < 1024) {
-		*scale = 1;
-		*value = threshold_ns >> 5;
-	} else if (threshold_ns < 32768) {
-		*scale = 2;
-		*value = threshold_ns >> 10;
-	} else if (threshold_ns < 1048576) {
-		*scale = 3;
-		*value = threshold_ns >> 15;
-	} else if (threshold_ns < 33554432) {
-		*scale = 4;
-		*value = threshold_ns >> 20;
-	} else {
-		*scale = 5;
-		*value = threshold_ns >> 25;
-	}
-}
-
 struct aspm_register_info {
 	u32 support:2;
 	u32 enabled:2;
@@ -539,7 +524,7 @@ static void aspm_calc_l1ss_info(struct pcie_link_state *link,
 	 * least 4us.
 	 */
 	l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
-	encode_l12_threshold(l1_2_threshold, &scale, &value);
+	pci_lat_encode(l1_2_threshold * 1000, &scale, &value);
 	link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
 }
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v6 2/2 PCI/ASPM: Add support for LTR _DSM

In reply to Bjorn Helgaas

From: Puranjay Mohan <[email protected]>

Latency Tolerance Reporting (LTR) is required for the ASPM L1.2 PM substate. Devices with Upstream Ports (Endpoints and Switches) may support the optional LTR Capability. When LTR is enabled, devices transmit LTR messages containing Snoop and No-Snoop Latencies upstream. The L1.2 substate may be entered if the most recent LTR values are greater than or equal to the LTR_L1.2_THRESHOLD from the L1 PM Substates Control 1 register.

Add a new function pci_ltr_init() which will be called from pci_init_capabilities() to initialize every PCIe device's LTR values. Add code in probe.c to evaluate LTR _DSM and save the latencies in pci_dev.

---
 drivers/pci/pci-acpi.c   | 35 ++++++++++++++++++++
 drivers/pci/pci.h        |  4 +++
 drivers/pci/pcie/aspm.c  | 71 ++++++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c      |  4 +++
 include/linux/pci-acpi.h |  1 +
 include/linux/pci.h      |  2 ++
 6 files changed, 117 insertions(+)
drivers/pci/pci-acpi.c +35 -0
index d5869a03f748..dc1623de3475 100644
@@ -1213,6 +1213,41 @@ static void pci_acpi_optimize_delay(struct pci_dev *pdev,
 	ACPI_FREE(obj);
 }
 
+/* pci_acpi_evaluate_ltr_latency
+ *
+ * @dev - pci_dev for which to evaluate Latency Tolerance Reporting _DSM
+ */
+void pci_acpi_evaluate_ltr_latency(struct pci_dev *dev)
+{
+#ifdef CONFIG_PCIEASPM
+	struct acpi_device *adev;
+	union acpi_object *obj, *elements;
+
+	adev = ACPI_COMPANION(&dev->dev);
+	if (!adev && !pci_dev_is_added(dev))
+		adev = acpi_pci_find_companion(&dev->dev);
+	if (!adev)
+		return;
+
+	if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid, 2,
+			    1ULL << DSM_PCI_LTR_MAX_LATENCY))
+		return;
+	obj = acpi_evaluate_dsm(adev->handle, &pci_acpi_dsm_guid, 2,
+				DSM_PCI_LTR_MAX_LATENCY, NULL);
+	if (!obj)
+		return;
+
+	if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 4) {
+		elements = obj->package.elements;
+		dev->max_snoop_latency = (u16) (elements[1].integer.value |
+			(elements[0].integer.value << PCI_LTR_SCALE_SHIFT));
+		dev->max_nosnoop_latency = (u16) (elements[3].integer.value |
+			(elements[2].integer.value << PCI_LTR_SCALE_SHIFT));
+	}
+	ACPI_FREE(obj);
+#endif
+}
+
 static void pci_acpi_set_external_facing(struct pci_dev *dev)
 {
 	u8 val;
drivers/pci/pci.h +4 -0
index fa12f7cbc1a0..cc1cc6f09530 100644
@@ -561,11 +561,13 @@ pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
 
 bool pcie_wait_for_link(struct pci_dev *pdev, bool active);
 #ifdef CONFIG_PCIEASPM
+void pci_ltr_init(struct pci_dev *dev);
 void pcie_aspm_init_link_state(struct pci_dev *pdev);
 void pcie_aspm_exit_link_state(struct pci_dev *pdev);
 void pcie_aspm_pm_state_change(struct pci_dev *pdev);
 void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
 #else
+static inline void pci_ltr_init(struct pci_dev *pdev) { }
 static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
 static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
 static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev) { }
@@ -680,11 +682,13 @@ static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL
 
 #ifdef CONFIG_ACPI
 int pci_acpi_program_hp_params(struct pci_dev *dev);
+void pci_acpi_evaluate_ltr_latency(struct pci_dev *dev);
 #else
 static inline int pci_acpi_program_hp_params(struct pci_dev *dev)
 {
 	return -ENODEV;
 }
+static inline void pci_acpi_evaluate_ltr_latency(struct pci_dev *dev) { }
 #endif
 
 #ifdef CONFIG_PCIEASPM
drivers/pci/pcie/aspm.c +71 -0

77 lines not on this page · raw message

drivers/pci/probe.c +4 -0
index 03d37128a24f..25ed03cd4757 100644
@@ -2106,6 +2106,9 @@ static void pci_configure_ltr(struct pci_dev *dev)
 	if (!pci_is_pcie(dev))
 		return;
 
+	/* Read latency values (if any) from platform */
+	pci_acpi_evaluate_ltr_latency(dev);
+
 	pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
 	if (!(cap & PCI_EXP_DEVCAP2_LTR))
 		return;
@@ -2400,6 +2403,7 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	pci_ptm_init(dev);		/* Precision Time Measurement */
 	pci_aer_init(dev);		/* Advanced Error Reporting */
 	pci_dpc_init(dev);		/* Downstream Port Containment */
+	pci_ltr_init(dev);		/* Latency Tolerance Reporting */
 
 	pcie_report_downtraining(dev);
 
include/linux/pci-acpi.h +1 -0
index 5ba475ca9078..e23236a4ff66 100644
@@ -110,6 +110,7 @@ extern const guid_t pci_acpi_dsm_guid;
 
 /* _DSM Definitions for PCI */
 #define DSM_PCI_PRESERVE_BOOT_CONFIG		0x05
+#define DSM_PCI_LTR_MAX_LATENCY			0x06
 #define DSM_PCI_DEVICE_NAME			0x07
 #define DSM_PCI_POWER_ON_RESET_DELAY		0x08
 #define DSM_PCI_DEVICE_READINESS_DURATIONS	0x09
include/linux/pci.h +2 -0
index 835530605c0d..af7558e3c331 100644
@@ -380,6 +380,8 @@ struct pci_dev {
 	struct pcie_link_state	*link_state;	/* ASPM link state */
 	unsigned int	ltr_path:1;	/* Latency Tolerance Reporting
 					   supported from root to here */
+	u16 max_snoop_latency;		/* LTR Max Snoop latency */
+	u16 max_nosnoop_latency;	/* LTR Max No-Snoop latency */
 #endif
 	unsigned int	eetlp_prefix_path:1;	/* End-to-End TLP Prefix */
 
Signature · 1 line
2.25.1
#3 lore raw
Quoted 27 lines · Bjorn Helgaas

From: Puranjay Mohan <[email protected]>

Latency Tolerance Reporting (LTR) is required for the ASPM L1.2 PM substate. Devices with Upstream Ports (Endpoints and Switches) may support the optional LTR Capability. When LTR is enabled, devices transmit LTR messages containing Snoop and No-Snoop Latencies upstream. The L1.2 substate may be entered if the most recent LTR values are greater than or equal to the LTR_L1.2_THRESHOLD from the L1 PM Substates Control 1 register.

Add a new function pci_ltr_init() which will be called from pci_init_capabilities() to initialize every PCIe device's LTR values. Add code in probe.c to evaluate LTR _DSM and save the latencies in pci_dev.

drivers/pci/probe.c +3 -0
@@ -2106,6 +2106,9 @@ static void pci_configure_ltr(struct pci_dev *dev)
 	if (!pci_is_pcie(dev))
 		return;
 
+	/* Read latency values (if any) from platform */
+	pci_acpi_evaluate_ltr_latency(dev);
+
 	pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
 	if (!(cap & PCI_EXP_DEVCAP2_LTR))
 		return;

drivers/pci/probe.c static void pci_init_capabilities(struct pci_dev *dev) · line 2406

Quoted 4 lines
drivers/pci/probe.c +1 -0
@@ -2400,6 +2403,7 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	pci_ptm_init(dev);		/* Precision Time Measurement */
 	pci_aer_init(dev);		/* Advanced Error Reporting */
 	pci_dpc_init(dev);		/* Downstream Port Containment */
+	pci_ltr_init(dev);		/* Latency Tolerance Reporting */

I don't think we're doing this in quite the right order. If I understand correctly, we have this:

pci_device_add
pci_configure_device
pci_configure_ltr
pci_acpi_evaluate_ltr_latency
acpi_evaluate_dsm(DSM_PCI_LTR_MAX_LATENCY)
pcie_capability_set_word(PCI_EXP_DEVCTL2_LTR_EN) <-- enable LTR
pci_init_capabilities
pci_ltr_init
pci_write_config_word(PCI_LTR_MAX_SNOOP_LAT) <-- set LTR msg content

So we enable LTR messages before we set the latency values in the LTR capability. I think we need to set the values *first*.

Quoted 1 line
drivers/pci/probe.c +0 -0
 	pcie_report_downtraining(dev);