PCI/PM: Always disable PTM for all devices during suspend
v4 cover PCI/PM: Always disable PTM for all devices during suspend
From: Bjorn Helgaas <[email protected]>
We currently disable PTM for Root Ports during suspend. Leaving PTM enabled for downstream devices causes UR errors if they send PTM Requests to upstream devices that have PTM disabled.
The intent of this series is to:
- Unconditionally disable PTM during suspend (even if the driver saves its own state) by moving the disable from pci_prepare_to_sleep() to pci_pm_suspend().
- Disable PTM for all devices by removing the Root Port condition and doing it early in the suspend paths.
- Explicitly re-enable PTM during resume.
Changes between v3 and v4:
- Use u16 for ptm_cap
- Add kernel-doc for pci_enable_ptm() and pci_disable_ptm() (exported functions)
- Drop "Preserve PTM Root Select" (unnecessary since enabling PTM sets Root Select when needed)
- Squash these three patches into one because they make more sense that way:
PCI/PTM: Add suspend/resume
PCI/PTM: Add pci_enable_ptm() wrapper
PCI/PTM: Add pci_disable_ptm() wrapper
- Add "PCI/PTM: Preserve RsvdP bits in PTM Control register"
- Add "PCI/PTM: Consolidate PTM interface declarations"
Bjorn Helgaas (9):
PCI/PTM: Cache PTM Capability offset
PCI/PTM: Add pci_upstream_ptm() helper
PCI/PTM: Separate configuration and enable PCI/PTM: Add pci_suspend_ptm() and pci_resume_ptm() PCI/PTM: Move pci_ptm_info() body into its only caller PCI/PTM: Preserve RsvdP bits in PTM Control register PCI/PTM: Reorder functions in logical order PCI/PTM: Consolidate PTM interface declarations PCI/PM: Always disable PTM for all devices during suspend
drivers/pci/pci-driver.c | 11 ++ drivers/pci/pci.c | 28 +-- drivers/pci/pci.h | 14 +- drivers/pci/pcie/ptm.c | 384 +++++++++++++++++++++------------------ include/linux/pci.h | 3 + 5 files changed, 234 insertions(+), 206 deletions(-)
Signature · 1 line
2.25.1
PATCH v4 1/9 PCI/PTM: Cache PTM Capability offset
From: Bjorn Helgaas <[email protected]>
Cache the PTM Capability offset instead of searching for it every time we enable/disable PTM or save/restore PTM state. No functional change intended.
--- drivers/pci/pcie/ptm.c | 41 +++++++++++++++++------------------------ include/linux/pci.h | 1 + 2 files changed, 18 insertions(+), 24 deletions(-)
drivers/pci/pcie/ptm.c +17 -24
@@ -31,13 +31,9 @@ static void pci_ptm_info(struct pci_dev *dev)void pci_disable_ptm (struct pci_dev *dev) { -int ptm; +u16 ptm = dev->ptm_cap;u16 ctrl; -if (!pci_is_pcie (dev)) -return ; - - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;
@@ -48,14 +44,10 @@ void pci_disable_ptm(struct pci_dev *dev)void pci_save_ptm_state (struct pci_dev *dev) { -int ptm; +u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state;u16 *cap; -if (!pci_is_pcie (dev)) -return ; - - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;
@@ -69,16 +61,15 @@ void pci_save_ptm_state(struct pci_dev *dev)void pci_restore_ptm_state (struct pci_dev *dev) { +u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -int ptm;u16 *cap; -if (!pci_is_pcie (dev)) +if (!ptm)return ; save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state || !ptm) +if (!save_state)return ; cap = (u16 *)&save_state->cap.data[0 ];
@@ -87,7 +78,7 @@ void pci_restore_ptm_state(struct pci_dev *dev)void pci_ptm_init (struct pci_dev *dev) { -int pos; +u16 ptm;u32 cap, ctrl;u8 local_clock;struct pci_dev *ups;
@@ -117,13 +108,14 @@ void pci_ptm_init(struct pci_dev *dev)return ; } - pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!pos) + ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); +if (!ptm)return ; + dev->ptm_cap = ptm;pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u16)); -pci_read_config_dword (dev, pos +PCI_PTM_CAP , &cap); +pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); local_clock = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ;/*
@@ -148,7 +140,7 @@ void pci_ptm_init(struct pci_dev *dev) } ctrl |= dev->ptm_granularity <<8 ; -pci_write_config_dword (dev, pos +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); dev->ptm_enabled =1 ;pci_ptm_info (dev);
@@ -156,18 +148,19 @@ void pci_ptm_init(struct pci_dev *dev)int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) { -int pos; +u16 ptm;u32 cap, ctrl;struct pci_dev *ups;if (!pci_is_pcie (dev))return -EINVAL ; - pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!pos) + ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); +if (!ptm)return -EINVAL ; -pci_read_config_dword (dev, pos +PCI_PTM_CAP , &cap); + dev->ptm_cap = ptm; +pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap);if (!(cap &PCI_PTM_CAP_REQ ))return -EINVAL ;
@@ -192,7 +185,7 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity) ctrl =PCI_PTM_CTRL_ENABLE ; ctrl |= dev->ptm_granularity <<8 ; -pci_write_config_dword (dev, pos +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); dev->ptm_enabled =1 ;pci_ptm_info (dev);
include/linux/pci.h +1 -0
@@ -475,6 +475,7 @@ struct pci_dev { unsigned int broken_cmd_compl:1; /* No compl for some cmds */#endif #ifdef CONFIG_PCIE_PTM +u16 ptm_cap;/* PTM Capability */ unsigned int ptm_root:1; unsigned int ptm_enabled:1;u8 ptm_granularity;
Signature · 1 line
2.25.1
Re: [PATCH v4 1/9] PCI/PTM: Cache PTM Capability offset
Hi,
Quoted 7 lines · Bjorn Helgaas
From: Bjorn Helgaas <[email protected]>
Cache the PTM Capability offset instead of searching for it every time we enable/disable PTM or save/restore PTM state. No functional change intended.
Signed-off-by Bjorn Helgaas <[email protected]>
Looks good to me.
Quoted 141 lines
--- drivers/pci/pcie/ptm.c | 41 +++++++++++++++++------------------------ include/linux/pci.h | 1 + 2 files changed, 18 insertions(+), 24 deletions(-)drivers/pci/pcie/ptm.c +17
-24@@ -31,13 +31,9 @@ static void pci_ptm_info(struct pci_dev *dev)void pci_disable_ptm (struct pci_dev *dev) { -int ptm; +u16 ptm = dev->ptm_cap;u16 ctrl; -if (!pci_is_pcie (dev)) -return ; - - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;@@ -48,14 +44,10 @@ void pci_disable_ptm(struct pci_dev *dev)void pci_save_ptm_state (struct pci_dev *dev) { -int ptm; +u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state;u16 *cap; -if (!pci_is_pcie (dev)) -return ; - - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;@@ -69,16 +61,15 @@ void pci_save_ptm_state(struct pci_dev *dev)void pci_restore_ptm_state (struct pci_dev *dev) { +u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -int ptm;u16 *cap; -if (!pci_is_pcie (dev)) +if (!ptm)return ; save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state || !ptm) +if (!save_state)return ; cap = (u16 *)&save_state->cap.data[0 ];@@ -87,7 +78,7 @@ void pci_restore_ptm_state(struct pci_dev *dev)void pci_ptm_init (struct pci_dev *dev) { -int pos; +u16 ptm;u32 cap, ctrl;u8 local_clock;struct pci_dev *ups;@@ -117,13 +108,14 @@ void pci_ptm_init(struct pci_dev *dev)return ; } - pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!pos) + ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); +if (!ptm)return ; + dev->ptm_cap = ptm;pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u16)); -pci_read_config_dword (dev, pos +PCI_PTM_CAP , &cap); +pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); local_clock = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ;/* @@ -148,7 +140,7 @@ void pci_ptm_init(struct pci_dev *dev) } ctrl |= dev->ptm_granularity <<8 ; -pci_write_config_dword (dev, pos +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); dev->ptm_enabled =1 ;pci_ptm_info (dev);@@ -156,18 +148,19 @@ void pci_ptm_init(struct pci_dev *dev)int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) { -int pos; +u16 ptm;u32 cap, ctrl;struct pci_dev *ups;if (!pci_is_pcie (dev))return -EINVAL ; - pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); -if (!pos) + ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM ); +if (!ptm)return -EINVAL ; -pci_read_config_dword (dev, pos +PCI_PTM_CAP , &cap); + dev->ptm_cap = ptm; +pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap);if (!(cap &PCI_PTM_CAP_REQ ))return -EINVAL ;@@ -192,7 +185,7 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity) ctrl =PCI_PTM_CTRL_ENABLE ; ctrl |= dev->ptm_granularity <<8 ; -pci_write_config_dword (dev, pos +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); dev->ptm_enabled =1 ;pci_ptm_info (dev);include/linux/pci.h +1
-0@@ -475,6 +475,7 @@ struct pci_dev { unsigned int broken_cmd_compl:1; /* No compl for some cmds */#endif #ifdef CONFIG_PCIE_PTM +u16 ptm_cap;/* PTM Capability */ unsigned int ptm_root:1; unsigned int ptm_enabled:1;u8 ptm_granularity;
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v4 2/9 PCI/PTM: Add pci_upstream_ptm() helper
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
PTM requires an unbroken path of PTM-supporting devices between the PTM Root and the ultimate PTM Requester, but if a Switch supports PTM, only the Upstream Port can have a PTM Capability; the Downstream Ports do not.
Previously we copied the PTM configuration from the Switch Upstream Port to the Downstream Ports so dev->ptm_enabled for any device implied that all the upstream devices support PTM.
Instead of making it look like Downstream Ports have their own PTM config, add pci_upstream_ptm(), which returns the upstream device that has a PTM Capability (either a Root Port or a Switch Upstream Port).
--- drivers/pci/pcie/ptm.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-)
drivers/pci/pcie/ptm.c +25 -14
@@ -76,6 +76,29 @@ void pci_restore_ptm_state(struct pci_dev *dev)pci_write_config_word (dev, ptm +PCI_PTM_CTRL , *cap); } +/* +* If the next upstream device supports PTM, return it; otherwise return +* NULL. PTM Messages are local, so both link partners must support it. +*/ +static struct pci_dev *pci_upstream_ptm (struct pci_dev *dev) +{ +struct pci_dev *ups =pci_upstream_bridge (dev); + +/* +* Switch Downstream Ports are not permitted to have a PTM +* capability; their PTM behavior is controlled by the Upstream +* Port (PCIe r5.0, sec 7.9.16), so if the upstream bridge is a +* Switch Downstream Port, look up one more level. +*/ +if (ups &&pci_pcie_type (ups) ==PCI_EXP_TYPE_DOWNSTREAM ) + ups =pci_upstream_bridge (ups); + +if (ups && ups->ptm_cap) +return ups; + +return NULL ; +} +void pci_ptm_init (struct pci_dev *dev) {u16 ptm;
@@ -95,19 +118,6 @@ void pci_ptm_init(struct pci_dev *dev) pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END))return ; -/* -* Switch Downstream Ports are not permitted to have a PTM -* capability; their PTM behavior is controlled by the Upstream -* Port (PCIe r5.0, sec 7.9.16). -*/ - ups =pci_upstream_bridge (dev); -if (pci_pcie_type (dev) ==PCI_EXP_TYPE_DOWNSTREAM && - ups && ups->ptm_enabled) { - dev->ptm_granularity = ups->ptm_granularity; - dev->ptm_enabled =1 ; -return ; - } - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;
@@ -124,6 +134,7 @@ void pci_ptm_init(struct pci_dev *dev)* the spec recommendation (PCIe r3.1, sec 7.32.3), select the * furthest upstream Time Source as the PTM Root. */ + ups =pci_upstream_ptm (dev);if (ups && ups->ptm_enabled) { ctrl =PCI_PTM_CTRL_ENABLE ;if (ups->ptm_granularity ==0 )
@@ -173,7 +184,7 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity)* associate the endpoint with a time source. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ENDPOINT ) { - ups =pci_upstream_bridge (dev); + ups =pci_upstream_ptm (dev);if (!ups || !ups->ptm_enabled)return -EINVAL ;
Signature · 1 line
2.25.1
Re: [PATCH v4 2/9] PCI/PTM: Add pci_upstream_ptm() helper
Quoted 10 lines · Bjorn Helgaas
From: Bjorn Helgaas <[email protected]>
PTM requires an unbroken path of PTM-supporting devices between the PTM Root and the ultimate PTM Requester, but if a Switch supports PTM, only the Upstream Port can have a PTM Capability; the Downstream Ports do not.
Previously we copied the PTM configuration from the Switch Upstream Port to the Downstream Ports so
dev->ptm_enabledfor any device implied that all the upstream devices support PTM.
Quoted 6 lines
Instead of making it look like Downstream Ports have their own PTM config, add
pci_upstream_ptm(), which returns the upstream device that has a PTM Capability (either a Root Port or a Switch Upstream Port).Signed-off-by Bjorn Helgaas <[email protected]>---
Looks good to me.
Quoted 74 lines
drivers/pci/pcie/ptm.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-)drivers/pci/pcie/ptm.c +25
-14@@ -76,6 +76,29 @@ void pci_restore_ptm_state(struct pci_dev *dev)pci_write_config_word (dev, ptm +PCI_PTM_CTRL , *cap); } +/* +* If the next upstream device supports PTM, return it; otherwise return +* NULL. PTM Messages are local, so both link partners must support it. +*/ +static struct pci_dev *pci_upstream_ptm (struct pci_dev *dev) +{ +struct pci_dev *ups =pci_upstream_bridge (dev); + +/* +* Switch Downstream Ports are not permitted to have a PTM +* capability; their PTM behavior is controlled by the Upstream +* Port (PCIe r5.0, sec 7.9.16), so if the upstream bridge is a +* Switch Downstream Port, look up one more level. +*/ +if (ups &&pci_pcie_type (ups) ==PCI_EXP_TYPE_DOWNSTREAM ) + ups =pci_upstream_bridge (ups); + +if (ups && ups->ptm_cap) +return ups; + +return NULL ; +} +void pci_ptm_init (struct pci_dev *dev) {u16 ptm;@@ -95,19 +118,6 @@ void pci_ptm_init(struct pci_dev *dev) pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END))return ; -/* -* Switch Downstream Ports are not permitted to have a PTM -* capability; their PTM behavior is controlled by the Upstream -* Port (PCIe r5.0, sec 7.9.16). -*/ - ups =pci_upstream_bridge (dev); -if (pci_pcie_type (dev) ==PCI_EXP_TYPE_DOWNSTREAM && - ups && ups->ptm_enabled) { - dev->ptm_granularity = ups->ptm_granularity; - dev->ptm_enabled =1 ; -return ; - } - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;@@ -124,6 +134,7 @@ void pci_ptm_init(struct pci_dev *dev)* the spec recommendation (PCIe r3.1, sec 7.32.3), select the * furthest upstream Time Source as the PTM Root. */ + ups =pci_upstream_ptm (dev);if (ups && ups->ptm_enabled) { ctrl =PCI_PTM_CTRL_ENABLE ;if (ups->ptm_granularity ==0 )@@ -173,7 +184,7 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity)* associate the endpoint with a time source. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ENDPOINT ) { - ups =pci_upstream_bridge (dev); + ups =pci_upstream_ptm (dev);if (!ups || !ups->ptm_enabled)return -EINVAL ;
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v4 3/9 PCI/PTM: Separate configuration and enable
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
PTM configuration and enabling were previously mixed together:pci_ptm_init() collected granularity info and enabled PTM for Root Ports and Switch Upstream Ports; pci_enable_ptm() did the same for Endpoints.
Move everything related to the PTM Capability register to pci_ptm_init() for all devices, and everything related to the PTM Control register to pci_enable_ptm().
--- drivers/pci/pcie/ptm.c | 104 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 55 deletions(-)
drivers/pci/pcie/ptm.c +49 -55
@@ -99,25 +99,19 @@ static struct pci_dev *pci_upstream_ptm(struct pci_dev *dev)return NULL ; } +/* +* Find the PTM Capability (if present) and extract the information we need +* to use it. +*/ void pci_ptm_init (struct pci_dev *dev) {u16 ptm; -u32 cap, ctrl; -u8 local_clock; +u32 cap;struct pci_dev *ups;if (!pci_is_pcie (dev))return ; -/* -* Enable PTM only on interior devices (root ports, switch ports, -* etc.) on the assumption that it causes no link traffic until an -* endpoint enables it. -*/ -if ((pci_pcie_type (dev) ==PCI_EXP_TYPE_ENDPOINT || -pci_pcie_type (dev) ==PCI_EXP_TYPE_RC_END )) -return ; - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return ;
@@ -126,76 +120,76 @@ void pci_ptm_init(struct pci_dev *dev)pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u16));pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); - local_clock = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ; + dev->ptm_granularity = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ;/* -* There's no point in enabling PTM unless it's enabled in the -* upstream device or this device can be a PTM Root itself. Per -* the spec recommendation (PCIe r3.1, sec 7.32.3), select the -* furthest upstream Time Source as the PTM Root. +* Per the spec recommendation (PCIe r6.0, sec 7.9.15.3), select the +* furthest upstream Time Source as the PTM Root. For Endpoints, +* "the Effective Granularity is the maximum Local Clock Granularity +* reported by the PTM Root and all intervening PTM Time Sources." */ ups =pci_upstream_ptm (dev); -if (ups && ups->ptm_enabled) { - ctrl =PCI_PTM_CTRL_ENABLE ; +if (ups) {if (ups->ptm_granularity ==0 ) dev->ptm_granularity =0 ; -else if (ups->ptm_granularity > local_clock) +else if (ups->ptm_granularity > dev->ptm_granularity) dev->ptm_granularity = ups->ptm_granularity; - }else { -if (cap &PCI_PTM_CAP_ROOT ) { - ctrl =PCI_PTM_CTRL_ENABLE |PCI_PTM_CTRL_ROOT ; - dev->ptm_root =1 ; - dev->ptm_granularity = local_clock; - }else -return ; + }else if (cap &PCI_PTM_CAP_ROOT ) { + dev->ptm_root =1 ; + }else if (pci_pcie_type (dev) ==PCI_EXP_TYPE_RC_END ) { + +/* +* Per sec 7.9.15.3, this should be the Local Clock +* Granularity of the associated Time Source. But it +* doesn't say how to find that Time Source. +*/ + dev->ptm_granularity =0 ; } - ctrl |= dev->ptm_granularity <<8 ; -pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); - dev->ptm_enabled =1 ; - -pci_ptm_info (dev); +if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT || +pci_pcie_type (dev) ==PCI_EXP_TYPE_UPSTREAM ) +pci_enable_ptm (dev,NULL ); } +/** +* pci_enable_ptm() - Enable Precision Time Measurement +* @dev: PCI device +* @granularity: pointer to return granularity +* +* Enable Precision Time Measurement for @dev. If successful and +* @granularity is non-NULL, return the Effective Granularity. +* +* Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or +* is not a PTM Root and lacks an upstream path of PTM-enabled devices. +*/ int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) { -u16 ptm; -u32 cap, ctrl; +u16 ptm = dev->ptm_cap;struct pci_dev *ups; +u32 ctrl; -if (!pci_is_pcie (dev)) -return -EINVAL ; - - ptm =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_PTM );if (!ptm)return -EINVAL ; - dev->ptm_cap = ptm; -pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); -if (!(cap &PCI_PTM_CAP_REQ )) -return -EINVAL ; -/* -* For a PCIe Endpoint, PTM is only useful if the endpoint can -* issue PTM requests to upstream devices that have PTM enabled. -* -* For Root Complex Integrated Endpoints, there is no upstream -* device, so there must be some implementation-specific way to -* associate the endpoint with a time source. +* A device uses local PTM Messages to request time information +* from a PTM Root that's farther upstream. Every device along the +* path must support PTM and have it enabled so it can handle the +* messages. Therefore, if this device is not a PTM Root, the +* upstream link partner must have PTM enabled before we can enable +* PTM. */ -if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ENDPOINT ) { +if (!dev->ptm_root) { ups =pci_upstream_ptm (dev);if (!ups || !ups->ptm_enabled)return -EINVAL ; - - dev->ptm_granularity = ups->ptm_granularity; - }else if (pci_pcie_type (dev) ==PCI_EXP_TYPE_RC_END ) { - dev->ptm_granularity =0 ; - }else -return -EINVAL ; + } ctrl =PCI_PTM_CTRL_ENABLE ; ctrl |= dev->ptm_granularity <<8 ; +if (dev->ptm_root) + ctrl |=PCI_PTM_CTRL_ROOT ; +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); dev->ptm_enabled =1 ;
Signature · 1 line
2.25.1
PATCH v4 4/9 PCI/PTM: Add pci_suspend_ptm() and pci_resume_ptm()
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
We disable PTM during suspend because that allows some Root Ports to enter lower-power PM states, which means we also need to disable PTM for all downstream devices. Add pci_suspend_ptm() and pci_resume_ptm() for this purpose.
pci_enable_ptm() and pci_disable_ptm() are for drivers to use to enable or disable PTM. They use dev->ptm_enabled to keep track of whether PTM should be enabled.
pci_suspend_ptm() and pci_resume_ptm() are PCI core-internal functions to temporarily disable PTM during suspend and (depending on dev->ptm_enabled) re-enable PTM during resume.
Enable/disable/suspend/resume all use internal __pci_enable_ptm() and __pci_disable_ptm() functions that only update the PTM Control register. Outline:
pci_enable_ptm(struct pci_dev *dev)
{
__pci_enable_ptm(dev);
dev->ptm_enabled = 1;
pci_ptm_info(dev);
}
pci_disable_ptm(struct pci_dev *dev)
{
if (dev->ptm_enabled) {
__pci_disable_ptm(dev);
dev->ptm_enabled = 0;
}
}
pci_suspend_ptm(struct pci_dev *dev)
{
if (dev->ptm_enabled)
__pci_disable_ptm(dev);
}
pci_resume_ptm(struct pci_dev *dev)
{
if (dev->ptm_enabled)
__pci_enable_ptm(dev);
}Nothing currently calls pci_resume_ptm(); the suspend path saves the PTM state before disabling PTM, so the PTM state restore in the resume path implicitly re-enables it. A future change will use pci_resume_ptm() to fix some problems with this approach.
--- drivers/pci/pci.c | 4 +-- drivers/pci/pci.h | 6 ++-- drivers/pci/pcie/ptm.c | 71 +++++++++++++++++++++++++++++++++--------- include/linux/pci.h | 2 ++ 4 files changed, 65 insertions(+), 18 deletions(-)
drivers/pci/pci.c +2 -2
@@ -2714,7 +2714,7 @@ int pci_prepare_to_sleep(struct pci_dev *dev)* lower-power idle state as a whole. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_disable_ptm (dev); +pci_suspend_ptm (dev);pci_enable_wake (dev, target_state, wakeup);
@@ -2772,7 +2772,7 @@ int pci_finish_runtime_suspend(struct pci_dev *dev)* lower-power idle state as a whole. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_disable_ptm (dev); +pci_suspend_ptm (dev);__pci_enable_wake (dev, target_state,pci_dev_run_wake (dev));
drivers/pci/pci.h +4 -2
@@ -507,11 +507,13 @@ static inline int pci_iov_bus_range(struct pci_bus *bus)#ifdef CONFIG_PCIE_PTM void pci_save_ptm_state (struct pci_dev *dev);void pci_restore_ptm_state (struct pci_dev *dev); -void pci_disable_ptm (struct pci_dev *dev); +void pci_suspend_ptm (struct pci_dev *dev); +void pci_resume_ptm (struct pci_dev *dev);#else static inline void pci_save_ptm_state (struct pci_dev *dev) { }static inline void pci_restore_ptm_state (struct pci_dev *dev) { } -static inline void pci_disable_ptm (struct pci_dev *dev) { } +static inline void pci_suspend_ptm (struct pci_dev *dev) { } +static inline void pci_resume_ptm (struct pci_dev *dev) { }#endif unsigned long pci_cardbus_resource_alignment (struct resource *);
drivers/pci/pcie/ptm.c +57 -14
102 lines not on this page · raw message
include/linux/pci.h +2 -0
@@ -1678,10 +1678,12 @@ bool pci_ats_disabled(void);#ifdef CONFIG_PCIE_PTM int pci_enable_ptm (struct pci_dev *dev,u8 *granularity); +void pci_disable_ptm (struct pci_dev *dev);bool pcie_ptm_enabled (struct pci_dev *dev);#else static inline int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) {return -EINVAL ; } +static inline void pci_disable_ptm (struct pci_dev *dev) { }static inline bool pcie_ptm_enabled (struct pci_dev *dev) {return false; }#endif
Signature · 1 line
2.25.1
Re: [PATCH v4 4/9] PCI/PTM: Add pci_suspend_ptm() and pci_resume_ptm()
Quoted 46 lines · Bjorn Helgaas
From: Bjorn Helgaas <[email protected]>
We disable PTM during suspend because that allows some Root Ports to enter lower-power PM states, which means we also need to disable PTM for all downstream devices. Add
pci_suspend_ptm()andpci_resume_ptm()for this purpose.
pci_enable_ptm()andpci_disable_ptm()are for drivers to use to enable or disable PTM. They usedev->ptm_enabledto keep track of whether PTM should be enabled.
pci_suspend_ptm()andpci_resume_ptm()are PCI core-internal functions to temporarily disable PTM during suspend and (depending ondev->ptm_enabled) re-enable PTM during resume.Enable/disable/suspend/resume all use internal
__pci_enable_ptm()and__pci_disable_ptm()functions that only update the PTM Control register. Outline:pci_enable_ptm(struct pci_dev *dev) { __pci_enable_ptm(dev); dev->ptm_enabled = 1; pci_ptm_info(dev); } pci_disable_ptm(struct pci_dev *dev) { if (dev->ptm_enabled) { __pci_disable_ptm(dev); dev->ptm_enabled = 0; } } pci_suspend_ptm(struct pci_dev *dev) { if (dev->ptm_enabled) __pci_disable_ptm(dev); } pci_resume_ptm(struct pci_dev *dev) { if (dev->ptm_enabled) __pci_enable_ptm(dev); }
Quoted 1 line
Nothing currently calls
pci_resume_ptm(); the suspend path saves the PTM
Is semicolon intentional ?
Quoted 182 lines
state before disabling PTM, so the PTM state restore in the resume path implicitly re-enables it. A future change will use
pci_resume_ptm()to fix some problems with this approach.Signed-off-by Bjorn Helgaas <[email protected]>--- drivers/pci/pci.c | 4 +-- drivers/pci/pci.h | 6 ++-- drivers/pci/pcie/ptm.c | 71 +++++++++++++++++++++++++++++++++--------- include/linux/pci.h | 2 ++ 4 files changed, 65 insertions(+), 18 deletions(-)drivers/pci/pci.c +2
-2@@ -2714,7 +2714,7 @@ int pci_prepare_to_sleep(struct pci_dev *dev)* lower-power idle state as a whole. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_disable_ptm (dev); +pci_suspend_ptm (dev);pci_enable_wake (dev, target_state, wakeup);@@ -2772,7 +2772,7 @@ int pci_finish_runtime_suspend(struct pci_dev *dev)* lower-power idle state as a whole. */ if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_disable_ptm (dev); +pci_suspend_ptm (dev);__pci_enable_wake (dev, target_state,pci_dev_run_wake (dev));drivers/pci/pci.h +4
-2@@ -507,11 +507,13 @@ static inline int pci_iov_bus_range(struct pci_bus *bus)#ifdef CONFIG_PCIE_PTM void pci_save_ptm_state (struct pci_dev *dev);void pci_restore_ptm_state (struct pci_dev *dev); -void pci_disable_ptm (struct pci_dev *dev); +void pci_suspend_ptm (struct pci_dev *dev); +void pci_resume_ptm (struct pci_dev *dev);#else static inline void pci_save_ptm_state (struct pci_dev *dev) { }static inline void pci_restore_ptm_state (struct pci_dev *dev) { } -static inline void pci_disable_ptm (struct pci_dev *dev) { } +static inline void pci_suspend_ptm (struct pci_dev *dev) { } +static inline void pci_resume_ptm (struct pci_dev *dev) { }#endif unsigned long pci_cardbus_resource_alignment (struct resource *);drivers/pci/pcie/ptm.c +57
-14@@ -29,7 +29,7 @@ static void pci_ptm_info(struct pci_dev *dev) dev->ptm_root ? " (root)" : "", clock_desc); } -void pci_disable_ptm (struct pci_dev *dev) +static void __pci_disable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;u16 ctrl;@@ -42,6 +42,21 @@ void pci_disable_ptm(struct pci_dev *dev)pci_write_config_word (dev, ptm +PCI_PTM_CTRL , ctrl); } +/** +* pci_disable_ptm() - Disable Precision Time Measurement +* @dev: PCI device +* +* Disable Precision Time Measurement for @dev. +*/ +void pci_disable_ptm (struct pci_dev *dev) +{ +if (dev->ptm_enabled) { +__pci_disable_ptm (dev); + dev->ptm_enabled =0 ; + } +} +EXPORT_SYMBOL (pci_disable_ptm); +void pci_save_ptm_state (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;@@ -151,18 +166,8 @@ void pci_ptm_init(struct pci_dev *dev)pci_enable_ptm (dev,NULL ); } -/** -* pci_enable_ptm() - Enable Precision Time Measurement -* @dev: PCI device -* @granularity: pointer to return granularity -* -* Enable Precision Time Measurement for @dev. If successful and -* @granularity is non-NULL, return the Effective Granularity. -* -* Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or -* is not a PTM Root and lacks an upstream path of PTM-enabled devices. -*/ -int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) +/* Enable PTM in the Control register if possible */ +static int __pci_enable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;struct pci_dev *ups;@@ -191,8 +196,29 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity) ctrl |=PCI_PTM_CTRL_ROOT ;pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); +return 0 ; +} + +/** +* pci_enable_ptm() - Enable Precision Time Measurement +* @dev: PCI device +* @granularity: pointer to return granularity +* +* Enable Precision Time Measurement for @dev. If successful and +* @granularity is non-NULL, return the Effective Granularity. +* +* Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or +* is not a PTM Root and lacks an upstream path of PTM-enabled devices. +*/ +int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) +{ +int rc; + + rc =__pci_enable_ptm (dev); +if (rc) +return rc; + dev->ptm_enabled =1 ; -pci_ptm_info (dev);if (granularity)@@ -201,6 +227,23 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity) }EXPORT_SYMBOL (pci_enable_ptm); +/* +* Disable PTM, but preserve dev->ptm_enabled so we silently re-enable it on +* resume if necessary. +*/ +void pci_suspend_ptm (struct pci_dev *dev) +{ +if (dev->ptm_enabled) +__pci_disable_ptm (dev); +} + +/* If PTM was enabled before suspend, re-enable it when resuming */ +void pci_resume_ptm (struct pci_dev *dev) +{ +if (dev->ptm_enabled) +__pci_enable_ptm (dev); +} +bool pcie_ptm_enabled (struct pci_dev *dev) {if (!dev)include/linux/pci.h +2
-0@@ -1678,10 +1678,12 @@ bool pci_ats_disabled(void);#ifdef CONFIG_PCIE_PTM int pci_enable_ptm (struct pci_dev *dev,u8 *granularity); +void pci_disable_ptm (struct pci_dev *dev);bool pcie_ptm_enabled (struct pci_dev *dev);#else static inline int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) {return -EINVAL ; } +static inline void pci_disable_ptm (struct pci_dev *dev) { }static inline bool pcie_ptm_enabled (struct pci_dev *dev) {return false; }#endif
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v4 5/9 PCI/PTM: Move pci_ptm_info() body into its only caller
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
pci_ptm_info() is simple and is only called by pci_enable_ptm(). Move the entire body there. No functional change intended.
--- drivers/pci/pcie/ptm.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-)
drivers/pci/pcie/ptm.c +17 -21
@@ -9,26 +9,6 @@#include <linux/pci.h> #include "../pci.h" -static void pci_ptm_info (struct pci_dev *dev) -{ -char clock_desc[8 ]; - -switch (dev->ptm_granularity) { -case 0 : -snprintf (clock_desc,sizeof (clock_desc),"unknown" ); -break ; -case 255 : -snprintf (clock_desc,sizeof (clock_desc),">254ns" ); -break ; -default : -snprintf (clock_desc,sizeof (clock_desc),"%uns" , - dev->ptm_granularity); -break ; - } -pci_info (dev,"PTM enabled%s, %s granularity\n" , - dev->ptm_root ?" (root)" :"" , clock_desc); -} -static void __pci_disable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;
@@ -213,16 +193,32 @@ static int __pci_enable_ptm(struct pci_dev *dev)int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) {int rc; +char clock_desc[8 ]; rc =__pci_enable_ptm (dev);if (rc)return rc; dev->ptm_enabled =1 ; -pci_ptm_info (dev);if (granularity) *granularity = dev->ptm_granularity; + +switch (dev->ptm_granularity) { +case 0 : +snprintf (clock_desc,sizeof (clock_desc),"unknown" ); +break ; +case 255 : +snprintf (clock_desc,sizeof (clock_desc),">254ns" ); +break ; +default : +snprintf (clock_desc,sizeof (clock_desc),"%uns" , + dev->ptm_granularity); +break ; + } +pci_info (dev,"PTM enabled%s, %s granularity\n" , + dev->ptm_root ?" (root)" :"" , clock_desc); +return 0 ; }EXPORT_SYMBOL (pci_enable_ptm);
Signature · 1 line
2.25.1
Re: [PATCH v4 5/9] PCI/PTM: Move pci_ptm_info() body into its only caller
Quoted 7 lines · Bjorn Helgaas
From: Bjorn Helgaas <[email protected]>
pci_ptm_info()is simple and is only called bypci_enable_ptm(). Move the entire body there. No functional change intended.Signed-off-by Bjorn Helgaas <[email protected]>---
Looks good to me.
Quoted 68 lines
drivers/pci/pcie/ptm.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-)drivers/pci/pcie/ptm.c +17
-21@@ -9,26 +9,6 @@#include <linux/pci.h> #include "../pci.h" -static void pci_ptm_info (struct pci_dev *dev) -{ -char clock_desc[8 ]; - -switch (dev->ptm_granularity) { -case 0 : -snprintf (clock_desc,sizeof (clock_desc),"unknown" ); -break ; -case 255 : -snprintf (clock_desc,sizeof (clock_desc),">254ns" ); -break ; -default : -snprintf (clock_desc,sizeof (clock_desc),"%uns" , - dev->ptm_granularity); -break ; - } -pci_info (dev,"PTM enabled%s, %s granularity\n" , - dev->ptm_root ?" (root)" :"" , clock_desc); -} -static void __pci_disable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;@@ -213,16 +193,32 @@ static int __pci_enable_ptm(struct pci_dev *dev)int pci_enable_ptm (struct pci_dev *dev,u8 *granularity) {int rc; +char clock_desc[8 ]; rc =__pci_enable_ptm (dev);if (rc)return rc; dev->ptm_enabled =1 ; -pci_ptm_info (dev);if (granularity) *granularity = dev->ptm_granularity; + +switch (dev->ptm_granularity) { +case 0 : +snprintf (clock_desc,sizeof (clock_desc),"unknown" ); +break ; +case 255 : +snprintf (clock_desc,sizeof (clock_desc),">254ns" ); +break ; +default : +snprintf (clock_desc,sizeof (clock_desc),"%uns" , + dev->ptm_granularity); +break ; + } +pci_info (dev,"PTM enabled%s, %s granularity\n" , + dev->ptm_root ?" (root)" :"" , clock_desc); +return 0 ; }EXPORT_SYMBOL (pci_enable_ptm);
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v4 6/9 PCI/PTM: Preserve RsvdP bits in PTM Control register
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
Even though only the low 16 bits of PTM Control are currently defined, the register is 32 bits wide and the unused bits are RsvdP ("Reserved and Preserved"), so software must preserve the values of those bits when writing the register.
Update PTM Control reads and writes to use 32-bit accesses and preserve the reserved bits on writes.
--- drivers/pci/pcie/ptm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/pci/pcie/ptm.c +14 -11
@@ -12,14 +12,14 @@static void __pci_disable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap; -u16 ctrl; +u32 ctrl;if (!ptm)return ; -pci_read_config_word (dev, ptm +PCI_PTM_CTRL , &ctrl); +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); ctrl &= ~(PCI_PTM_CTRL_ENABLE |PCI_PTM_CTRL_ROOT ); -pci_write_config_word (dev, ptm +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); }/**
@@ -41,7 +41,7 @@ void pci_save_ptm_state(struct pci_dev *dev) {u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -u16 *cap; +u32 *cap;if (!ptm)return ;
@@ -50,15 +50,15 @@ void pci_save_ptm_state(struct pci_dev *dev)if (!save_state)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_read_config_word (dev, ptm +PCI_PTM_CTRL , cap); + cap = (u32 *)&save_state->cap.data[0 ]; +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , cap); }void pci_restore_ptm_state (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -u16 *cap; +u32 *cap;if (!ptm)return ;
@@ -67,8 +67,8 @@ void pci_restore_ptm_state(struct pci_dev *dev)if (!save_state)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_write_config_word (dev, ptm +PCI_PTM_CTRL , *cap); + cap = (u32 *)&save_state->cap.data[0 ]; +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , *cap); }/*
@@ -112,7 +112,7 @@ void pci_ptm_init(struct pci_dev *dev)return ; dev->ptm_cap = ptm; -pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u16)); +pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u32));pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); dev->ptm_granularity = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ;
@@ -170,7 +170,10 @@ static int __pci_enable_ptm(struct pci_dev *dev)return -EINVAL ; } - ctrl =PCI_PTM_CTRL_ENABLE ; +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); + + ctrl |=PCI_PTM_CTRL_ENABLE ; + ctrl &= ~PCI_PTM_GRANULARITY_MASK ; ctrl |= dev->ptm_granularity <<8 ;if (dev->ptm_root) ctrl |=PCI_PTM_CTRL_ROOT ;
Signature · 1 line
2.25.1
Re: [PATCH v4 6/9] PCI/PTM: Preserve RsvdP bits in PTM Control register
Quoted 7 lines · Bjorn Helgaas
From: Bjorn Helgaas <[email protected]>
Even though only the low 16 bits of PTM Control are currently defined, the register is 32 bits wide and the unused bits are RsvdP ("Reserved and Preserved"), so software must preserve the values of those bits when writing the register.
Quoted 5 lines
Update PTM Control reads and writes to use 32-bit accesses and preserve the reserved bits on writes.
Signed-off-by Bjorn Helgaas <[email protected]>---
Looks good to me.
Quoted 85 lines
drivers/pci/pcie/ptm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)drivers/pci/pcie/ptm.c +14
-11@@ -12,14 +12,14 @@static void __pci_disable_ptm (struct pci_dev *dev) {u16 ptm = dev->ptm_cap; -u16 ctrl; +u32 ctrl;if (!ptm)return ; -pci_read_config_word (dev, ptm +PCI_PTM_CTRL , &ctrl); +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); ctrl &= ~(PCI_PTM_CTRL_ENABLE |PCI_PTM_CTRL_ROOT ); -pci_write_config_word (dev, ptm +PCI_PTM_CTRL , ctrl); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); }/** @@ -41,7 +41,7 @@ void pci_save_ptm_state(struct pci_dev *dev) {u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -u16 *cap; +u32 *cap;if (!ptm)return ;@@ -50,15 +50,15 @@ void pci_save_ptm_state(struct pci_dev *dev)if (!save_state)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_read_config_word (dev, ptm +PCI_PTM_CTRL , cap); + cap = (u32 *)&save_state->cap.data[0 ]; +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , cap); }void pci_restore_ptm_state (struct pci_dev *dev) {u16 ptm = dev->ptm_cap;struct pci_cap_saved_state *save_state; -u16 *cap; +u32 *cap;if (!ptm)return ;@@ -67,8 +67,8 @@ void pci_restore_ptm_state(struct pci_dev *dev)if (!save_state)return ; - cap = (u16 *)&save_state->cap.data[0 ]; -pci_write_config_word (dev, ptm +PCI_PTM_CTRL , *cap); + cap = (u32 *)&save_state->cap.data[0 ]; +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , *cap); }/* @@ -112,7 +112,7 @@ void pci_ptm_init(struct pci_dev *dev)return ; dev->ptm_cap = ptm; -pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u16)); +pci_add_ext_cap_save_buffer (dev,PCI_EXT_CAP_ID_PTM ,sizeof (u32));pci_read_config_dword (dev, ptm +PCI_PTM_CAP , &cap); dev->ptm_granularity = (cap &PCI_PTM_GRANULARITY_MASK ) >>8 ;@@ -170,7 +170,10 @@ static int __pci_enable_ptm(struct pci_dev *dev)return -EINVAL ; } - ctrl =PCI_PTM_CTRL_ENABLE ; +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); + + ctrl |=PCI_PTM_CTRL_ENABLE ; + ctrl &= ~PCI_PTM_GRANULARITY_MASK ; ctrl |= dev->ptm_granularity <<8 ;if (dev->ptm_root) ctrl |=PCI_PTM_CTRL_ROOT ;
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v4 7/9 PCI/PTM: Reorder functions in logical order
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
pci_enable_ptm() and pci_disable_ptm() were separated. pci_save_ptm_state() and pci_restore_ptm_state() dangled at the top. Move them to logical places. No functional change intended.
--- drivers/pci/pcie/ptm.c | 124 ++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 62 deletions(-)
drivers/pci/pcie/ptm.c +62 -62
@@ -9,68 +9,6 @@#include <linux/pci.h> #include "../pci.h" -static void __pci_disable_ptm (struct pci_dev *dev) -{ -u16 ptm = dev->ptm_cap; -u32 ctrl; - -if (!ptm) -return ; - -pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); - ctrl &= ~(PCI_PTM_CTRL_ENABLE |PCI_PTM_CTRL_ROOT ); -pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); -} - -/** -* pci_disable_ptm() - Disable Precision Time Measurement -* @dev: PCI device -* -* Disable Precision Time Measurement for @dev. -*/ -void pci_disable_ptm (struct pci_dev *dev) -{ -if (dev->ptm_enabled) { -__pci_disable_ptm (dev); - dev->ptm_enabled =0 ; - } -} -EXPORT_SYMBOL (pci_disable_ptm); - -void pci_save_ptm_state (struct pci_dev *dev) -{ -u16 ptm = dev->ptm_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; - -if (!ptm) -return ; - - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state) -return ; - - cap = (u32 *)&save_state->cap.data[0 ]; -pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , cap); -} - -void pci_restore_ptm_state (struct pci_dev *dev) -{ -u16 ptm = dev->ptm_cap; -struct pci_cap_saved_state *save_state; -u32 *cap; - -if (!ptm) -return ; - - save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); -if (!save_state) -return ; - - cap = (u32 *)&save_state->cap.data[0 ]; -pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , *cap); -} -/* * If the next upstream device supports PTM, return it; otherwise return * NULL. PTM Messages are local, so both link partners must support it.
@@ -146,6 +84,40 @@ void pci_ptm_init(struct pci_dev *dev)pci_enable_ptm (dev,NULL ); } +void pci_save_ptm_state (struct pci_dev *dev) +{ +u16 ptm = dev->ptm_cap; +struct pci_cap_saved_state *save_state; +u32 *cap; + +if (!ptm) +return ; + + save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); +if (!save_state) +return ; + + cap = (u32 *)&save_state->cap.data[0 ]; +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , cap); +} + +void pci_restore_ptm_state (struct pci_dev *dev) +{ +u16 ptm = dev->ptm_cap; +struct pci_cap_saved_state *save_state; +u32 *cap; + +if (!ptm) +return ; + + save_state =pci_find_saved_ext_cap (dev,PCI_EXT_CAP_ID_PTM ); +if (!save_state) +return ; + + cap = (u32 *)&save_state->cap.data[0 ]; +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , *cap); +} +/* Enable PTM in the Control register if possible */ static int __pci_enable_ptm (struct pci_dev *dev) {
@@ -226,6 +198,34 @@ int pci_enable_ptm(struct pci_dev *dev, u8 *granularity) }EXPORT_SYMBOL (pci_enable_ptm); +static void __pci_disable_ptm (struct pci_dev *dev) +{ +u16 ptm = dev->ptm_cap; +u32 ctrl; + +if (!ptm) +return ; + +pci_read_config_dword (dev, ptm +PCI_PTM_CTRL , &ctrl); + ctrl &= ~(PCI_PTM_CTRL_ENABLE |PCI_PTM_CTRL_ROOT ); +pci_write_config_dword (dev, ptm +PCI_PTM_CTRL , ctrl); +} + +/** +* pci_disable_ptm() - Disable Precision Time Measurement +* @dev: PCI device +* +* Disable Precision Time Measurement for @dev. +*/ +void pci_disable_ptm (struct pci_dev *dev) +{ +if (dev->ptm_enabled) { +__pci_disable_ptm (dev); + dev->ptm_enabled =0 ; + } +} +EXPORT_SYMBOL (pci_disable_ptm); +/* * Disable PTM, but preserve dev->ptm_enabled so we silently re-enable it on * resume if necessary.
Signature · 1 line
2.25.1
PATCH v4 8/9 PCI/PTM: Consolidate PTM interface declarations
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
Consolidate all the PTM-related declarations in drivers/pci/pci.h. No functional change intended.
--- drivers/pci/pci.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/pci/pci.h +2 -6
@@ -505,11 +505,13 @@ static inline int pci_iov_bus_range(struct pci_bus *bus)#endif /* CONFIG_PCI_IOV */ #ifdef CONFIG_PCIE_PTM +void pci_ptm_init (struct pci_dev *dev);void pci_save_ptm_state (struct pci_dev *dev);void pci_restore_ptm_state (struct pci_dev *dev);void pci_suspend_ptm (struct pci_dev *dev);void pci_resume_ptm (struct pci_dev *dev);#else +static inline void pci_ptm_init (struct pci_dev *dev) { }static inline void pci_save_ptm_state (struct pci_dev *dev) { }static inline void pci_restore_ptm_state (struct pci_dev *dev) { }static inline void pci_suspend_ptm (struct pci_dev *dev) { }
@@ -577,12 +579,6 @@ static inline void pcie_set_ecrc_checking(struct pci_dev *dev) { }static inline void pcie_ecrc_get_policy (char *str) { }#endif -#ifdef CONFIG_PCIE_PTM -void pci_ptm_init (struct pci_dev *dev); -#else -static inline void pci_ptm_init (struct pci_dev *dev) { } -#endif -struct pci_dev_reset_methods {u16 vendor;u16 device;
Signature · 1 line
2.25.1
PATCH v4 9/9 PCI/PM: Always disable PTM for all devices during suspend
In reply to Bjorn HelgaasFrom: Bjorn Helgaas <[email protected]>
We want to disable PTM on Root Ports because that allows some chips, e.g., Intel mobile chips since Coffee Lake, to enter a lower-power PM state.
That means we also have to disable PTM on downstream devices. PCIe r6.0, sec 2.2.8, recommends that functions support generation of messages in non-D0 states, so we have to assume Switch Upstream Ports or Endpoints may send PTM Requests while in D1, D2, and D3hot. A PTM message received by a Downstream Port (including a Root Port) with PTM disabled must be treated as an Unsupported Request (sec 6.21.3).
PTM was previously disabled only for Root Ports, and it was disabled in pci_prepare_to_sleep(), which is not called at all if a driver supports legacy PM or does its own state saving.
Instead, disable PTM early in pci_pm_suspend() and pci_pm_runtime_suspend() so we do it in all cases.
Previously PTM was disabled *after* saving device state, so the state restore on resume automatically re-enabled it. Since we now disable PTM *before* saving state, we must explicitly re-enable it in pci_pm_resume() and pci_pm_runtime_resume().
Here's a sample of errors that occur when PTM is disabled only on the Root Port. With this topology:
0000:00:1d.0 Root Port to [bus 08-71]
0000:08:00.0 Switch Upstream Port to [bus 09-71]
Kai-Heng reported errors like this:
pcieport 0000:00:1d.0: [20] UnsupReq (First)
pcieport 0000:00:1d.0: AER: TLP Header: 34000000 08000052 00000000 00000000
Decoding TLP header 0x34...... (0011 0100b) and 0x08000052:
Fmt 001b 4 DW header, no data Type 1 0100b Msg (Local - Terminate at Receiver) Requester ID 0x0800 Bus 08 Devfn 00.0 Message Code 0x52 0101 0010b PTM Request
The 00:1d.0 Root Port logged an Unsupported Request error when it received a PTM Request with Requester ID 08:00.0.
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215453
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216210
--- drivers/pci/pci-driver.c | 11 +++++++++++ drivers/pci/pci.c | 28 ++-------------------------- 2 files changed, 13 insertions(+), 26 deletions(-)
drivers/pci/pci-driver.c +11 -0
@@ -774,6 +774,12 @@ static int pci_pm_suspend(struct device *dev) pci_dev->skip_bus_pm = false; +/* +* Disabling PTM allows some systems, e.g., Intel mobile chips +* since Coffee Lake, to enter a lower-power PM state. +*/ +pci_suspend_ptm (pci_dev); +if (pci_has_legacy_pm_support (pci_dev))return pci_legacy_suspend (dev,PMSG_SUSPEND );
@@ -982,6 +988,8 @@ static int pci_pm_resume(struct device *dev)if (pci_dev->state_saved)pci_restore_standard_config (pci_dev); +pci_resume_ptm (pci_dev); +if (pci_has_legacy_pm_support (pci_dev))return pci_legacy_resume (dev);
@@ -1269,6 +1277,8 @@ static int pci_pm_runtime_suspend(struct device *dev)pci_power_t prev = pci_dev->current_state;int error; +pci_suspend_ptm (pci_dev); +/* * If pci_dev->driver is not set (unbound), we leave the device in D0, * but it may go to D3cold when the bridge above it runtime suspends.
@@ -1330,6 +1340,7 @@ static int pci_pm_runtime_resume(struct device *dev)* D3cold when the bridge above it runtime suspended. */ pci_pm_default_resume_early (pci_dev); +pci_resume_ptm (pci_dev);if (!pci_dev->driver)return 0 ;
drivers/pci/pci.c +2 -26
@@ -2706,24 +2706,12 @@ int pci_prepare_to_sleep(struct pci_dev *dev)if (target_state ==PCI_POWER_ERROR )return -EIO ; -/* -* There are systems (for example, Intel mobile chips since Coffee -* Lake) where the power drawn while suspended can be significantly -* reduced by disabling PTM on PCIe root ports as this allows the -* port to enter a lower-power PM state and the SoC to reach a -* lower-power idle state as a whole. -*/ -if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_suspend_ptm (dev); -pci_enable_wake (dev, target_state, wakeup); error =pci_set_power_state (dev, target_state); -if (error) { +if (error)pci_enable_wake (dev, target_state, false); -pci_restore_ptm_state (dev); - }return error; }
@@ -2764,24 +2752,12 @@ int pci_finish_runtime_suspend(struct pci_dev *dev)if (target_state ==PCI_POWER_ERROR )return -EIO ; -/* -* There are systems (for example, Intel mobile chips since Coffee -* Lake) where the power drawn while suspended can be significantly -* reduced by disabling PTM on PCIe root ports as this allows the -* port to enter a lower-power PM state and the SoC to reach a -* lower-power idle state as a whole. -*/ -if (pci_pcie_type (dev) ==PCI_EXP_TYPE_ROOT_PORT ) -pci_suspend_ptm (dev); -__pci_enable_wake (dev, target_state,pci_dev_run_wake (dev)); error =pci_set_power_state (dev, target_state); -if (error) { +if (error)pci_enable_wake (dev, target_state, false); -pci_restore_ptm_state (dev); - }return error; }
Signature · 1 line
2.25.1
Quoted 10 lines · Bjorn Helgaas
Bjorn Helgaas (9):
PCI/PTM: Cache PTM Capability offset
PCI/PTM: Addpci_upstream_ptm()helper
PCI/PTM: Separate configuration and enable PCI/PTM: Addpci_suspend_ptm()andpci_resume_ptm()PCI/PTM: Movepci_ptm_info()body into its only caller PCI/PTM: Preserve RsvdP bits in PTM Control register PCI/PTM: Reorder functions in logical order PCI/PTM: Consolidate PTM interface declarations PCI/PM: Always disable PTM for all devices during suspend
For the whole series,
Quoted 12 lines · Mika Westerberg
Quoted 10 lines · Bjorn Helgaas
Bjorn Helgaas (9):
PCI/PTM: Cache PTM Capability offset
PCI/PTM: Addpci_upstream_ptm()helper
PCI/PTM: Separate configuration and enable PCI/PTM: Addpci_suspend_ptm()andpci_resume_ptm()PCI/PTM: Movepci_ptm_info()body into its only caller PCI/PTM: Preserve RsvdP bits in PTM Control register PCI/PTM: Reorder functions in logical order PCI/PTM: Consolidate PTM interface declarations PCI/PM: Always disable PTM for all devices during suspend
Quoted 3 lines
For the whole series,
Reviewed-by Mika Westerberg <[email protected]>
Thank you very much, Mika, Kuppuswamy, and Rajvi for your effort in reviewing and testing these. I know that's a lot of work, and I really appreciate it.
I put these on pci/pm together with Rajvi's pci_pm_suspend_noirq() simplification for v6.1.
Bjorn
Quoted 16 lines · Bjorn Helgaas
Quoted 14 lines · Mika Westerberg
Quoted 10 lines · Bjorn Helgaas
Bjorn Helgaas (9):
PCI/PTM: Cache PTM Capability offset
PCI/PTM: Addpci_upstream_ptm()helper
PCI/PTM: Separate configuration and enable PCI/PTM: Addpci_suspend_ptm()andpci_resume_ptm()PCI/PTM: Movepci_ptm_info()body into its only caller PCI/PTM: Preserve RsvdP bits in PTM Control register PCI/PTM: Reorder functions in logical order PCI/PTM: Consolidate PTM interface declarations PCI/PM: Always disable PTM for all devices during suspendFor the whole series,
Quoted 8 lines
Quoted 1 line
Reviewed-by Mika Westerberg <[email protected]>Thank you very much, Mika, Kuppuswamy, and Rajvi for your effort in reviewing and testing these. I know that's a lot of work, and I really appreciate it.
I put these on pci/pm together with Rajvi's
pci_pm_suspend_noirq()simplification for v6.1.
Awesome, thanks!