diff mbox series

[V4,03/12] PCI/TPH: Add pcie_tph_modes() to query TPH modes

Message ID 20240822204120.3634-4-wei.huang2@amd.com (mailing list archive)
State New
Headers show
Series PCIe TPH and cache direct injection support | expand

Commit Message

Wei Huang Aug. 22, 2024, 8:41 p.m. UTC
Add pcie_tph_modes() to allow drivers to query the TPH modes supported
by an endpoint device, as reported in the TPH Requester Capability
register. The modes are reported as a bitmask and current supported
modes include:

 - PCI_TPH_CAP_NO_ST: NO ST Mode Supported
 - PCI_TPH_CAP_INT_VEC: Interrupt Vector Mode Supported
 - PCI_TPH_CAP_DEV_SPEC: Device Specific Mode Supported

Co-developed-by: Eric Van Tassell <Eric.VanTassell@amd.com>
Signed-off-by: Eric Van Tassell <Eric.VanTassell@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Reviewed-by: Ajit Khaparde <ajit.khaparde@broadcom.com>
Reviewed-by: Somnath Kotur <somnath.kotur@broadcom.com>
Reviewed-by: Andy Gospodarek <andrew.gospodarek@broadcom.com>
---
 drivers/pci/pcie/tph.c  | 33 +++++++++++++++++++++++++++++++++
 include/linux/pci-tph.h | 18 ++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 include/linux/pci-tph.h

Comments

Bjorn Helgaas Sept. 4, 2024, 7:40 p.m. UTC | #1
On Thu, Aug 22, 2024 at 03:41:11PM -0500, Wei Huang wrote:
> Add pcie_tph_modes() to allow drivers to query the TPH modes supported
> by an endpoint device, as reported in the TPH Requester Capability
> register. The modes are reported as a bitmask and current supported
> modes include:
> 
>  - PCI_TPH_CAP_NO_ST: NO ST Mode Supported
>  - PCI_TPH_CAP_INT_VEC: Interrupt Vector Mode Supported
>  - PCI_TPH_CAP_DEV_SPEC: Device Specific Mode Supported

> + * pcie_tph_modes - Get the ST modes supported by device
> + * @pdev: PCI device
> + *
> + * Returns a bitmask with all TPH modes supported by a device as shown in the
> + * TPH capability register. Current supported modes include:
> + *   PCI_TPH_CAP_NO_ST - NO ST Mode Supported
> + *   PCI_TPH_CAP_INT_VEC - Interrupt Vector Mode Supported
> + *   PCI_TPH_CAP_DEV_SPEC - Device Specific Mode Supported
> + *
> + * Return: 0 when TPH is not supported, otherwise bitmask of supported modes
> + */
> +int pcie_tph_modes(struct pci_dev *pdev)
> +{
> +	if (!pdev->tph_cap)
> +		return 0;
> +
> +	return get_st_modes(pdev);
> +}
> +EXPORT_SYMBOL(pcie_tph_modes);

I'm not sure I see the need for pcie_tph_modes().  The new bnxt code
looks like this:

  bnxt_request_irq
    if (pcie_tph_modes(bp->pdev) & PCI_TPH_CAP_INT_VEC)
      rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);

What is the advantage of this over just this?

  bnxt_request_irq
    rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);

It seems like drivers could just ask for what they want since
pcie_enable_tph() has to verify support for it anyway.  If that fails,
the driver can fall back to another mode.

Returning a bitmask of supported modes might be useful if the driver
could combine them, but IIUC the modes are all mutually exclusive, so
the driver can't request a combination of them.

