linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] PCIe: Refactor link speed configuration with unified macro
@ 2025-05-19 16:31 Hans Zhang
  2025-05-19 16:31 ` [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro Hans Zhang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Hans Zhang @ 2025-05-19 16:31 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	ilpo.jarvinen, jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

This series standardizes PCIe link speed handling across multiple drivers
by introducing a common conversion macro PCIE_SPEED2LNKCTL2_TLS_ENC. The
changes eliminate redundant speed-to-register mappings and simplify code
maintenance:

The refactoring improves code consistency and reduces conditional
branching, while maintaining full backward compatibility with existing
speed settings.

Hans Zhang (3):
  PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro
  PCI: dwc: Simplify link speed configuration with macro
  PCI/bwctrl: Replace legacy speed conversion with shared macro

 drivers/pci/controller/dwc/pcie-designware.c | 18 +++---------------
 drivers/pci/pci.h                            |  9 +++++++++
 drivers/pci/pcie/bwctrl.c                    | 19 +------------------
 3 files changed, 13 insertions(+), 33 deletions(-)


base-commit: fee3e843b309444f48157e2188efa6818bae85cf
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro
  2025-05-19 16:31 [PATCH 0/3] PCIe: Refactor link speed configuration with unified macro Hans Zhang
@ 2025-05-19 16:31 ` Hans Zhang
  2025-06-03  9:08   ` Ilpo Järvinen
  2025-05-19 16:31 ` [PATCH 2/3] PCI: dwc: Simplify link speed configuration with macro Hans Zhang
  2025-05-19 16:31 ` [PATCH 3/3] PCI/bwctrl: Replace legacy speed conversion with shared macro Hans Zhang
  2 siblings, 1 reply; 6+ messages in thread
From: Hans Zhang @ 2025-05-19 16:31 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	ilpo.jarvinen, jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Introduce PCIE_SPEED2LNKCTL2_TLS_ENC macro to standardize the conversion
between PCIe speed enumerations and LNKCTL2_TLS register values. This
centralizes speed-to-register mapping logic, eliminating duplicated
conversion code across multiple drivers.

Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/pci/pci.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index f92928dadc6a..b7e2d08825c6 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -415,6 +415,15 @@ void pci_bus_put(struct pci_bus *bus);
 	 (lnkctl2) == PCI_EXP_LNKCTL2_TLS_2_5GT ? PCIE_SPEED_2_5GT : \
 	 PCI_SPEED_UNKNOWN)
 
+#define PCIE_SPEED2LNKCTL2_TLS_ENC(speed) \
+	((speed) == PCIE_SPEED_2_5GT ? PCI_EXP_LNKCTL2_TLS_2_5GT : \
+	 (speed) == PCIE_SPEED_5_0GT ? PCI_EXP_LNKCTL2_TLS_5_0GT : \
+	 (speed) == PCIE_SPEED_8_0GT ? PCI_EXP_LNKCTL2_TLS_8_0GT : \
+	 (speed) == PCIE_SPEED_16_0GT ? PCI_EXP_LNKCTL2_TLS_16_0GT : \
+	 (speed) == PCIE_SPEED_32_0GT ? PCI_EXP_LNKCTL2_TLS_32_0GT : \
+	 (speed) == PCIE_SPEED_64_0GT ? PCI_EXP_LNKCTL2_TLS_64_0GT : \
+	 0)
+
 /* PCIe speed to Mb/s reduced by encoding overhead */
 #define PCIE_SPEED2MBS_ENC(speed) \
 	((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] PCI: dwc: Simplify link speed configuration with macro
  2025-05-19 16:31 [PATCH 0/3] PCIe: Refactor link speed configuration with unified macro Hans Zhang
  2025-05-19 16:31 ` [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro Hans Zhang
@ 2025-05-19 16:31 ` Hans Zhang
  2025-05-19 16:31 ` [PATCH 3/3] PCI/bwctrl: Replace legacy speed conversion with shared macro Hans Zhang
  2 siblings, 0 replies; 6+ messages in thread
From: Hans Zhang @ 2025-05-19 16:31 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	ilpo.jarvinen, jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Replace switch-case based speed-to-register conversion in DesignWare
driver with the newly introduced PCIE_SPEED2LNKCTL2_TLS_ENC macro.

Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/pci/controller/dwc/pcie-designware.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-designware.c b/drivers/pci/controller/dwc/pcie-designware.c
index 97d76d3dc066..951e2ce69dfa 100644
--- a/drivers/pci/controller/dwc/pcie-designware.c
+++ b/drivers/pci/controller/dwc/pcie-designware.c
@@ -754,24 +754,12 @@ static void dw_pcie_link_set_max_speed(struct dw_pcie *pci)
 	ctrl2 = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCTL2);
 	ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
 
