diff mbox series

[v3,02/10] PCI/PTM: Cache PTM Capability offset

Message ID 20220906222351.64760-3-helgaas@kernel.org
State New
Headers show
Series PCI/PM: Always disable PTM for all devices during suspend | expand

Commit Message

Bjorn Helgaas Sept. 6, 2022, 10:23 p.m. UTC
From: Bjorn Helgaas <bhelgaas@google.com>

Cache the PTM Capability offset instead of searching for it every time we
enable/disable PTM or save/restore PTM state.

Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
---
 drivers/pci/pcie/ptm.c | 41 +++++++++++++++++------------------------
 include/linux/pci.h    |  1 +
 2 files changed, 18 insertions(+), 24 deletions(-)

Comments

Kuppuswamy Sathyanarayanan Sept. 6, 2022, 11:18 p.m. UTC | #1
Hi,

On 9/6/22 3:23 PM, Bjorn Helgaas wrote:
> From: Bjorn Helgaas <bhelgaas@google.com>
> 
> Cache the PTM Capability offset instead of searching for it every time we
> enable/disable PTM or save/restore PTM state.
> 
> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
> ---
>  drivers/pci/pcie/ptm.c | 41 +++++++++++++++++------------------------
>  include/linux/pci.h    |  1 +
>  2 files changed, 18 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
> index b6a417247ce3..6ac7ff48be57 100644
> --- a/drivers/pci/pcie/ptm.c
> +++ b/drivers/pci/pcie/ptm.c
> @@ -31,13 +31,9 @@ static void pci_ptm_info(struct pci_dev *dev)
>  
>  void pci_disable_ptm(struct pci_dev *dev)
>  {
> -	int ptm;
> +	int ptm = dev->ptm_cap;

I think you don't need to store it. Directly use dev->ptm?

>  	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;
> +	int ptm = dev->ptm_cap;

Same as above.

>  	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)
>  {
> +	int ptm = dev->ptm_cap;

It can be u16?

>  	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;
> +	int ptm;

Why rename? Also ptm can be u16

>  	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;
> +	int 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);
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 060af91bafcd..54be939023a3 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -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;
Bjorn Helgaas Sept. 7, 2022, 3:49 p.m. UTC | #2
On Tue, Sep 06, 2022 at 04:18:23PM -0700, Sathyanarayanan Kuppuswamy wrote:
> On 9/6/22 3:23 PM, Bjorn Helgaas wrote:

> >  void pci_disable_ptm(struct pci_dev *dev)
> >  {
> > -	int ptm;
> > +	int ptm = dev->ptm_cap;
> 
> I think you don't need to store it. Directly use dev->ptm?

True, no need, but the value is used three times in this function, so
I think the variable reduces clutter overall.

> >  void pci_restore_ptm_state(struct pci_dev *dev)
> >  {
> > +	int ptm = dev->ptm_cap;
> 
> It can be u16?

Done, thanks!  I see that in ee8b1c478a9f ("PCI: Return u16 from
pci_find_ext_capability() and similar"), I forgot to change the inline
stub from int to u16.  I'll add a patch to do that.  Probably not a
prerequisite, since the stub is for !CONFIG_PCI and this code won't
be compiled at all in that case.

> >  void pci_ptm_init(struct pci_dev *dev)
> >  {
> > -	int pos;
> > +	int ptm;
> 
> Why rename? Also ptm can be u16

"ptm" conveys more information than "pos" and I think it's worth using
the same name for all the functions.

Thank you!

Bjorn
diff mbox series

Patch

diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
index b6a417247ce3..6ac7ff48be57 100644
--- a/drivers/pci/pcie/ptm.c
+++ b/drivers/pci/pcie/ptm.c
@@ -31,13 +31,9 @@  static void pci_ptm_info(struct pci_dev *dev)
 
 void pci_disable_ptm(struct pci_dev *dev)
 {
-	int ptm;
+	int 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;
+	int 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)
 {
+	int 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;
+	int 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;
+	int 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);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 060af91bafcd..54be939023a3 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -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;