On Wed, 19 Apr 2023 13:22:01 -0700 Dave Jiang <dave.jiang@xxxxxxxxx> wrote: > The latency is calculated by dividing the flit size over the bandwidth. Add > support to retrieve the flit size for the CXL device and calculate the > latency of the downstream link. > > Signed-off-by: Dave Jiang <dave.jiang@xxxxxxxxx> Totally trivial stuff about using defines that exist for the various multipliers. Otherwise looks good Reviewed-by: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx> > > --- > v2: > - Fix commit log issues. (Jonathan) > - Fix var declaration issues. (Jonathan) > --- > drivers/cxl/core/pci.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ > drivers/cxl/cxlpci.h | 15 +++++++++++ > drivers/cxl/pci.c | 13 --------- > 3 files changed, 83 insertions(+), 13 deletions(-) > > diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c > index 1c415b26e866..bb58296b3e56 100644 > --- a/drivers/cxl/core/pci.c > +++ b/drivers/cxl/core/pci.c > +static int cxl_pci_mbits_to_mbytes(struct pci_dev *pdev) > +{ > + int mbits; > + > + mbits = pci_bus_speed_to_mbps(pdev->bus->cur_bus_speed); > + if (mbits < 0) > + return mbits; > + > + return mbits >> 3; mbits / BITS_PER_BYTE; from linux/bits.h maybe. > +} > +/** > + * cxl_pci_get_latency - calculate the link latency for the PCIe link > + * @pdev - PCI device > + * > + * return: calculated latency or -errno > + * > + * CXL Memory Device SW Guide v1.0 2.11.4 Link latency calculation > + * Link latency = LinkPropagationLatency + FlitLatency + RetimerLatency > + * LinkProgationLatency is negligible, so 0 will be used > + * RetimerLatency is assumed to be negligible and 0 will be used > + * FlitLatency = FlitSize / LinkBandwidth > + * FlitSize is defined by spec. CXL rev3.0 4.2.1. > + * 68B flit is used up to 32GT/s. >32GT/s, 256B flit size is used. > + * The FlitLatency is converted to picoseconds. > + */ > +long cxl_pci_get_latency(struct pci_dev *pdev) > +{ > + long bw; > + > + bw = cxl_pci_mbits_to_mbytes(pdev); > + if (bw < 0) > + return bw; > + > + return cxl_flit_size(pdev) * 1000000L / bw; MEGA from include/linux/units.h perhaps though it's an oddity because output of this is pico seconds, so maybe needs to be PICO / MEGA to act as documentation of why. > +} > +EXPORT_SYMBOL_NS_GPL(cxl_pci_get_latency, CXL);