-	switch (pcie_link_speed[pci->max_link_speed]) {
-	case PCIE_SPEED_2_5GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_2_5GT;
-		break;
-	case PCIE_SPEED_5_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT;
-		break;
-	case PCIE_SPEED_8_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_8_0GT;
-		break;
-	case PCIE_SPEED_16_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_16_0GT;
-		break;
-	default:
+	link_speed = pcie_link_speed[pci->max_link_speed];
+	link_speed = PCIE_SPEED2LNKCTL2_TLS_ENC(link_speed);
+	if (link_speed == 0) {
 		/* Use hardware capability */
 		link_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, cap);
 		ctrl2 &= ~PCI_EXP_LNKCTL2_HASD;
-		break;
 	}
 
 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCTL2, ctrl2 | link_speed);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] PCI/bwctrl: Replace legacy speed conversion with shared macro
  2025-05-19 16:31 [PATCH 0/3] PCIe: Refactor link speed configuration with unified macro Hans Zhang
  2025-05-19 16:31 ` [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro Hans Zhang
  2025-05-19 16:31 ` [PATCH 2/3] PCI: dwc: Simplify link speed configuration with macro Hans Zhang
@ 2025-05-19 16:31 ` Hans Zhang
  2 siblings, 0 replies; 6+ messages in thread
From: Hans Zhang @ 2025-05-19 16:31 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	ilpo.jarvinen, jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Remove obsolete pci_bus_speed2lnkctl2() function and utilize the common
PCIE_SPEED2LNKCTL2_TLS_ENC macro instead.

Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/pci/pcie/bwctrl.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)

diff --git a/drivers/pci/pcie/bwctrl.c b/drivers/pci/pcie/bwctrl.c
index d8d2aa85a229..3e17f6366a69 100644
--- a/drivers/pci/pcie/bwctrl.c
+++ b/drivers/pci/pcie/bwctrl.c
@@ -63,23 +63,6 @@ static bool pcie_valid_speed(enum pci_bus_speed speed)
 	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
 }
 
-static u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
-{
-	static const u8 speed_conv[] = {
-		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
-		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
-		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
-		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
-		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
-		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
-	};
-
-	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
-		return 0;
-
-	return speed_conv[speed];
-}
-
 static inline u16 pcie_supported_speeds2target_speed(u8 supported_speeds)
 {
 	return __fls(supported_speeds);
@@ -101,7 +84,7 @@ static u16 pcie_bwctrl_select_speed(struct pci_dev *port, enum pci_bus_speed spe
 	u8 desired_speeds, supported_speeds;
 	struct pci_dev *dev;
 
-	desired_speeds = GENMASK(pci_bus_speed2lnkctl2(speed_req),
+	desired_speeds = GENMASK(PCIE_SPEED2LNKCTL2_TLS_ENC(speed_req),
 				 __fls(PCI_EXP_LNKCAP2_SLS_2_5GB));
 
 	supported_speeds = port->supported_speeds;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro
  2025-05-19 16:31 ` [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro Hans Zhang
@ 2025-06-03  9:08   ` Ilpo Järvinen
  2025-06-03 15:31     ` Hans Zhang
  0 siblings, 1 reply; 6+ messages in thread
From: Ilpo Järvinen @ 2025-06-03  9:08 UTC (permalink / raw)
  To: Hans Zhang
  Cc: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	jingoohan1, robh, linux-pci, LKML

On Tue, 20 May 2025, Hans Zhang wrote:

> Introduce PCIE_SPEED2LNKCTL2_TLS_ENC macro to standardize the conversion

Use () parenthesis like you'd use them in C so functions and macros in 
changelog should have them appended.

> between PCIe speed enumerations and LNKCTL2_TLS register values. This
> centralizes speed-to-register mapping logic, eliminating duplicated
> conversion code across multiple drivers.
> 
> Signed-off-by: Hans Zhang <18255117159@163.com>
> ---
>  drivers/pci/pci.h | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index f92928dadc6a..b7e2d08825c6 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h
> @@ -415,6 +415,15 @@ void pci_bus_put(struct pci_bus *bus);
>  	 (lnkctl2) == PCI_EXP_LNKCTL2_TLS_2_5GT ? PCIE_SPEED_2_5GT : \
>  	 PCI_SPEED_UNKNOWN)
>  
> +#define PCIE_SPEED2LNKCTL2_TLS_ENC(speed) \

I'm not a big fan of including that _ENC there, it just makes the long 
name even longer and doesn't really provide added value, IMO.

Other than those, this change logs fine.