Bjorn
Wei Huang Sept. 5, 2024, 2:46 p.m. UTC | #2
On 9/4/24 14:40, Bjorn Helgaas wrote:
> On Thu, Aug 22, 2024 at 03:41:11PM -0500, Wei Huang wrote:
>> Add pcie_tph_modes() to allow drivers to query the TPH modes supported
>> by an endpoint device, as reported in the TPH Requester Capability
>> register. The modes are reported as a bitmask and current supported
>> modes include:
>>
>>  - PCI_TPH_CAP_NO_ST: NO ST Mode Supported
>>  - PCI_TPH_CAP_INT_VEC: Interrupt Vector Mode Supported
>>  - PCI_TPH_CAP_DEV_SPEC: Device Specific Mode Supported
> 
>> + * pcie_tph_modes - Get the ST modes supported by device
>> + * @pdev: PCI device
>> + *
>> + * Returns a bitmask with all TPH modes supported by a device as shown in the
>> + * TPH capability register. Current supported modes include:
>> + *   PCI_TPH_CAP_NO_ST - NO ST Mode Supported
>> + *   PCI_TPH_CAP_INT_VEC - Interrupt Vector Mode Supported
>> + *   PCI_TPH_CAP_DEV_SPEC - Device Specific Mode Supported
>> + *
>> + * Return: 0 when TPH is not supported, otherwise bitmask of supported modes
>> + */
>> +int pcie_tph_modes(struct pci_dev *pdev)
>> +{
>> +	if (!pdev->tph_cap)
>> +		return 0;
>> +
>> +	return get_st_modes(pdev);
>> +}
>> +EXPORT_SYMBOL(pcie_tph_modes);
> 
> I'm not sure I see the need for pcie_tph_modes().  The new bnxt code
> looks like this:
> 
>   bnxt_request_irq
>     if (pcie_tph_modes(bp->pdev) & PCI_TPH_CAP_INT_VEC)
>       rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);
> 
> What is the advantage of this over just this?
> 
>   bnxt_request_irq
>     rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);
> 
> It seems like drivers could just ask for what they want since
> pcie_enable_tph() has to verify support for it anyway.  If that fails,
> the driver can fall back to another mode.

I can get rid of pcie_tph_modes() if unnecessary.

The design logic was that a driver can be used on various devices from
the same company. Some of these devices might not be TPH capable. So
instead of using trial-and-error (i.e. try INT_VEC ==> DEV_SPEC ==> give
up), we provide a way for the driver to query the device TPH
capabilities and pick a mode explicitly. IMO the code will be a bit cleaner.

> 
> Returning a bitmask of supported modes might be useful if the driver
> could combine them, but IIUC the modes are all mutually exclusive, so
> the driver can't request a combination of them.

In the real world cases I saw, this is true. In the spec I didn't find
that these bits are mutually exclusive though.

> 
> Bjorn
Bjorn Helgaas Sept. 5, 2024, 3:12 p.m. UTC | #3
On Thu, Sep 05, 2024 at 09:46:44AM -0500, Wei Huang wrote:
> On 9/4/24 14:40, Bjorn Helgaas wrote:
> > On Thu, Aug 22, 2024 at 03:41:11PM -0500, Wei Huang wrote:
> >> Add pcie_tph_modes() to allow drivers to query the TPH modes supported
> >> by an endpoint device, as reported in the TPH Requester Capability
> >> register. The modes are reported as a bitmask and current supported
> >> modes include:
> >>
> >>  - PCI_TPH_CAP_NO_ST: NO ST Mode Supported
> >>  - PCI_TPH_CAP_INT_VEC: Interrupt Vector Mode Supported
> >>  - PCI_TPH_CAP_DEV_SPEC: Device Specific Mode Supported
> > 
> >> + * pcie_tph_modes - Get the ST modes supported by device
> >> + * @pdev: PCI device
> >> + *
> >> + * Returns a bitmask with all TPH modes supported by a device as shown in the
> >> + * TPH capability register. Current supported modes include:
> >> + *   PCI_TPH_CAP_NO_ST - NO ST Mode Supported
> >> + *   PCI_TPH_CAP_INT_VEC - Interrupt Vector Mode Supported
> >> + *   PCI_TPH_CAP_DEV_SPEC - Device Specific Mode Supported
> >> + *
> >> + * Return: 0 when TPH is not supported, otherwise bitmask of supported modes
> >> + */
> >> +int pcie_tph_modes(struct pci_dev *pdev)
> >> +{
> >> +	if (!pdev->tph_cap)
> >> +		return 0;
> >> +
> >> +	return get_st_modes(pdev);
> >> +}
> >> +EXPORT_SYMBOL(pcie_tph_modes);
> > 
> > I'm not sure I see the need for pcie_tph_modes().  The new bnxt code
> > looks like this:
> > 
> >   bnxt_request_irq
> >     if (pcie_tph_modes(bp->pdev) & PCI_TPH_CAP_INT_VEC)
> >       rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);
> > 
> > What is the advantage of this over just this?
> > 
> >   bnxt_request_irq
> >     rc = pcie_enable_tph(bp->pdev, PCI_TPH_CAP_INT_VEC);
> > 
> > It seems like drivers could just ask for what they want since
> > pcie_enable_tph() has to verify support for it anyway.  If that fails,
> > the driver can fall back to another mode.
> 
> I can get rid of pcie_tph_modes() if unnecessary.
> 
> The design logic was that a driver can be used on various devices from
> the same company. Some of these devices might not be TPH capable. So
> instead of using trial-and-error (i.e. try INT_VEC ==> DEV_SPEC ==> give
> up), we provide a way for the driver to query the device TPH
> capabilities and pick a mode explicitly. IMO the code will be a bit cleaner.
>
> > Returning a bitmask of supported modes might be useful if the driver
> > could combine them, but IIUC the modes are all mutually exclusive, so
> > the driver can't request a combination of them.
> 
> In the real world cases I saw, this is true. In the spec I didn't find
> that these bits are mutually exclusive though.

