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 --- 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 980124dcb075..f021419251cc 100644 --- a/include/linux/pci-tph.h +++ b/include/linux/pci-tph.h @@ -29,7 +29,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 @@ -41,7 +42,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