* [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:29 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
` (11 subsequent siblings)
12 siblings, 1 reply; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
pcie_tph_get_st_table_loc() incorrectly uses FIELD_GET(), which shifts the
field value to bit 0. But the function is designed to return raw
PCI_TPH_LOC_* values as defined in the function comment.
This causes incorrect ST table location detection. Fix it by using bitwise
AND with PCI_TPH_CAP_LOC_MASK to return the unshifted field value matching
the function specification.
This doesn't make a difference to mlx5_st_create(), the lone external
caller, because it only checks for PCI_TPH_LOC_NONE (0), but will be needed
for callers that check for PCI_TPH_LOC_CAP or PCI_TPH_LOC_MSIX.
Also add tph_cap validation for pcie_tph_get_st_table_loc() to prevent
invalid PCI configuration space access when TPH is not supported. Add stub
functions for pcie_tph_get_st_table_size() and pcie_tph_get_st_table_loc()
when !CONFIG_PCIE_TPH.
Fixes: d2e8a34876ce ("PCI/TPH: Add Steering Tag support")
Cc: stable@vger.kernel.org
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Reviewed-by: Alex Williamson <alex.williamson@nvidia.com>
Reviewed-by: Bjorn Helgaas <bhelgaas@google.com>
---
drivers/pci/tph.c | 12 +++++-------
include/linux/pci-tph.h | 4 ++++
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 4d064b86befc..c4ceb08204d6 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -167,11 +167,14 @@ static u8 get_st_modes(struct pci_dev *pdev)
*/
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
{
- u32 reg;
+ u32 reg = 0;
+
+ if (!pdev->tph_cap)
+ return PCI_TPH_LOC_NONE;
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
- return FIELD_GET(PCI_TPH_CAP_LOC_MASK, reg);
+ return reg & PCI_TPH_CAP_LOC_MASK;
}
EXPORT_SYMBOL(pcie_tph_get_st_table_loc);
@@ -186,9 +189,6 @@ u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
/* Check ST table location first */
loc = pcie_tph_get_st_table_loc(pdev);
-
- /* Convert loc to match with PCI_TPH_LOC_* defined in pci_regs.h */
- loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
if (loc != PCI_TPH_LOC_CAP)
return 0;
@@ -317,8 +317,6 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
loc = pcie_tph_get_st_table_loc(pdev);
- /* Convert loc to match with PCI_TPH_LOC_* */
- loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
switch (loc) {
case PCI_TPH_LOC_MSIX:
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index be68cd17f2f8..159ffb82b34e 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -41,6 +41,10 @@ static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
static inline void pcie_disable_tph(struct pci_dev *pdev) { }
static inline int pcie_enable_tph(struct pci_dev *pdev, int mode)
{ return -EINVAL; }
+static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
+{ return 0; }
+static inline u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
+{ return PCI_TPH_LOC_NONE; }
#endif
#endif /* LINUX_PCI_TPH_H */
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* Re: [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
2026-11-18 9:12 ` [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
@ 2026-11-18 9:29 ` sashiko-bot
0 siblings, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-11-18 9:29 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] PCI/TPH: undeclared PCI_TPH_LOC_NONE in pci-tph.h when
!CONFIG_PCIE_TPH
- [Low] PCI/TPH: inaccurate Fixes trailer in commit message
--
commit 47bc8345d31e14953151f380f2af4f107d3d65ae
Author: Chengwen Feng <fengchengwen@huawei.com>
> PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
>
> pcie_tph_get_st_table_loc() incorrectly uses FIELD_GET(), which shifts the
> field value to bit 0. But the function is designed to return raw
> PCI_TPH_LOC_* values as defined in the function comment.
>
> This causes incorrect ST table location detection. Fix it by using bitwise
> AND with PCI_TPH_CAP_LOC_MASK to return the unshifted field value matching
> the function specification.
>
> This doesn't make a difference to mlx5_st_create(), the lone external
> caller, because it only checks for PCI_TPH_LOC_NONE (0), but will be needed
> for callers that check for PCI_TPH_LOC_CAP or PCI_TPH_LOC_MSIX.
>
> Also add tph_cap validation for pcie_tph_get_st_table_loc() to prevent
> invalid PCI configuration space access when TPH is not supported. Add stub
> functions for pcie_tph_get_st_table_size() and pcie_tph_get_st_table_loc()
> when !CONFIG_PCIE_TPH.
>
> Fixes: d2e8a34876ce ("PCI/TPH: Add Steering Tag support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> Reviewed-by: Alex Williamson <alex.williamson@nvidia.com>
> Reviewed-by: Bjorn Helgaas <bhelgaas@google.com>
[Severity: Low]
In d2e8a34876ce ("PCI/TPH: Add Steering Tag support"), get_st_table_loc()
and get_st_table_size() were static helpers whose internal callers already
validated pdev->tph_cap and shifted the return value with FIELD_PREP().
Should the Fixes: tags point to the commits that exported these functions
without stubs or pdev->tph_cap checks instead:
Fixes: 0a61ec9cc51b ("PCI/TPH: Expose pcie_tph_get_st_table_size()")
Fixes: 7b8a8ec20cfc ("PCI/TPH: Expose pcie_tph_get_st_table_loc()")
[ ... ]
> diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
> index be68cd17f2f83..159ffb82b34ec 100644
> --- a/include/linux/pci-tph.h
> +++ b/include/linux/pci-tph.h
> @@ -41,6 +41,10 @@ static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
> static inline void pcie_disable_tph(struct pci_dev *pdev) { }
> static inline int pcie_enable_tph(struct pci_dev *pdev, int mode)
> { return -EINVAL; }
> +static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
> +{ return 0; }
> +static inline u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
> +{ return PCI_TPH_LOC_NONE; }
> #endif
>
> #endif /* LINUX_PCI_TPH_H */
[Severity: High]
Does this break the build when CONFIG_PCIE_TPH is disabled?
include/linux/pci-tph.h uses PCI_TPH_LOC_NONE here without including
linux/pci.h or uapi/linux/pci_regs.h. Because include/rdma/ib_verbs.h
includes linux/pci-tph.h before any header that pulls in pci_regs.h,
translation units such as drivers/infiniband/core/mr_pool.c and
drivers/infiniband/hw/mlx5/dmah.c fail to compile with PCI_TPH_LOC_NONE
undeclared.
A later patch in the series, commit "PCI/TPH: Add macros to check standard
and extended TPH support", adds #include <linux/pci.h> to this header,
leaving earlier commits in the series and standalone stable backports of
this patch broken when CONFIG_PCIE_TPH is disabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118091238.45992-1-fengchengwen@huawei.com?part=1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:38 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
` (10 subsequent siblings)
12 siblings, 1 reply; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Split tph_enabled from shared pci_dev bitfield into spare bit of tph_cap's
u16: tph_cap is immutable post-enumeration (15 bits for offset), remaining
1 bit stores tph_enabled. Removes cross-bitfield concurrent write hazards
highlighted by Sashiko after VFIO TPH exposure. No functional changes.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
include/linux/pci.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 6482549a8417..6b577890fbb2 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -478,7 +478,6 @@ struct pci_dev {
unsigned int ats_enabled:1; /* Address Translation Svc */
unsigned int pasid_enabled:1; /* Process Address Space ID */
unsigned int pri_enabled:1; /* Page Request Interface */
- unsigned int tph_enabled:1; /* TLP Processing Hints */
unsigned int fm_enabled:1; /* Flit Mode (segment captured) */
unsigned int is_managed:1; /* Managed via devres */
unsigned int is_msi_managed:1; /* MSI release via devres installed */
@@ -596,7 +595,8 @@ struct pci_dev {
struct gpio_desc *wake; /* WAKE# GPIO */
#ifdef CONFIG_PCIE_TPH
- u16 tph_cap; /* TPH capability offset */
+ u16 tph_cap:15; /* TPH capability offset */
+ u16 tph_enabled:1; /* Whether TPH is enabled */
u8 tph_mode; /* TPH mode */
u8 tph_req_type; /* TPH requester type */
#endif
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* Re: [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
2026-11-18 9:12 ` [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
@ 2026-11-18 9:38 ` sashiko-bot
0 siblings, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-11-18 9:38 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] PCI/TPH: missing Fixes trailer for tph_enabled bitfield race fix
--
commit b7d26d11444570684d469d0d07a19d348c65832e
Author: Chengwen Feng <fengchengwen@huawei.com>
> PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
>
> Split tph_enabled from shared pci_dev bitfield into spare bit of tph_cap's
> u16: tph_cap is immutable post-enumeration (15 bits for offset), remaining
> 1 bit stores tph_enabled. Removes cross-bitfield concurrent write hazards
> highlighted by Sashiko after VFIO TPH exposure. No functional changes.
>
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
[Severity: Low]
Since this fixes the concurrent bitfield update hazard on tph_enabled,
should this include a Fixes: tag for the commit that introduced it:
Fixes: f69767a1ada3 ("PCI: Add TLP Processing Hints (TPH) support")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118091238.45992-1-fengchengwen@huawei.com?part=2
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v21 03/13] PCI/TPH: Cache negotiated max requester type at probe time
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:45 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
` (9 subsequent siblings)
12 siblings, 1 reply; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
pcie_enable_tph() recalculates the TPH requester capability and queries
root port completer type on every invocation, causing redundant config
space reads.
Calculate the negotiated maximum requester type once during pci_tph_init()
and cache it in a new pdev->tph_max_type bitfield. Simplify the enable path
by reusing the cached value to eliminate repeated hardware access.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 45 ++++++++++++++++++++++++---------------------
include/linux/pci.h | 4 +++-
2 files changed, 27 insertions(+), 22 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index c4ceb08204d6..58f7ba41e1f7 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -375,9 +375,8 @@ EXPORT_SYMBOL(pcie_disable_tph);
* - PCI_TPH_ST_DS_MODE: Device Specific Mode
*
* Check whether the mode is actually supported by the device before enabling
- * and return an error if not. Additionally determine what types of requests,
- * TPH or extended TPH, can be issued by the device based on its TPH requester
- * capability and the Root Port's completer capability.
+ * and return an error if not. Automatically uses the pre-cached tph_max_type
+ * value, the negotiated maximum requester type between device and root port.
*
* Return: 0 on success, otherwise negative value (-errno)
*/
@@ -385,7 +384,6 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
{
u32 reg;
u8 dev_modes;
- u8 rp_req_type;
/* Honor "notph" kernel parameter */
if (pci_tph_disabled)
@@ -397,6 +395,9 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
if (pdev->tph_enabled)
return -EBUSY;
+ if (pdev->tph_max_type == PCI_TPH_REQ_DISABLE)
+ return -EINVAL;
+
/* Sanitize and check ST mode compatibility */
mode &= PCI_TPH_CTRL_MODE_SEL_MASK;
dev_modes = get_st_modes(pdev);
@@ -405,23 +406,7 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
pdev->tph_mode = mode;
- /* Get req_type supported by device and its Root Port */
- pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
- if (FIELD_GET(PCI_TPH_CAP_EXT_TPH, reg))
- pdev->tph_req_type = PCI_TPH_REQ_EXT_TPH;
- else
- pdev->tph_req_type = PCI_TPH_REQ_TPH_ONLY;
-
- /* Check if the device is behind a Root Port */
- if (pci_pcie_type(pdev) != PCI_EXP_TYPE_RC_END) {
- rp_req_type = get_rp_completer_type(pdev);
-
- /* Final req_type is the smallest value of two */
- pdev->tph_req_type = min(pdev->tph_req_type, rp_req_type);
- }
-
- if (pdev->tph_req_type == PCI_TPH_REQ_DISABLE)
- return -EINVAL;
+ pdev->tph_req_type = pdev->tph_max_type;
/* Write them into TPH control register */
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, ®);
@@ -508,13 +493,31 @@ void pci_no_tph(void)
void pci_tph_init(struct pci_dev *pdev)
{
+ u8 tph_req_type, rp_req_type;
int num_entries;
u32 save_size;
+ u32 reg = 0;
pdev->tph_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_TPH);
if (!pdev->tph_cap)
return;
+ /* Get req_type supported by device and its Root Port */
+ pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
+ if (FIELD_GET(PCI_TPH_CAP_EXT_TPH, reg))
+ tph_req_type = PCI_TPH_REQ_EXT_TPH;
+ else
+ tph_req_type = PCI_TPH_REQ_TPH_ONLY;
+
+ /* Check if the device is behind a Root Port */
+ if (pci_pcie_type(pdev) != PCI_EXP_TYPE_RC_END) {
+ rp_req_type = get_rp_completer_type(pdev);
+ /* Final req_type is the smallest value of two */
+ tph_req_type = min(tph_req_type, rp_req_type);
+ }
+
+ pdev->tph_max_type = tph_req_type;
+
num_entries = pcie_tph_get_st_table_size(pdev);
save_size = sizeof(u32) + num_entries * sizeof(u16);
pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_TPH, save_size);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 6b577890fbb2..1dc3cb011dff 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -595,7 +595,9 @@ struct pci_dev {
struct gpio_desc *wake; /* WAKE# GPIO */
#ifdef CONFIG_PCIE_TPH
- u16 tph_cap:15; /* TPH capability offset */
+ u16 tph_cap:13; /* TPH capability offset */
+ u16 tph_max_type:2; /* Negotiated max TPH requester type
+ between device and root port */
u16 tph_enabled:1; /* Whether TPH is enabled */
u8 tph_mode; /* TPH mode */
u8 tph_req_type; /* TPH requester type */
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (2 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:48 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
` (8 subsequent siblings)
12 siblings, 1 reply; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Refactor the core TPH enable logic by renaming the original function to
pcie_enable_tph_ext(), which accepts an explicit requester type argument.
- Keep the original pcie_enable_tph() as a compatibility wrapper; it uses
the pre-cached tph_max_type negotiated capability automatically, so
existing drivers calling pcie_enable_tph() require no modification.
- Export new pcie_enable_tph_ext() to support explicit requester type
selection, intended for use by the upcoming VFIO TPH series.
- Add requester type validation to reject values out of hardware negotiated
range or invalid reserved types.
Update pci-tph.h header and CONFIG_PCIE_TPH stub inline definitions
to match the new function signatures.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 41 ++++++++++++++++++++++++++++++-----------
include/linux/pci-tph.h | 8 ++++++--
2 files changed, 36 insertions(+), 13 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 58f7ba41e1f7..875c381e094f 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -366,21 +366,18 @@ void pcie_disable_tph(struct pci_dev *pdev)
EXPORT_SYMBOL(pcie_disable_tph);
/**
- * pcie_enable_tph - Enable TPH support for device using a specific ST mode
+ * pcie_enable_tph_ext - Enable TPH support with specific ST mode and
+ * requester type
* @pdev: PCI device
- * @mode: ST mode to enable. Current supported modes include:
- *
- * - PCI_TPH_ST_NS_MODE: NO ST Mode
- * - PCI_TPH_ST_IV_MODE: Interrupt Vector Mode
- * - PCI_TPH_ST_DS_MODE: Device Specific Mode
+ * @mode: ST mode to enable: PCI_TPH_ST_(NS_MODE / IV_MODE / DS_MODE)
+ * @req_type: Requester type to enable: PCI_TPH_REQ_(TPH_ONLY / EXT_TPH)
*
- * Check whether the mode is actually supported by the device before enabling
- * and return an error if not. Automatically uses the pre-cached tph_max_type
- * value, the negotiated maximum requester type between device and root port.
+ * Check whether the mode and requester type is actually supported by the
+ * device before enabling and return an error if not.
*
* Return: 0 on success, otherwise negative value (-errno)
*/
-int pcie_enable_tph(struct pci_dev *pdev, int mode)
+int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type)
{
u32 reg;
u8 dev_modes;
@@ -398,6 +395,11 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
if (pdev->tph_max_type == PCI_TPH_REQ_DISABLE)
return -EINVAL;
+ /* Sanitize and check TPH requester type compatibility */
+ if (req_type == PCI_TPH_REQ_DISABLE || req_type == 2 ||
+ req_type > pdev->tph_max_type)
+ return -EINVAL;
+
/* Sanitize and check ST mode compatibility */
mode &= PCI_TPH_CTRL_MODE_SEL_MASK;
dev_modes = get_st_modes(pdev);
@@ -406,7 +408,7 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
pdev->tph_mode = mode;
- pdev->tph_req_type = pdev->tph_max_type;
+ pdev->tph_req_type = req_type;
/* Write them into TPH control register */
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, ®);
@@ -420,6 +422,23 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
return 0;
}
+EXPORT_SYMBOL(pcie_enable_tph_ext);
+
+/**
+ * pcie_enable_tph - Enable TPH support for device using a specific ST mode
+ * @pdev: PCI device
+ * @mode: ST mode to enable: PCI_TPH_ST_(NS_MODE / IV_MODE / DS_MODE)
+ *
+ * Wrapper for pcie_enable_tph_ext(), automatically uses the pre-cached
+ * tph_max_type value (negotiated maximum requester type between device and
+ * root port).
+ *
+ * Return: 0 on success, otherwise negative value (-errno)
+ */
+int pcie_enable_tph(struct pci_dev *pdev, u8 mode)
+{
+ return pcie_enable_tph_ext(pdev, mode, pdev->tph_max_type);
+}
EXPORT_SYMBOL(pcie_enable_tph);
void pci_restore_tph_state(struct pci_dev *pdev)
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 159ffb82b34e..d3ff4ccfcc6b 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -27,7 +27,8 @@ int pcie_tph_get_cpu_st(struct pci_dev *dev,
enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag);
void pcie_disable_tph(struct pci_dev *pdev);
-int pcie_enable_tph(struct pci_dev *pdev, int mode);
+int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
+int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
#else
@@ -39,7 +40,10 @@ static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
unsigned int cpu, u16 *tag)
{ return -EINVAL; }
static inline void pcie_disable_tph(struct pci_dev *pdev) { }
-static inline int pcie_enable_tph(struct pci_dev *pdev, int mode)
+static inline int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode,
+ u8 req_type)
+{ return -EINVAL; }
+static inline int pcie_enable_tph(struct pci_dev *pdev, u8 mode)
{ return -EINVAL; }
static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
{ return 0; }
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (3 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:53 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 06/13] PCI/TPH: Add macros to check standard and extended TPH support Chengwen Feng
` (7 subsequent siblings)
12 siblings, 1 reply; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Refactor pcie_tph_get_cpu_st() and add pcie_tph_get_cpu_st_ext() extended
variant with explicit requester type parameter.
- Preserve original pcie_tph_get_cpu_st() as a compatibility wrapper,
using the default negotiated tph_max_type automatically, so existing
callers require no modification.
- Add exported pcie_tph_get_cpu_st_ext() to accept an explicit requester
type argument for ST namespace selection, used by upcoming VFIO TPH
support.
- Add requester type validation to reject invalid values and enforce the
hardware negotiated tph_max_type capability limit.
Update pci-tph.h header and CONFIG_PCIE_TPH stub inline definitions
to match the new function signatures.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 45 ++++++++++++++++++++++++++++++++---------
include/linux/pci-tph.h | 10 +++++++--
2 files changed, 44 insertions(+), 11 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 875c381e094f..51a065d83104 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -233,20 +233,22 @@ static int write_tag_to_st_table(struct pci_dev *pdev, int index, u16 tag)
}
/**
- * pcie_tph_get_cpu_st() - Retrieve Steering Tag for a target memory associated
- * with a specific CPU
+ * pcie_tph_get_cpu_st_ext() - Retrieve Steering Tag for a target memory
+ * associated with a specific CPU, with explicit requester type selection
* @pdev: PCI device
* @mem_type: target memory type (volatile or persistent RAM)
+ * @req_type: TPH requester type to select ST namespace
* @cpu: associated CPU id
* @tag: Steering Tag to be returned
*
- * Return the Steering Tag for a target memory that is associated with a
- * specific CPU as indicated by cpu.
+ * Return the Steering Tag for a target memory associated with a specific CPU,
+ * using the specified TPH requester type to select the correct ST namespace.
+ * Validates requester type against hardware negotiated capability limits.
*
* Return: 0 if success, otherwise negative value (-errno)
*/
-int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
- unsigned int cpu, u16 *tag)
+int pcie_tph_get_cpu_st_ext(struct pci_dev *pdev, enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu, u16 *tag)
{
#ifdef CONFIG_ACPI
struct pci_dev *rp;
@@ -255,6 +257,10 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
u32 cpu_uid;
int ret;
+ if (req_type == PCI_TPH_REQ_DISABLE || req_type == 2 ||
+ req_type > pdev->tph_max_type)
+ return -EINVAL;
+
ret = acpi_get_cpu_uid(cpu, &cpu_uid);
if (ret != 0)
return ret;
@@ -270,17 +276,38 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
return -EINVAL;
}
- *tag = tph_extract_tag(mem_type, pdev->tph_req_type, &info);
+ *tag = tph_extract_tag(mem_type, req_type, &info);
- pci_dbg(pdev, "get steering tag: mem_type=%s, cpu=%d, tag=%#04x\n",
+ pci_dbg(pdev, "get steering tag: mem_type=%s, req_type=%u, cpu=%d, tag=%#04x\n",
(mem_type == TPH_MEM_TYPE_VM) ? "volatile" : "persistent",
- cpu, *tag);
+ req_type, cpu, *tag);
return 0;
#else
return -ENODEV;
#endif
}
+EXPORT_SYMBOL(pcie_tph_get_cpu_st_ext);
+
+/**
+ * pcie_tph_get_cpu_st() - Retrieve Steering Tag for a target memory associated
+ * with a specific CPU
+ * @pdev: PCI device
+ * @mem_type: target memory type (volatile or persistent RAM)
+ * @cpu: associated CPU id
+ * @tag: Steering Tag to be returned
+ *
+ * Return the Steering Tag for a target memory associated with a specific CPU,
+ * using the default negotiated TPH requester type.
+ *
+ * Return: 0 if success, otherwise negative value (-errno)
+ */
+int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
+ unsigned int cpu, u16 *tag)
+{
+ return pcie_tph_get_cpu_st_ext(pdev, mem_type, pdev->tph_max_type,
+ cpu, tag);
+}
EXPORT_SYMBOL(pcie_tph_get_cpu_st);
/**
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index d3ff4ccfcc6b..b2bec02f3ea0 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -23,8 +23,9 @@ enum tph_mem_type {
#ifdef CONFIG_PCIE_TPH
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
-int pcie_tph_get_cpu_st(struct pci_dev *dev,
- enum tph_mem_type mem_type,
+int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu, u16 *tag);
+int pcie_tph_get_cpu_st(struct pci_dev *dev, enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag);
void pcie_disable_tph(struct pci_dev *pdev);
int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
@@ -35,6 +36,11 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
+static inline int pcie_tph_get_cpu_st_ext(struct pci_dev *dev,
+ enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu,
+ u16 *tag)
+{ return -EINVAL; }
static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag)
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* Re: [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
2026-11-18 9:12 ` [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
@ 2026-11-18 9:53 ` sashiko-bot
0 siblings, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-11-18 9:53 UTC (permalink / raw)
To: Chengwen Feng; +Cc: linux-pci
> Refactor pcie_tph_get_cpu_st() and add pcie_tph_get_cpu_st_ext() extended
> variant with explicit requester type parameter.
>
> - Preserve original pcie_tph_get_cpu_st() as a compatibility wrapper,
> using the default negotiated tph_max_type automatically, so existing
> callers require no modification.
> - Add exported pcie_tph_get_cpu_st_ext() to accept an explicit requester
> type argument for ST namespace selection, used by upcoming VFIO TPH
> support.
> - Add requester type validation to reject invalid values and enforce the
> hardware negotiated tph_max_type capability limit.
>
> Update pci-tph.h header and CONFIG_PCIE_TPH stub inline definitions
> to match the new function signatures.
>
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118091238.45992-1-fengchengwen@huawei.com?part=5
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v21 06/13] PCI/TPH: Add macros to check standard and extended TPH support
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (4 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
` (6 subsequent siblings)
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add two helper macros for TPH capability checking:
- pcie_std_tph_supported(): check basic standard TPH support
- pcie_ext_tph_supported(): check extended TPH requester support
They rely on the cached tph_max_type value validated during probe,
and provide consistent capability queries for subsequent VFIO TPH
virtualization code. Stub definitions are added for !CONFIG_PCIE_TPH.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
include/linux/pci-tph.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index b2bec02f3ea0..50640fdc84e1 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -9,6 +9,8 @@
#ifndef LINUX_PCI_TPH_H
#define LINUX_PCI_TPH_H
+#include <linux/pci.h>
+
/*
* According to the ECN for PCI Firmware Spec, Steering Tag can be different
* depending on the memory type: Volatile Memory or Persistent Memory. When a
@@ -21,6 +23,18 @@ enum tph_mem_type {
};
#ifdef CONFIG_PCIE_TPH
+/**
+ * pcie_std_tph_supported - check standard TPH requester support
+ * @pdev: PCI device
+ */
+#define pcie_std_tph_supported(pdev) \
+ ((pdev)->tph_max_type >= PCI_TPH_REQ_TPH_ONLY)
+/**
+ * pcie_ext_tph_supported - check extended TPH requester support
+ * @pdev: PCI device
+ */
+#define pcie_ext_tph_supported(pdev) \
+ ((pdev)->tph_max_type == PCI_TPH_REQ_EXT_TPH)
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
@@ -33,6 +47,8 @@ int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
#else
+#define pcie_std_tph_supported(pdev) false
+#define pcie_ext_tph_supported(pdev) false
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (5 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 06/13] PCI/TPH: Add macros to check standard and extended TPH support Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 08/13] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
` (5 subsequent siblings)
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add batch variant pcie_tph_set_st_entries() to write a contiguous range
of ST table entries, returning the number of successfully written entries
or negative errno.
- Keep original pcie_tph_set_st_entry() as a wrapper for backward
compatibility
- Disable/enable TPH once per batch instead of per entry for better
performance
- Add header declaration and fallback static inline version
- Add error logging for failed ST entry writes
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 77 +++++++++++++++++++++++++----------------
include/linux/pci-tph.h | 6 ++++
2 files changed, 54 insertions(+), 29 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 51a065d83104..c8b867dd233d 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -311,21 +311,19 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
EXPORT_SYMBOL(pcie_tph_get_cpu_st);
/**
- * pcie_tph_set_st_entry() - Set Steering Tag in the ST table entry
+ * pcie_tph_set_st_entries - Batch set Steering Tag entries in the ST table
* @pdev: PCI device
- * @index: ST table entry index
- * @tag: Steering Tag to be written
+ * @start: starting ST table index
+ * @count: number of contiguous entries to write
+ * @tags: array of u16 tag values to program
*
- * Figure out the proper location of ST table, either in the MSI-X table or
- * in the TPH Extended Capability space, and write the Steering Tag into
- * the ST entry pointed by index.
- *
- * Return: 0 if success, otherwise negative value (-errno)
+ * Return: number of successfully written entries (>0), or negative errno
*/
-int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
+int pcie_tph_set_st_entries(struct pci_dev *pdev, unsigned int start,
+ unsigned int count, const u16 *tags)
{
- u32 loc;
int err = 0;
+ u32 loc, i;
if (!pdev->tph_cap)
return -EINVAL;
@@ -333,9 +331,12 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
if (!pdev->tph_enabled)
return -EINVAL;
+ if (!count)
+ return -EINVAL;
+
/* No need to write tag if device is in "No ST Mode" */
if (pdev->tph_mode == PCI_TPH_ST_NS_MODE)
- return 0;
+ return count;
/*
* Disable TPH before updating ST to avoid potential instability as
@@ -344,29 +345,47 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
loc = pcie_tph_get_st_table_loc(pdev);
-
- switch (loc) {
- case PCI_TPH_LOC_MSIX:
- err = pci_msix_write_tph_tag(pdev, index, tag);
- break;
- case PCI_TPH_LOC_CAP:
- err = write_tag_to_st_table(pdev, index, tag);
- break;
- default:
- err = -EINVAL;
- }
-
- if (err) {
- pcie_disable_tph(pdev);
- return err;
+ for (i = 0; i < count; i++) {
+ switch (loc) {
+ case PCI_TPH_LOC_MSIX:
+ err = pci_msix_write_tph_tag(pdev, start + i, tags[i]);
+ break;
+ case PCI_TPH_LOC_CAP:
+ err = write_tag_to_st_table(pdev, start + i, tags[i]);
+ break;
+ default:
+ err = -EINVAL;
+ }
+ if (err != 0) {
+ pci_err(pdev, "failed to set steering tag: %s table, index=%u, tags=%u, ret=%d\n",
+ (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST",
+ start + i, tags[i], err);
+ pcie_disable_tph(pdev);
+ return i > 0 ? i : (err > 0 ? -EINVAL : err);
+ }
}
set_ctrl_reg_req_en(pdev, pdev->tph_req_type);
- pci_dbg(pdev, "set steering tag: %s table, index=%d, tag=%#04x\n",
- (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST", index, tag);
+ pci_dbg(pdev, "batch set steering tag: %s table, start=%u, count=%u success!\n",
+ (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST", start, count);
- return 0;
+ return count;
+}
+EXPORT_SYMBOL(pcie_tph_set_st_entries);
+
+/**
+ * pcie_tph_set_st_entry() - Set Steering Tag in the ST table entry
+ * @pdev: PCI device
+ * @index: ST table entry index
+ * @tag: Steering Tag to be written
+ *
+ * Return: 0 if success, otherwise negative value (-errno)
+ */
+int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
+{
+ int ret = pcie_tph_set_st_entries(pdev, index, 1, &tag);
+ return ret == 1 ? 0 : ret;
}
EXPORT_SYMBOL(pcie_tph_set_st_entry);
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 50640fdc84e1..58c8d416aff2 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -37,6 +37,8 @@ enum tph_mem_type {
((pdev)->tph_max_type == PCI_TPH_REQ_EXT_TPH)
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
+int pcie_tph_set_st_entries(struct pci_dev *pdev, unsigned int start,
+ unsigned int count, const u16 *tags);
int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
u8 req_type, unsigned int cpu, u16 *tag);
int pcie_tph_get_cpu_st(struct pci_dev *dev, enum tph_mem_type mem_type,
@@ -52,6 +54,10 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
+static inline int pcie_tph_set_st_entries(struct pci_dev *pdev,
+ unsigned int start,
+ unsigned int count, const u16 *tags)
+{ return -EINVAL; }
static inline int pcie_tph_get_cpu_st_ext(struct pci_dev *dev,
enum tph_mem_type mem_type,
u8 req_type, unsigned int cpu,
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 08/13] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (6 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
` (4 subsequent siblings)
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add new helper pci_tph_dsm_supported() to check whether the root port
of a given PCI device exposes the TPH ST ACPI _DSM method.
Wrap all ACPI logic under CONFIG_ACPI guard, return false directly
when ACPI is disabled.
This helper is prepared for follow-up VFIO TPH virtualization patches
to uniformly judge platform CPU TPH resolving support.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 28 ++++++++++++++++++++++++++++
include/linux/pci-tph.h | 3 +++
2 files changed, 31 insertions(+)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index c8b867dd233d..c3d940745fe4 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -587,3 +587,31 @@ void pci_tph_init(struct pci_dev *pdev)
save_size = sizeof(u32) + num_entries * sizeof(u16);
pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_TPH, save_size);
}
+
+/**
+ * pcie_tph_dsm_supported - Check if root port exposes TPH ST _DSM
+ * @pdev: target PCI device
+ *
+ * Return true if root port of @pdev provides TPH ST DSM function,
+ * false otherwise (ACPI disabled or DSM missing).
+ */
+bool pcie_tph_dsm_supported(struct pci_dev *pdev)
+{
+#ifdef CONFIG_ACPI
+ struct pci_dev *rp = pcie_find_root_port(pdev);
+ acpi_handle handle;
+
+ if (!rp || !rp->bus || !rp->bus->bridge)
+ return false;
+
+ handle = ACPI_HANDLE(rp->bus->bridge);
+ if (!handle)
+ return false;
+
+ return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 7,
+ BIT(TPH_ST_DSM_FUNC_INDEX));
+#else
+ return false;
+#endif
+}
+EXPORT_SYMBOL_GPL(pcie_tph_dsm_supported);
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 58c8d416aff2..c19dbbb87620 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -48,6 +48,7 @@ int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
+bool pcie_tph_dsm_supported(struct pci_dev *pdev);
#else
#define pcie_std_tph_supported(pdev) false
#define pcie_ext_tph_supported(pdev) false
@@ -77,6 +78,8 @@ static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
{ return 0; }
static inline u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
{ return PCI_TPH_LOC_NONE; }
+static inline bool pcie_tph_dsm_supported(struct pci_dev *pdev)
+{ return false; }
#endif
#endif /* LINUX_PCI_TPH_H */
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (7 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 08/13] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 10/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
` (3 subsequent siblings)
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Implement basic PCIe TPH support restricted to NO-ST mode:
- Add a tph_policy module parameter, currently locked to NO-ST.
The parameter range is reserved for future TPH policies.
- Virtualize the TPH extended capability register: mask IV/DS
modes, ST location and ST size fields, only NO-ST capability
is visible to userspace.
- Gate all TPH control register writes behind tph_opt_in.
Opt-in is one-shot (0 -> 1 only), no reset path.
- Validate TPH control register writes: only NO-ST mode and
supported requester enable values are accepted; invalid writes
are dropped. CTRL reads always fetch hardware state directly.
- Add VFIO_DEVICE_FEATURE_TPH ioctl for opt-in (SET) and VFIO
usable capability query (GET). GET fails with -EINVAL if the
device has not been opted in.
- ST table accesses beyond the base TPH capability return zero
on read and writes are discarded.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 16 +++
drivers/vfio/pci/vfio_pci_config.c | 167 ++++++++++++++++++++++++++---
drivers/vfio/pci/vfio_pci_core.c | 46 ++++++++
drivers/vfio/pci/vfio_pci_priv.h | 4 +
include/linux/vfio_pci_core.h | 3 +
include/uapi/linux/vfio.h | 22 ++++
6 files changed, 241 insertions(+), 17 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index e7c012febe91..77764eabb71a 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -60,6 +60,21 @@ static bool disable_denylist;
module_param(disable_denylist, bool, 0444);
MODULE_PARM_DESC(disable_denylist, "Disable use of device denylist. Disabling the denylist allows binding to devices with known errata that may lead to exploitable stability or security issues when accessed by untrusted users.");
+static unsigned int tph_policy;
+static int tph_policy_set(const char *val, const struct kernel_param *kp)
+{
+ return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
+ VFIO_PCI_TPH_POLICY_NO_ST);
+}
+static const struct kernel_param_ops tph_param_ops = {
+ .set = tph_policy_set,
+ .get = param_get_uint,
+};
+module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
+MODULE_PARM_DESC(tph_policy,
+ "Global TPH policy level (0=default No-ST):\n"
+ "0 = No-ST mode: ST programming unavailable\n");
+
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
switch (pdev->vendor) {
@@ -142,6 +157,7 @@ static int vfio_pci_init_dev(struct vfio_device *core_vdev)
#ifdef CONFIG_VFIO_PCI_VGA
vdev->disable_vga = disable_vga;
#endif
+ vdev->tph_policy = tph_policy;
return vfio_pci_core_init_dev(core_vdev);
}
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 9914f3ac69ae..3946ed2ec0cf 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -20,8 +20,10 @@
* must be negotiated with the underlying OS.
*/
+#include <linux/bitfield.h>
#include <linux/fs.h>
#include <linux/pci.h>
+#include <linux/pci-tph.h>
#include <linux/uaccess.h>
#include <linux/vfio.h>
#include <linux/slab.h>
@@ -1085,6 +1087,148 @@ static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
return 0;
}
+static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
+{
+ u8 cap;
+ int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
+ PCI_STD_HEADER_SIZEOF;
+ cap = vdev->pci_config_map[pos];
+
+ if (cap == PCI_CAP_ID_BASIC)
+ return 0;
+
+ /* XXX Can we have to abutting capabilities of the same type? */
+ while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
+ pos--;
+
+ return pos;
+}
+
+/* Permissions for TPH extended capability */
+static int __init init_pci_ext_cap_tph_perm(struct perm_bits *perm)
+{
+ /*
+ * We only virtualize the TPH header (excluding the ST table).
+ * For the ST table (if any), the read operation returns 0 by
+ * default, and the write operation is discarded by default.
+ */
+ if (alloc_perm_bits(perm, PCI_TPH_BASE_SIZEOF))
+ return -ENOMEM;
+
+ p_setd(perm, 0, ALL_VIRT, NO_WRITE);
+ p_setd(perm, PCI_TPH_CAP, ALL_VIRT, NO_WRITE);
+
+ p_setd(perm, PCI_TPH_CTRL, ALL_VIRT,
+ PCI_TPH_CTRL_MODE_SEL_MASK | PCI_TPH_CTRL_REQ_EN_MASK);
+
+ return 0;
+}
+
+static void vfio_tph_vcap_rewrite(struct vfio_pci_core_device *vdev, int pos)
+{
+ __le32 *vptr = (__le32 *)&vdev->vconfig[pos + PCI_TPH_CAP];
+ struct pci_dev *pdev = vdev->pdev;
+ u32 val = le32_to_cpu(*vptr);
+
+ if (!pcie_ext_tph_supported(pdev)) {
+ /* Remove extended TPH if root-port doesn't support */
+ val &= ~PCI_TPH_CAP_EXT_TPH;
+ }
+
+ if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_NO_ST) {
+ /*
+ * NO-ST policy: mask IV/DS mode, ST location and ST size fields.
+ * Userspace only sees NO-ST capability.
+ */
+ val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
+ PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
+ }
+
+ *vptr = cpu_to_le32(val);
+
+ /* Cache the final virtualized TPH capability value */
+ vdev->tph_vcap = val;
+}
+
+static int vfio_tph_config_read(struct vfio_pci_core_device *vdev, int pos,
+ int count, struct perm_bits *perm,
+ int offset, __le32 *val)
+{
+ int ret;
+
+ if (offset >= PCI_TPH_BASE_SIZEOF) {
+ *val = cpu_to_le32(0);
+ return count;
+ }
+
+ if (offset < PCI_TPH_CTRL)
+ return vfio_default_config_read(vdev, pos, count, perm,
+ offset, val);
+
+ /* Read the actual device value for CTRL register. */
+ ret = vfio_user_config_read(vdev->pdev, pos, val, count);
+ return ret != 0 ? ret : count;
+}
+
+static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
+ int count, struct perm_bits *perm,
+ int offset, __le32 val)
+{
+ u16 start = vfio_find_cap_start(vdev, pos);
+ struct pci_dev *pdev = vdev->pdev;
+ u32 org_ctrl, new_ctrl, cap;
+ u8 mode, req, org_req;
+ __le32 ctrl_val = 0;
+ u16 cmd;
+ int ret;
+
+ if (!vdev->tph_opt_in || offset >= PCI_TPH_BASE_SIZEOF ||
+ offset < PCI_TPH_CTRL)
+ return count;
+
+ cmd = vfio_pci_memory_lock_and_enable(vdev);
+
+ /*
+ * Synchronize the CTRL value of the hardware to the virt. Otherwise,
+ * the value will be inconsistent when the device is reset.
+ */
+ vfio_user_config_read(pdev, start + PCI_TPH_CTRL, &ctrl_val, 4);
+ vfio_default_config_write(vdev, start + PCI_TPH_CTRL, 4, perm,
+ PCI_TPH_CTRL, ctrl_val);
+ org_ctrl = le32_to_cpu(ctrl_val);
+
+ /* Do write operation to virt. */
+ ret = vfio_default_config_write(vdev, pos, count, perm, offset, val);
+ if (ret != count)
+ goto out;
+
+ /* Reads the TPH_CTRL value after the write operation. */
+ new_ctrl = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CTRL]);
+ if (new_ctrl == org_ctrl)
+ goto out; /* Exit because there is no change. */
+
+ cap = vdev->tph_vcap;
+ mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, new_ctrl);
+ req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, new_ctrl);
+ if (mode != PCI_TPH_ST_NS_MODE || !(cap & (1u << mode)) ||
+ req == 0x2 /* reserved */ ||
+ (req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
+ goto out; /* Drop invalid or unsupported write value. */
+
+ org_req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, org_ctrl);
+ if (req == org_req)
+ goto out; /* Only care about requester enable. */
+
+ if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH)
+ pcie_enable_tph_ext(pdev, mode, req);
+ else if (req == PCI_TPH_REQ_DISABLE)
+ pcie_disable_tph(vdev->pdev);
+
+out:
+ vfio_pci_memory_unlock_and_restore(vdev, cmd);
+ return count;
+}
+
/*
* Initialize the shared permission tables
*/
@@ -1100,6 +1244,7 @@ void vfio_pci_uninit_perm_bits(void)
free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
+ free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_TPH]);
}
int __init vfio_pci_init_perm_bits(void)
@@ -1120,6 +1265,9 @@ int __init vfio_pci_init_perm_bits(void)
/* Extended capabilities */
ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
+ ret |= init_pci_ext_cap_tph_perm(&ecap_perms[PCI_EXT_CAP_ID_TPH]);
+ ecap_perms[PCI_EXT_CAP_ID_TPH].readfn = vfio_tph_config_read;
+ ecap_perms[PCI_EXT_CAP_ID_TPH].writefn = vfio_tph_config_write;
ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
ecap_perms[PCI_EXT_CAP_ID_DVSEC].writefn = vfio_raw_config_write;
@@ -1129,23 +1277,6 @@ int __init vfio_pci_init_perm_bits(void)
return ret;
}
-static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
-{
- u8 cap;
- int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
- PCI_STD_HEADER_SIZEOF;
- cap = vdev->pci_config_map[pos];
-
- if (cap == PCI_CAP_ID_BASIC)
- return 0;
-
- /* XXX Can we have to abutting capabilities of the same type? */
- while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
- pos--;
-
- return pos;
-}
-
static int vfio_msi_config_read(struct vfio_pci_core_device *vdev, int pos,
int count, struct perm_bits *perm,
int offset, __le32 *val)
@@ -1702,6 +1833,8 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev)
ret = vfio_fill_vconfig_bytes(vdev, epos, len);
if (ret)
return ret;
+ if (ecap == PCI_EXT_CAP_ID_TPH)
+ vfio_tph_vcap_rewrite(vdev, epos);
/*
* If we're just using this capability to anchor the list,
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..05a03aa2bceb 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -22,6 +22,7 @@
#include <linux/notifier.h>
#include <linux/pagemap.h>
#include <linux/pci.h>
+#include <linux/pci-tph.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/types.h>
@@ -629,6 +630,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_free_zdev;
+ vdev->tph_opt_in = 0;
+
msix_pos = pdev->msix_cap;
if (msix_pos) {
u16 flags;
@@ -1594,6 +1597,47 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
return 0;
}
+static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+{
+ /* NO-ST only, no capability bits exposed now */
+ return 0;
+}
+
+static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
+ u32 flags,
+ struct vfio_device_feature_tph __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_tph tph;
+ int ret;
+
+ if (!pcie_std_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz,
+ VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET,
+ sizeof(tph));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&tph, arg, sizeof(tph)))
+ return -EFAULT;
+
+ if (flags & VFIO_DEVICE_FEATURE_SET) {
+ if (tph.flags != 0)
+ return -EINVAL;
+ vdev->tph_opt_in = 1;
+ return 0;
+ }
+
+ if (!vdev->tph_opt_in)
+ return -EINVAL;
+
+ tph.flags = vfio_pci_tph_get_capability(vdev);
+
+ return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
+}
+
int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz)
{
@@ -1614,6 +1658,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
case VFIO_DEVICE_FEATURE_ZPCI_ERROR:
return vfio_pci_zdev_feature_err(device, flags, arg, argsz);
+ case VFIO_DEVICE_FEATURE_TPH:
+ return vfio_pci_core_feature_tph(vdev, flags, arg, argsz);
default:
return -ENOTTY;
}
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 4e7162234a2e..f3d53af93dfd 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -11,6 +11,10 @@
/* Cap maximum number of ioeventfds per device (arbitrary) */
#define VFIO_PCI_IOEVENTFD_MAX 1000
+enum vfio_pci_tph_policy {
+ VFIO_PCI_TPH_POLICY_NO_ST = 0,
+};
+
struct vfio_pci_ioeventfd {
struct list_head next;
struct vfio_pci_core_device *vdev;
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 2f3fa2787737..17c4ad490dc2 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -151,6 +151,9 @@ struct vfio_pci_core_device {
struct notifier_block nb;
struct rw_semaphore memory_lock;
struct list_head dmabufs;
+ u8 tph_policy;
+ u8 tph_opt_in;
+ u32 tph_vcap;
};
enum vfio_pci_io_width {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index e41437fa17ad..00e23c0b21e6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1555,6 +1555,28 @@ struct vfio_device_feature_zpci_err {
#define VFIO_DEVICE_FEATURE_ZPCI_ERROR 13
+/*
+ * VFIO_DEVICE_FEATURE_TPH - Control and query VFIO TPH feature capabilities
+ *
+ * SET: Opt-in to enable the VFIO TPH feature for the device; flags must be
+ * zero.
+ * GET: Return VFIO-usable TPH capability bits in flags (currently zero).
+ * Returns an error if SET opt-in has not been performed.
+ *
+ * Typical userspace sequence:
+ * 1. Read virtualized PCI TPH capability via VFIO config space access to
+ * discover underlying hardware PCI TPH support.
+ * 2. Invoke SET to opt-in for VFIO TPH feature permission.
+ * 3. Invoke GET to retrieve the set of TPH capabilities accessible via VFIO.
+ * 4. Write the TPH control register through VFIO config space to activate
+ * device TPH hardware.
+ */
+#define VFIO_DEVICE_FEATURE_TPH 14
+
+struct vfio_device_feature_tph {
+ __u32 flags;
+};
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 10/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (8 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 11/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
` (2 subsequent siblings)
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Extend the NO-ST baseline with an IV-ST policy and introduce
VFIO_DEVICE_FEATURE_TPH_PROGRAM for batch programming of architected ST
entries.
- Add VFIO_PCI_TPH_POLICY_IV_ST and extend the tph_policy module
parameter:
* IV-ST: expose IV mode and architected ST table access, hide DS
- Restrict TPH control register writes to NO-ST and IV-ST only;
block device-specific TPH mode
- VFIO TPH GET reports PROG_ST_NONE and PROG_ST_CPU capabilities after
opt-in
- Add VFIO_DEVICE_FEATURE_TPH_PROGRAM (SET-only) to batch program
architected ST entries for NONE and CPU (volatile/persistent) sources
- All ST programming is gated behind tph_opt_in and policy checks
- Register a per-device cpuhp offline callback: automatically clear ST
entries bound to an offlined CPU and set those ST values to zero
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 5 +-
drivers/vfio/pci/vfio_pci_config.c | 10 +-
drivers/vfio/pci/vfio_pci_core.c | 287 ++++++++++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_priv.h | 1 +
include/linux/vfio_pci_core.h | 3 +
include/uapi/linux/vfio.h | 74 +++++++-
6 files changed, 371 insertions(+), 9 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 77764eabb71a..664775bb3526 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -64,7 +64,7 @@ static unsigned int tph_policy;
static int tph_policy_set(const char *val, const struct kernel_param *kp)
{
return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
- VFIO_PCI_TPH_POLICY_NO_ST);
+ VFIO_PCI_TPH_POLICY_IV_ST);
}
static const struct kernel_param_ops tph_param_ops = {
.set = tph_policy_set,
@@ -73,7 +73,8 @@ static const struct kernel_param_ops tph_param_ops = {
module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
MODULE_PARM_DESC(tph_policy,
"Global TPH policy level (0=default No-ST):\n"
- "0 = No-ST mode: ST programming unavailable\n");
+ "0 = No-ST mode: ST programming unavailable\n"
+ "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n");
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 3946ed2ec0cf..398f8db8b8ff 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -1142,6 +1142,14 @@ static void vfio_tph_vcap_rewrite(struct vfio_pci_core_device *vdev, int pos)
*/
val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
+ } else if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_IV_ST) {
+ /*
+ * Add IV policy: mask DS mode, drop ST location and size
+ * if device does not support IV mode.
+ */
+ val &= ~PCI_TPH_CAP_ST_DS;
+ if (!(val & PCI_TPH_CAP_ST_IV))
+ val &= ~(PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
}
*vptr = cpu_to_le32(val);
@@ -1210,7 +1218,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
cap = vdev->tph_vcap;
mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, new_ctrl);
req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, new_ctrl);
- if (mode != PCI_TPH_ST_NS_MODE || !(cap & (1u << mode)) ||
+ if (mode > PCI_TPH_ST_IV_MODE || !(cap & (1u << mode)) ||
req == 0x2 /* reserved */ ||
(req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
goto out; /* Drop invalid or unsupported write value. */
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 05a03aa2bceb..23ed8ea39657 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -11,6 +11,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/aperture.h>
+#include <linux/cpuhotplug.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/eventfd.h>
@@ -576,6 +577,9 @@ static void vfio_pci_core_map_bars(struct vfio_pci_core_device *vdev)
}
}
+static int vfio_pci_tph_dev_init(struct vfio_pci_core_device *vdev);
+static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev);
+
int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
{
struct pci_dev *pdev = vdev->pdev;
@@ -630,7 +634,10 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_free_zdev;
- vdev->tph_opt_in = 0;
+ /* After identifying the capability, initialize TPH. */
+ ret = vfio_pci_tph_dev_init(vdev);
+ if (ret != 0)
+ goto out_free_config;
msix_pos = pdev->msix_cap;
if (msix_pos) {
@@ -656,6 +663,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
return 0;
+out_free_config:
+ vfio_config_free(vdev);
out_free_zdev:
vfio_pci_zdev_close_device(vdev);
out_free_state:
@@ -729,6 +738,8 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
kfree(vdev->region);
vdev->region = NULL; /* don't krealloc a freed pointer */
+ vfio_pci_tph_dev_deinit(vdev);
+
vfio_config_free(vdev);
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
@@ -1597,12 +1608,116 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
return 0;
}
-static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+static enum cpuhp_state vfio_tph_cpuhp_state;
+static u32 vfio_pci_tph_get_st_table_size(struct vfio_pci_core_device *vdev);
+
+static int vfio_pci_tph_cpuhp_offline(unsigned int cpu, struct hlist_node *node)
+{
+ struct vfio_pci_core_device *vdev =
+ hlist_entry(node, struct vfio_pci_core_device, tph_cpuhp_node);
+ u32 st_size = vfio_pci_tph_get_st_table_size(vdev);
+ int i;
+
+ if (vdev->tph_st_cpu == NULL)
+ return 0;
+
+ mutex_lock(&vdev->tph_lock);
+ for (i = 0; i < st_size; i++) {
+ if (vdev->tph_st_cpu[i] != (s64)cpu)
+ continue;
+ pcie_tph_set_st_entry(vdev->pdev, i, 0);
+ vdev->tph_st_cpu[i] = -1;
+ }
+ mutex_unlock(&vdev->tph_lock);
+
+ return 0;
+}
+
+static int vfio_pci_tph_cpuhp_init(void)
+{
+ int ret;
+
+ ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+ "vfio/pci/tph:online",
+ NULL,
+ vfio_pci_tph_cpuhp_offline);
+ if (ret < 0)
+ return ret;
+
+ vfio_tph_cpuhp_state = ret;
+ return 0;
+}
+
+static void vfio_pci_tph_cpuhp_deinit(void)
+{
+ cpuhp_remove_multi_state(vfio_tph_cpuhp_state);
+}
+
+static bool vfio_pci_tph_has_architected_st(struct vfio_pci_core_device *vdev)
+{
+ u32 loc = vdev->tph_vcap & PCI_TPH_CAP_LOC_MASK;
+
+ return (loc == PCI_TPH_LOC_CAP || loc == PCI_TPH_LOC_MSIX) &&
+ (vfio_pci_tph_get_st_table_size(vdev) > 0);
+}
+
+static int vfio_pci_tph_dev_init(struct vfio_pci_core_device *vdev)
{
- /* NO-ST only, no capability bits exposed now */
+ bool has_arch_st;
+ u32 st_size;
+ int i, ret;
+
+ vdev->tph_opt_in = 0;
+ vdev->tph_st_cpu = NULL;
+
+ has_arch_st = vfio_pci_tph_has_architected_st(vdev);
+ if (!has_arch_st)
+ return 0;
+
+ ret = cpuhp_state_add_instance_nocalls(vfio_tph_cpuhp_state,
+ &vdev->tph_cpuhp_node);
+ if (ret != 0)
+ return ret;
+
+ st_size = vfio_pci_tph_get_st_table_size(vdev);
+ vdev->tph_st_cpu = kcalloc(st_size, sizeof(*vdev->tph_st_cpu),
+ GFP_KERNEL);
+ if (vdev->tph_st_cpu == NULL) {
+ cpuhp_state_remove_instance_nocalls(vfio_tph_cpuhp_state,
+ &vdev->tph_cpuhp_node);
+ return -ENOMEM;
+ }
+ for (i = 0; i < st_size; i++)
+ vdev->tph_st_cpu[i] = -1;
+
return 0;
}
+static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev)
+{
+ bool has_arch_st;
+
+ has_arch_st = vfio_pci_tph_has_architected_st(vdev);
+ if (!has_arch_st)
+ return;
+
+ kfree(vdev->tph_st_cpu);
+ vdev->tph_st_cpu = NULL;
+ cpuhp_state_remove_instance_nocalls(vfio_tph_cpuhp_state,
+ &vdev->tph_cpuhp_node);
+}
+
+static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+{
+ bool has_arch_st;
+
+ has_arch_st = vfio_pci_tph_has_architected_st(vdev);
+ if (!has_arch_st)
+ return 0;
+
+ return VFIO_DEVICE_TPH_CAP_PROG_ST_NONE | VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
+}
+
static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
u32 flags,
struct vfio_device_feature_tph __user *arg,
@@ -1638,6 +1753,154 @@ static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
}
+static u32 vfio_pci_tph_get_src_mask(struct vfio_pci_core_device *vdev)
+{
+ u32 flags = vfio_pci_tph_get_capability(vdev);
+ u32 src_mask = 0;
+
+ if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_NONE)
+ src_mask |= VFIO_DEVICE_TPH_SRC_NONE;
+ if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_CPU)
+ src_mask |= VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
+ VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
+
+ return src_mask;
+}
+
+static u32 vfio_pci_tph_get_st_table_size(struct vfio_pci_core_device *vdev)
+{
+ struct pci_dev *pdev = vdev->pdev;
+ u32 loc = pcie_tph_get_st_table_loc(pdev);
+ int ret;
+
+ if (loc == PCI_TPH_LOC_CAP) {
+ return pcie_tph_get_st_table_size(pdev);
+ } else if (loc == PCI_TPH_LOC_MSIX) {
+ ret = pci_msix_vec_count(pdev);
+ if (ret < 0)
+ return 0;
+ return ret;
+ } else {
+ return 0;
+ }
+}
+
+static int vfio_pci_tph_resolve_st_tag(struct pci_dev *pdev, u32 src_bits,
+ u32 src_handle, bool extended, u16 *st)
+{
+ u8 req_type = extended ? PCI_TPH_REQ_EXT_TPH : PCI_TPH_REQ_TPH_ONLY;
+ int ret = 0;
+
+ if (src_bits & VFIO_DEVICE_TPH_SRC_NONE)
+ *st = 0;
+ else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_VOLATILE)
+ ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_VM,
+ req_type, src_handle, st);
+ else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT)
+ ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_PM,
+ req_type, src_handle, st);
+ else
+ ret = -EINVAL;
+
+ if (ret != 0)
+ *st = 0;
+
+ return ret;
+}
+
+static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
+ u32 flags,
+ struct vfio_device_feature_tph_program __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_tph_program program;
+ u32 user_flags, src_mask, st_size;
+ struct pci_dev *pdev = vdev->pdev;
+ bool extended, stop_on_zero;
+ u32 *src_handle = NULL;
+ int ret, i, k, written;
+ u16 *tags = NULL;
+ u16 cmd;
+
+ if (!vdev->tph_opt_in ||
+ !vfio_pci_tph_has_architected_st(vdev))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET,
+ sizeof(program));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&program, arg, sizeof(program)))
+ return -EFAULT;
+
+ user_flags = program.flags;
+ extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
+ if (extended && !pcie_ext_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+ user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
+
+ stop_on_zero = !!(user_flags & VFIO_DEVICE_TPH_REQUIRE_ST);
+ if (stop_on_zero && (user_flags & VFIO_DEVICE_TPH_SRC_NONE))
+ return -EINVAL;
+ user_flags &= ~VFIO_DEVICE_TPH_REQUIRE_ST;
+
+ src_mask = vfio_pci_tph_get_src_mask(vdev);
+ if (user_flags & ~src_mask || !is_power_of_2(user_flags))
+ return -EINVAL;
+
+ st_size = vfio_pci_tph_get_st_table_size(vdev);
+ if (program.start >= st_size || program.count == 0 ||
+ program.count > st_size - program.start)
+ return -EINVAL;
+
+ if (!(user_flags & VFIO_DEVICE_TPH_SRC_NONE)) {
+ src_handle = memdup_array_user(u64_to_user_ptr(program.srcs),
+ program.count, sizeof(u32));
+ if (IS_ERR(src_handle))
+ return PTR_ERR(src_handle);
+ }
+ tags = kcalloc(program.count, sizeof(u16), GFP_KERNEL_ACCOUNT);
+ if (!tags) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ cpus_read_lock();
+
+ for (i = 0; i < program.count; i++) {
+ ret = vfio_pci_tph_resolve_st_tag(pdev, user_flags,
+ src_handle ? src_handle[i] : 0,
+ extended, &tags[i]);
+ if (ret || (stop_on_zero && tags[i] == 0))
+ break;
+ }
+
+ if (i > 0) {
+ mutex_lock(&vdev->tph_lock);
+
+ cmd = vfio_pci_memory_lock_and_enable(vdev);
+ ret = written = pcie_tph_set_st_entries(pdev, program.start, i,
+ tags);
+ vfio_pci_memory_unlock_and_restore(vdev, cmd);
+
+ if (written > 0) {
+ for (k = 0; k < written; k++)
+ vdev->tph_st_cpu[program.start + k] =
+ (tags[k] == 0) ? -1 : src_handle[k];
+ }
+
+ mutex_unlock(&vdev->tph_lock);
+ }
+
+ cpus_read_unlock();
+
+out:
+ kfree(tags);
+ kfree(src_handle);
+ return ret;
+}
+
int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz)
{
@@ -1660,6 +1923,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
return vfio_pci_zdev_feature_err(device, flags, arg, argsz);
case VFIO_DEVICE_FEATURE_TPH:
return vfio_pci_core_feature_tph(vdev, flags, arg, argsz);
+ case VFIO_DEVICE_FEATURE_TPH_PROGRAM:
+ return vfio_pci_core_feature_tph_program(vdev, flags,
+ arg, argsz);
default:
return -ENOTTY;
}
@@ -2222,6 +2488,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
mutex_init(&vdev->igate);
spin_lock_init(&vdev->irqlock);
mutex_init(&vdev->ioeventfds_lock);
+ mutex_init(&vdev->tph_lock);
INIT_LIST_HEAD(&vdev->dummy_resources_list);
INIT_LIST_HEAD(&vdev->ioeventfds_list);
INIT_LIST_HEAD(&vdev->sriov_pfs_item);
@@ -2242,6 +2509,7 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev)
container_of(core_vdev, struct vfio_pci_core_device, vdev);
mutex_destroy(&vdev->igate);
+ mutex_destroy(&vdev->tph_lock);
mutex_destroy(&vdev->ioeventfds_lock);
kfree(vdev->region);
kfree(vdev->pm_save);
@@ -2697,12 +2965,23 @@ static void vfio_pci_core_cleanup(void)
{
vfio_pci_uninit_perm_bits();
rcu_barrier();
+ vfio_pci_tph_cpuhp_deinit();
}
static int __init vfio_pci_core_init(void)
{
+ int ret;
+
+ ret = vfio_pci_tph_cpuhp_init();
+ if (ret != 0)
+ return ret;
+
/* Allocate shared config space permission data used by all devices */
- return vfio_pci_init_perm_bits();
+ ret = vfio_pci_init_perm_bits();
+ if (ret != 0)
+ vfio_pci_tph_cpuhp_deinit();
+
+ return ret;
}
module_init(vfio_pci_core_init);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index f3d53af93dfd..e8c66f3d1743 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -13,6 +13,7 @@
enum vfio_pci_tph_policy {
VFIO_PCI_TPH_POLICY_NO_ST = 0,
+ VFIO_PCI_TPH_POLICY_IV_ST,
};
struct vfio_pci_ioeventfd {
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 17c4ad490dc2..217f95aaa9db 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -154,6 +154,9 @@ struct vfio_pci_core_device {
u8 tph_policy;
u8 tph_opt_in;
u32 tph_vcap;
+ s64 *tph_st_cpu;
+ struct hlist_node tph_cpuhp_node;
+ struct mutex tph_lock;
};
enum vfio_pci_io_width {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 00e23c0b21e6..237af73909bf 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1560,8 +1560,9 @@ struct vfio_device_feature_zpci_err {
*
* SET: Opt-in to enable the VFIO TPH feature for the device; flags must be
* zero.
- * GET: Return VFIO-usable TPH capability bits in flags (currently zero).
- * Returns an error if SET opt-in has not been performed.
+ * GET: Return VFIO-usable TPH capability bits in flags (see
+ * VFIO_DEVICE_TPH_CAP_*). Returns an error if SET opt-in has not been
+ * performed.
*
* Typical userspace sequence:
* 1. Read virtualized PCI TPH capability via VFIO config space access to
@@ -1570,6 +1571,8 @@ struct vfio_device_feature_zpci_err {
* 3. Invoke GET to retrieve the set of TPH capabilities accessible via VFIO.
* 4. Write the TPH control register through VFIO config space to activate
* device TPH hardware.
+ * 5. Program steering tag (ST) table entries by VFIO_DEVICE_FEATURE_TPH_PROGRAM
+ * if the device has an architected ST table.
*/
#define VFIO_DEVICE_FEATURE_TPH 14
@@ -1577,6 +1580,73 @@ struct vfio_device_feature_tph {
__u32 flags;
};
+/*
+ * Capability bits returned by VFIO_DEVICE_FEATURE_TPH GET
+ */
+
+/*
+ * Supports programming steering tag (ST) entries using the NONE source.
+ * This source resolves to a zero ST value.
+ */
+#define VFIO_DEVICE_TPH_CAP_PROG_ST_NONE (1u << 0)
+/*
+ * Supports programming steering tag (ST) entries using a CPU source handle,
+ * for either volatile or persistent memory type.
+ */
+#define VFIO_DEVICE_TPH_CAP_PROG_ST_CPU (1u << 1)
+
+/*
+ * VFIO_DEVICE_FEATURE_TPH_PROGRAM - Batch program architected steering tag
+ * (ST) table entries. This interface only operates on the architected ST
+ * table described by the PCIe TPH ST Table Location field; it does not
+ * manage device-specific (DS) ST storage or any vendor-private ST array.
+ *
+ * SET only
+ *
+ * Prerequisites before invoking this feature:
+ * 1. Call VFIO_DEVICE_FEATURE_TPH with SET to enable vfio TPH permission;
+ * 2. Write the TPH control register to activate device hardware TPH logic.
+ *
+ * @flags: IN - source type selector (see VFIO_DEVICE_TPH_SRC_*) and
+ * steering tag namespace selector (see VFIO_DEVICE_TPH_EXTENDED) and
+ * failure policy (see VFIO_DEVICE_TPH_REQUIRE_ST)
+ * Note: only one source type is permitted; combining source type bits
+ * is rejected.
+ * @start: IN - first architected ST table index
+ * @count: IN - number of contiguous entries to program
+ * @srcs: IN - user pointer to array of source u32 handles (CPU ID for CPU
+ * sources, ignored for SRC_NONE)
+ *
+ * Return value: >=0 = number of entries successfully programmed;
+ * <0 = negative errno
+ *
+ * CPU offline note: When a CPU goes offline, the kernel automatically clears
+ * all ST table entries mapped to that CPU, setting those ST values to zero.
+ */
+#define VFIO_DEVICE_FEATURE_TPH_PROGRAM 15
+
+struct vfio_device_feature_tph_program {
+ __u32 flags;
+ __u16 start;
+ __u16 count;
+ __aligned_u64 srcs;
+};
+
+/* Use extended 16-bit steering tag namespace */
+#define VFIO_DEVICE_TPH_EXTENDED (1u << 0)
+/*
+ * Stop the batch when an entry resolves to a zero steering tag.
+ * Programming halts at that entry; the returned count
+ * is the number of entries successfully programmed before the stop.
+ */
+#define VFIO_DEVICE_TPH_REQUIRE_ST (1u << 1)
+/* Source resolves to a zero steering tag (ST) */
+#define VFIO_DEVICE_TPH_SRC_NONE (1u << 2)
+/* Source holds a volatile memory CPU ID */
+#define VFIO_DEVICE_TPH_SRC_CPU_VOLATILE (1u << 3)
+/* Source holds a persistent memory CPU ID */
+#define VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT (1u << 4)
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 11/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (9 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 10/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Extend the VFIO TPH policy model beyond NO-ST and IV-ST, add DS-ST
policy and introduce VFIO_DEVICE_FEATURE_TPH_RESOLVE for steering tag (ST)
lookup.
- Add VFIO_PCI_TPH_POLICY_DS_ST, extend module parameter valid range
* DS-ST: expose TPH_RESOLVE for CPU source steering tag lookup;
ST programming supports NONE and CPU sources
Note: In this 'DS-ST mode', device-specific ST tables are not preserved
across PCI resets, userspace must re-establish ST state after
reset. The kernel cannot validate these ST entries, so this mode
is not recommended for general virtualization usecases
- Relax TPH control register write check to allow NS/IV/DS mode selection
- Implement TPH_RESOLVE:
* Support CPU volatile / persistent memory source steering tag resolution
* Return resolved steering tag (ST) when capability is advertised
- Advertise TPH_RESOLVE capability only when hardware reports DS-ST support
- Update module parameter description to document DS-ST caveat
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 11 ++++--
drivers/vfio/pci/vfio_pci_config.c | 2 +-
drivers/vfio/pci/vfio_pci_core.c | 58 ++++++++++++++++++++++++++++--
drivers/vfio/pci/vfio_pci_priv.h | 1 +
include/uapi/linux/vfio.h | 37 +++++++++++++++++++
5 files changed, 103 insertions(+), 6 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 664775bb3526..0369069e7f6a 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -64,7 +64,7 @@ static unsigned int tph_policy;
static int tph_policy_set(const char *val, const struct kernel_param *kp)
{
return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
- VFIO_PCI_TPH_POLICY_IV_ST);
+ VFIO_PCI_TPH_POLICY_DS_ST);
}
static const struct kernel_param_ops tph_param_ops = {
.set = tph_policy_set,
@@ -74,7 +74,14 @@ module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
MODULE_PARM_DESC(tph_policy,
"Global TPH policy level (0=default No-ST):\n"
"0 = No-ST mode: ST programming unavailable\n"
- "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n");
+ "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n"
+ "2 = Add DS-ST mode: RESOLVE returns ST for CPU sources;\n"
+ " ST programming supports NONE/CPU sources\n"
+ " Note: In this 'Add DS-ST mode', device-specific ST tables are\n"
+ " not preserved across PCI resets, userspace must re-establish\n"
+ " ST state after reset. The kernel cannot validate these ST\n"
+ " entries, so this mode is not recommended for general\n"
+ " virtualization usecases.");
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 398f8db8b8ff..8e47f84216b0 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -1218,7 +1218,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
cap = vdev->tph_vcap;
mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, new_ctrl);
req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, new_ctrl);
- if (mode > PCI_TPH_ST_IV_MODE || !(cap & (1u << mode)) ||
+ if (mode > PCI_TPH_ST_DS_MODE || !(cap & (1u << mode)) ||
req == 0x2 /* reserved */ ||
(req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
goto out; /* Drop invalid or unsupported write value. */
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 23ed8ea39657..049392831b35 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1710,12 +1710,17 @@ static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev)
static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
{
bool has_arch_st;
+ u32 cap = 0;
has_arch_st = vfio_pci_tph_has_architected_st(vdev);
- if (!has_arch_st)
- return 0;
+ if (has_arch_st)
+ cap |= VFIO_DEVICE_TPH_CAP_PROG_ST_NONE |
+ VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
- return VFIO_DEVICE_TPH_CAP_PROG_ST_NONE | VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
+ if (vdev->tph_vcap & PCI_TPH_CAP_ST_DS)
+ cap |= VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST;
+
+ return cap;
}
static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
@@ -1901,6 +1906,50 @@ static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
return ret;
}
+static int vfio_pci_core_feature_tph_resolve(struct vfio_pci_core_device *vdev,
+ u32 flags,
+ struct vfio_device_feature_tph_resolve __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_tph_resolve resolve;
+ u32 user_flags, src_mask;
+ bool extended;
+ u16 tag = 0;
+ int ret;
+
+ if (!vdev->tph_opt_in || !(vdev->tph_vcap & PCI_TPH_CAP_ST_DS))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
+ sizeof(resolve));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&resolve, arg, sizeof(resolve)))
+ return -EFAULT;
+
+ user_flags = resolve.flags;
+ extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
+ if (extended && !pcie_ext_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+ user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
+
+ src_mask = vfio_pci_tph_get_src_mask(vdev) & ~VFIO_DEVICE_TPH_SRC_NONE;
+ if (user_flags & ~src_mask || !is_power_of_2(user_flags))
+ return -EINVAL;
+
+ ret = vfio_pci_tph_resolve_st_tag(vdev->pdev, user_flags, resolve.src,
+ extended, &tag);
+ if (ret != 0)
+ return ret;
+
+ resolve.valid = VFIO_DEVICE_TPH_VALID_ST;
+ resolve.rsv = 0;
+ resolve.st = tag;
+
+ return copy_to_user(arg, &resolve, sizeof(resolve)) ? -EFAULT : 0;
+}
+
int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz)
{
@@ -1926,6 +1975,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
case VFIO_DEVICE_FEATURE_TPH_PROGRAM:
return vfio_pci_core_feature_tph_program(vdev, flags,
arg, argsz);
+ case VFIO_DEVICE_FEATURE_TPH_RESOLVE:
+ return vfio_pci_core_feature_tph_resolve(vdev, flags,
+ arg, argsz);
default:
return -ENOTTY;
}
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index e8c66f3d1743..967f706f509e 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -14,6 +14,7 @@
enum vfio_pci_tph_policy {
VFIO_PCI_TPH_POLICY_NO_ST = 0,
VFIO_PCI_TPH_POLICY_IV_ST,
+ VFIO_PCI_TPH_POLICY_DS_ST,
};
struct vfio_pci_ioeventfd {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 237af73909bf..1820bb20a460 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1573,6 +1573,9 @@ struct vfio_device_feature_zpci_err {
* device TPH hardware.
* 5. Program steering tag (ST) table entries by VFIO_DEVICE_FEATURE_TPH_PROGRAM
* if the device has an architected ST table.
+ * 6. Query the ST value for a given CPU ID via VFIO_DEVICE_FEATURE_TPH_RESOLVE
+ * for DS-ST capable devices, userspace may then configure the ST through
+ * device MMIO or firmware interfaces.
*/
#define VFIO_DEVICE_FEATURE_TPH 14
@@ -1594,6 +1597,10 @@ struct vfio_device_feature_tph {
* for either volatile or persistent memory type.
*/
#define VFIO_DEVICE_TPH_CAP_PROG_ST_CPU (1u << 1)
+/*
+ * Supports resolving a steering tag (ST) from a CPU source handle.
+ */
+#define VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST (1u << 2)
/*
* VFIO_DEVICE_FEATURE_TPH_PROGRAM - Batch program architected steering tag
@@ -1647,6 +1654,36 @@ struct vfio_device_feature_tph_program {
/* Source holds a persistent memory CPU ID */
#define VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT (1u << 4)
+/*
+ * VFIO_DEVICE_FEATURE_TPH_RESOLVE - Resolve TPH attributes from given source
+ *
+ * GET only
+ *
+ * Requires prior successful SET on VFIO_DEVICE_FEATURE_TPH, otherwise
+ * this feature will return error.
+ *
+ * @flags: IN - source type selector (see VFIO_DEVICE_TPH_SRC_*)
+ * and steering tag namespace selector (see VFIO_DEVICE_TPH_EXTENDED)
+ * Note: only one source type is permitted (except SRC_NONE);
+ * combining source type bits is rejected.
+ * @src: IN - source identifier, e.g. CPU ID when SRC_CPU_VOLATILE is set
+ * in flags
+ * @valid: OUT - bitmap indicating valid output fields, see
+ * VFIO_DEVICE_TPH_VALID_*
+ * @rsv: OUT - Reserved for future extension, kernel returns zero
+ * @st: OUT - TPH steering tag (valid when VALID_ST is set in valid)
+ */
+#define VFIO_DEVICE_FEATURE_TPH_RESOLVE 16
+
+struct vfio_device_feature_tph_resolve {
+ __u32 flags;
+ __u32 src;
+ __u8 valid;
+#define VFIO_DEVICE_TPH_VALID_ST (1u << 0) /* st holds valid steering tag */
+ __u8 rsv;
+ __u16 st;
+};
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (10 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 11/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Hardware resets clear PCI TPH registers, but PCI core software TPH state
may remain stale across VFIO device open/close cycles.
Invoke pcie_disable_tph() at enable and disable boundaries:
- In vfio_pci_core_enable(): reset TPH state before saving PCI state
for a fresh user session
- In vfio_pci_core_disable(): reset TPH state when releasing the device,
to clear stale software TPH tracking state
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci_core.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 049392831b35..8d3d532cd51a 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -606,6 +606,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
goto out_disable_device;
vdev->reset_works = !ret;
+ /* Reset TPH state for new user session */
+ pcie_disable_tph(vdev->pdev);
pci_save_state(pdev);
vdev->pci_saved_state = pci_store_saved_state(pdev);
if (!vdev->pci_saved_state)
@@ -762,6 +764,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
vfio_pci_zdev_close_device(vdev);
+ /* Reset TPH state on user session exit */
+ pcie_disable_tph(vdev->pdev);
+
/*
* If we have saved state, restore it. If we can reset the device,
* even better. Resetting with current state seems better than
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v21 13/13] vfio/pci: Expose per-device TPH state via debugfs
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (11 preceding siblings ...)
2026-11-18 9:12 ` [PATCH v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
@ 2026-11-18 9:12 ` Chengwen Feng
12 siblings, 0 replies; 19+ messages in thread
From: Chengwen Feng @ 2026-11-18 9:12 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add read-only debugfs file "tph" to report per-device TPH runtime state:
- effective tph_policy mode
- virtual TPH capability value
- tph_opt-in user enable state
Align debugfs comments to include tph_policy and new tph debug entry.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci_core.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 8d3d532cd51a..a9cdcb0f95a0 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -130,10 +130,20 @@ static int vfio_pci_core_debugfs_disable_idle_d3(struct seq_file *seq,
return 0;
}
+static int vfio_pci_core_debugfs_tph(struct seq_file *seq,
+ void *data)
+{
+ struct vfio_pci_core_device *vdev = vfio_pci_core_debugfs_private(seq);
+
+ seq_printf(seq, "policy=%u cap-virt=0x%x opt-in=%u\n",
+ vdev->tph_policy, vdev->tph_vcap, vdev->tph_opt_in);
+ return 0;
+}
+
/*
- * disable_idle_d3 and nointxmask are writable module parameters latched
- * per device at init, so a device's effective value can differ from the
- * current parameter setting. Expose the per-device (read-only) values
+ * disable_idle_d3, nointxmask and tph_policy are writable module parameters
+ * latched per device at init, so a device's effective value can differ from
+ * the current parameter setting. Expose the per-device (read-only) values
* here for visibility; read-only parameters can't drift and are omitted.
*/
static void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
@@ -149,6 +159,8 @@ static void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
vfio_pci_core_debugfs_nointxmask);
debugfs_create_devm_seqfile(dev, "disable_idle_d3", pci_dir,
vfio_pci_core_debugfs_disable_idle_d3);
+ debugfs_create_devm_seqfile(dev, "tph", pci_dir,
+ vfio_pci_core_debugfs_tph);
}
#else
static inline void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
--
2.17.1
^ permalink raw reply related [flat|nested] 19+ messages in thread