A device may advertise *support* for multiple modes in TPH Requester
Capability, of course.  

What I meant by "driver can't request a combination" is that we can
only *select* one of them via the ST Mode select in TPH Requester
Control.
diff mbox series

Patch

diff --git a/drivers/pci/pcie/tph.c b/drivers/pci/pcie/tph.c
index a547858c3f68..a28dced3097d 100644
--- a/drivers/pci/pcie/tph.c
+++ b/drivers/pci/pcie/tph.c
@@ -6,9 +6,42 @@ 
  *     Eric Van Tassell <Eric.VanTassell@amd.com>
  *     Wei Huang <wei.huang2@amd.com>
  */
+#include <linux/pci.h>
+#include <linux/pci-tph.h>
 
 #include "../pci.h"
 
+static u8 get_st_modes(struct pci_dev *pdev)
+{
+	u32 reg;
+
+	pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, &reg);
+	reg &= PCI_TPH_CAP_NO_ST | PCI_TPH_CAP_INT_VEC | PCI_TPH_CAP_DEV_SPEC;
+
+	return reg;
+}
+
+/**
+ * pcie_tph_modes - Get the ST modes supported by device
+ * @pdev: PCI device
+ *
+ * Returns a bitmask with all TPH modes supported by a device as shown in the
+ * TPH capability register. Current supported modes include:
+ *   PCI_TPH_CAP_NO_ST - NO ST Mode Supported
+ *   PCI_TPH_CAP_INT_VEC - Interrupt Vector Mode Supported
+ *   PCI_TPH_CAP_DEV_SPEC - Device Specific Mode Supported
+ *
+ * Return: 0 when TPH is not supported, otherwise bitmask of supported modes
+ */
+int pcie_tph_modes(struct pci_dev *pdev)
+{
+	if (!pdev->tph_cap)
+		return 0;
+
+	return get_st_modes(pdev);
+}
+EXPORT_SYMBOL(pcie_tph_modes);
+
 void pci_tph_init(struct pci_dev *pdev)
 {
 	pdev->tph_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_TPH);
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
new file mode 100644
index 000000000000..fa378afe9c7e
--- /dev/null
+++ b/include/linux/pci-tph.h
@@ -0,0 +1,18 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * TPH (TLP Processing Hints)
+ *
+ * Copyright (C) 2024 Advanced Micro Devices, Inc.
+ *     Eric Van Tassell <Eric.VanTassell@amd.com>
+ *     Wei Huang <wei.huang2@amd.com>
+ */
+#ifndef LINUX_PCI_TPH_H
+#define LINUX_PCI_TPH_H
+
+#ifdef CONFIG_PCIE_TPH
+int pcie_tph_modes(struct pci_dev *pdev);
+#else
+static inline int pcie_tph_modes(struct pci_dev *pdev) { return 0; }
+#endif
+
+#endif /* LINUX_PCI_TPH_H */