> +	((speed) == PCIE_SPEED_2_5GT ? PCI_EXP_LNKCTL2_TLS_2_5GT : \
> +	 (speed) == PCIE_SPEED_5_0GT ? PCI_EXP_LNKCTL2_TLS_5_0GT : \
> +	 (speed) == PCIE_SPEED_8_0GT ? PCI_EXP_LNKCTL2_TLS_8_0GT : \
> +	 (speed) == PCIE_SPEED_16_0GT ? PCI_EXP_LNKCTL2_TLS_16_0GT : \
> +	 (speed) == PCIE_SPEED_32_0GT ? PCI_EXP_LNKCTL2_TLS_32_0GT : \
> +	 (speed) == PCIE_SPEED_64_0GT ? PCI_EXP_LNKCTL2_TLS_64_0GT : \
> +	 0)
> +
>  /* PCIe speed to Mb/s reduced by encoding overhead */
>  #define PCIE_SPEED2MBS_ENC(speed) \
>  	((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
> 

-- 
 i.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro
  2025-06-03  9:08   ` Ilpo Järvinen
@ 2025-06-03 15:31     ` Hans Zhang
  0 siblings, 0 replies; 6+ messages in thread
From: Hans Zhang @ 2025-06-03 15:31 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: bhelgaas, lpieralisi, kw, krzk+dt, manivannan.sadhasivam,
	jingoohan1, robh, linux-pci, LKML



On 2025/6/3 17:08, Ilpo Järvinen wrote:
> On Tue, 20 May 2025, Hans Zhang wrote:
> 
>> Introduce PCIE_SPEED2LNKCTL2_TLS_ENC macro to standardize the conversion
> 
> Use () parenthesis like you'd use them in C so functions and macros in
> changelog should have them appended.
> 

Dear Ilpo,

Thank you very much for your reply and reminder. Will change.

>> between PCIe speed enumerations and LNKCTL2_TLS register values. This
>> centralizes speed-to-register mapping logic, eliminating duplicated
>> conversion code across multiple drivers.
>>
>> Signed-off-by: Hans Zhang <18255117159@163.com>
>> ---
>>   drivers/pci/pci.h | 9 +++++++++
>>   1 file changed, 9 insertions(+)
>>
>> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
>> index f92928dadc6a..b7e2d08825c6 100644
>> --- a/drivers/pci/pci.h
>> +++ b/drivers/pci/pci.h
>> @@ -415,6 +415,15 @@ void pci_bus_put(struct pci_bus *bus);
>>   	 (lnkctl2) == PCI_EXP_LNKCTL2_TLS_2_5GT ? PCIE_SPEED_2_5GT : \
>>   	 PCI_SPEED_UNKNOWN)
>>   
>> +#define PCIE_SPEED2LNKCTL2_TLS_ENC(speed) \
> 
> I'm not a big fan of including that _ENC there, it just makes the long
> name even longer and doesn't really provide added value, IMO.
> 

Will delete _ENC.

Best regards,
Hans

> Other than those, this change logs fine.
> 
>> +	((speed) == PCIE_SPEED_2_5GT ? PCI_EXP_LNKCTL2_TLS_2_5GT : \
>> +	 (speed) == PCIE_SPEED_5_0GT ? PCI_EXP_LNKCTL2_TLS_5_0GT : \
>> +	 (speed) == PCIE_SPEED_8_0GT ? PCI_EXP_LNKCTL2_TLS_8_0GT : \
>> +	 (speed) == PCIE_SPEED_16_0GT ? PCI_EXP_LNKCTL2_TLS_16_0GT : \
>> +	 (speed) == PCIE_SPEED_32_0GT ? PCI_EXP_LNKCTL2_TLS_32_0GT : \
>> +	 (speed) == PCIE_SPEED_64_0GT ? PCI_EXP_LNKCTL2_TLS_64_0GT : \
>> +	 0)
>> +
>>   /* PCIe speed to Mb/s reduced by encoding overhead */
>>   #define PCIE_SPEED2MBS_ENC(speed) \
>>   	((speed) == PCIE_SPEED_64_0GT ? 64000*1/1 : \
>>
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-06-03 15:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-19 16:31 [PATCH 0/3] PCIe: Refactor link speed configuration with unified macro Hans Zhang
2025-05-19 16:31 ` [PATCH 1/3] PCI: Add PCIE_SPEED2LNKCTL2_TLS_ENC conversion macro Hans Zhang
2025-06-03  9:08   ` Ilpo Järvinen
2025-06-03 15:31     ` Hans Zhang
2025-05-19 16:31 ` [PATCH 2/3] PCI: dwc: Simplify link speed configuration with macro Hans Zhang
2025-05-19 16:31 ` [PATCH 3/3] PCI/bwctrl: Replace legacy speed conversion with shared macro Hans Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).