cxl: Add helper function that calculate performance data for downstream ports
authorDave Jiang <dave.jiang@intel.com>
Thu, 21 Dec 2023 22:03:58 +0000 (15:03 -0700)
committerDan Williams <dan.j.williams@intel.com>
Fri, 22 Dec 2023 23:31:52 +0000 (15:31 -0800)
The CDAT information from the switch, Switch Scoped Latency and Bandwidth
Information Structure (SSLBIS), is parsed and stored under a cxl_dport
based on the correlated downstream port id from the SSLBIS entry. Walk
the entire CXL port paths and collect all the performance data. Also
pick up the link latency number that's stored under the dports. The
entire path PCIe bandwidth can be retrieved using the
pcie_bandwidth_available() call.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Dave Jiang <dave.jiang@intel.com>
Link: https://lore.kernel.org/r/170319623824.2212653.10302079766473698427.stgit@djiang5-mobl3
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/cxl/core/port.c
drivers/cxl/cxl.h

index b5ad227..8c00fd6 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
+#include <linux/node.h>
 #include <cxlmem.h>
 #include <cxlpci.h>
 #include <cxl.h>
@@ -2094,6 +2095,80 @@ bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
 }
 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL);
 
+static void combine_coordinates(struct access_coordinate *c1,
+                               struct access_coordinate *c2)
+{
+               if (c2->write_bandwidth)
+                       c1->write_bandwidth = min(c1->write_bandwidth,
+                                                 c2->write_bandwidth);
+               c1->write_latency += c2->write_latency;
+
+               if (c2->read_bandwidth)
+                       c1->read_bandwidth = min(c1->read_bandwidth,
+                                                c2->read_bandwidth);
+               c1->read_latency += c2->read_latency;
+}
+
+/**
+ * cxl_endpoint_get_perf_coordinates - Retrieve performance numbers stored in dports
+ *                                of CXL path
+ * @port: endpoint cxl_port
+ * @coord: output performance data
+ *
+ * Return: errno on failure, 0 on success.
+ */
+int cxl_endpoint_get_perf_coordinates(struct cxl_port *port,
+                                     struct access_coordinate *coord)
+{
+       struct access_coordinate c = {
+               .read_bandwidth = UINT_MAX,
+               .write_bandwidth = UINT_MAX,
+       };
+       struct cxl_port *iter = port;
+       struct cxl_dport *dport;
+       struct pci_dev *pdev;
+       unsigned int bw;
+
+       if (!is_cxl_endpoint(port))
+               return -EINVAL;
+
+       dport = iter->parent_dport;
+
+       /*
+        * Exit the loop when the parent port of the current port is cxl root.
+        * The iterative loop starts at the endpoint and gathers the
+        * latency of the CXL link from the current iter to the next downstream
+        * port each iteration. If the parent is cxl root then there is
+        * nothing to gather.
+        */
+       while (iter && !is_cxl_root(to_cxl_port(iter->dev.parent))) {
+               combine_coordinates(&c, &dport->sw_coord);
+               c.write_latency += dport->link_latency;
+               c.read_latency += dport->link_latency;
+
+               iter = to_cxl_port(iter->dev.parent);
+               dport = iter->parent_dport;
+       }
+
+       /* Augment with the generic port (host bridge) perf data */
+       combine_coordinates(&c, &dport->hb_coord);
+
+       /* Get the calculated PCI paths bandwidth */
+       pdev = to_pci_dev(port->uport_dev->parent);
+       bw = pcie_bandwidth_available(pdev, NULL, NULL, NULL);
+       if (bw == 0)
+               return -ENXIO;
+       bw /= BITS_PER_BYTE;
+
+       c.write_bandwidth = min(c.write_bandwidth, bw);
+       c.read_bandwidth = min(c.read_bandwidth, bw);
+
+       *coord = c;
+
+       return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_endpoint_get_perf_coordinates, CXL);
+
 /* for user tooling to ensure port disable work has completed */
 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
 {
index dd234f3..492dbf6 100644 (file)
@@ -876,6 +876,9 @@ static inline struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
 void cxl_endpoint_parse_cdat(struct cxl_port *port);
 void cxl_switch_parse_cdat(struct cxl_port *port);
 
+int cxl_endpoint_get_perf_coordinates(struct cxl_port *port,
+                                     struct access_coordinate *coord);
+
 /*
  * Unit test builds overrides this to __weak, find the 'strong' version
  * of these symbols in tools/testing/cxl/.