[PATCH 05/11] PCI: Add pci_rebar_size_supported() helper

Ilpo Järvinen posted 11 patches 3 weeks ago
There is a newer version of this series
[PATCH 05/11] PCI: Add pci_rebar_size_supported() helper
Posted by Ilpo Järvinen 3 weeks ago
Many callers of pci_rebar_get_possible_sizes() are interested in
finding out if a particular BAR Size (PCIe r6.2 sec. 7.8.6.3) is
supported by the particular BAR.

Add pci_rebar_size_supported() into PCI core to make it easy for the
drivers to determine if the BAR Size is supported or not.

Use the new function in pci_resize_resource() and in
pci_iov_vf_bar_set_size().

Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
 drivers/pci/iov.c   |  7 +------
 drivers/pci/rebar.c | 29 +++++++++++++++++++++++------
 include/linux/pci.h |  1 +
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index ac4375954c94..51844a9176a0 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -1334,7 +1334,6 @@ EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);
  */
 int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
 {
-	u32 sizes;
 	int ret;
 
 	if (!pci_resource_is_iov(resno))
@@ -1343,11 +1342,7 @@ int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
 	if (pci_iov_is_memory_decoding_enabled(dev))
 		return -EBUSY;
 
-	sizes = pci_rebar_get_possible_sizes(dev, resno);
-	if (!sizes)
-		return -ENOTSUPP;
-
-	if (!(sizes & BIT(size)))
+	if (!pci_rebar_size_supported(dev, resno, size))
 		return -EINVAL;
 
 	ret = pci_rebar_set_size(dev, resno, size);
diff --git a/drivers/pci/rebar.c b/drivers/pci/rebar.c
index 64315dd8b6bb..735d9afd6ab1 100644
--- a/drivers/pci/rebar.c
+++ b/drivers/pci/rebar.c
@@ -3,6 +3,7 @@
  * PCI Resizable BAR Extended Capability handling.
  */
 
+#include <linux/bits.h>
 #include <linux/bitfield.h>
 #include <linux/errno.h>
 #include <linux/export.h>
@@ -124,6 +125,27 @@ u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
 }
 EXPORT_SYMBOL(pci_rebar_get_possible_sizes);
 
+/**
+ * pci_rebar_size_supported - check if size is supported for BAR
+ * @pdev: PCI device
+ * @bar: BAR to check
+ * @size: size as defined in the PCIe spec (0=1MB, 31=128TB)
+ *
+ * Return: %true if @bar is resizable and @size is a supported, otherwise
+ *	   %false.
+ */
+bool pci_rebar_size_supported(struct pci_dev *pdev, int bar, int size)
+{
+	u64 sizes;
+
+	sizes = pci_rebar_get_possible_sizes(pdev, bar);
+	if (!sizes)
+		return false;
+
+	return BIT(size) & sizes;
+}
+EXPORT_SYMBOL_GPL(pci_rebar_size_supported);
+
 /**
  * pci_rebar_get_current_size - get the current size of a Resizable BAR
  * @pdev: PCI device
@@ -231,7 +253,6 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
 	struct resource *res = pci_resource_n(dev, resno);
 	struct pci_host_bridge *host;
 	int old, ret;
-	u32 sizes;
 
 	/* Check if we must preserve the firmware's resource assignment */
 	host = pci_find_host_bridge(dev->bus);
@@ -245,11 +266,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
 	if (pci_resize_is_memory_decoding_enabled(dev, resno))
 		return -EBUSY;
 
-	sizes = pci_rebar_get_possible_sizes(dev, resno);
-	if (!sizes)
-		return -ENOTSUPP;
-
-	if (!(sizes & BIT(size)))
+	if (!pci_rebar_size_supported(dev, resno, size))
 		return -EINVAL;
 
 	old = pci_rebar_get_current_size(dev, resno);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 6f0c31290675..917c3b897739 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1423,6 +1423,7 @@ void pci_release_resource(struct pci_dev *dev, int resno);
 int pci_rebar_bytes_to_size(u64 bytes);
 resource_size_t pci_rebar_size_to_bytes(int size);
 u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar);
+bool pci_rebar_size_supported(struct pci_dev *pdev, int bar, int size);
 int __must_check pci_resize_resource(struct pci_dev *dev, int i, int size);
 
 int pci_select_bars(struct pci_dev *dev, unsigned long flags);
-- 
2.39.5

Re: [PATCH 05/11] PCI: Add pci_rebar_size_supported() helper
Posted by Christian König 3 weeks ago

On 11.09.25 09:55, Ilpo Järvinen wrote:
> Many callers of pci_rebar_get_possible_sizes() are interested in
> finding out if a particular BAR Size (PCIe r6.2 sec. 7.8.6.3) is
> supported by the particular BAR.
> 
> Add pci_rebar_size_supported() into PCI core to make it easy for the
> drivers to determine if the BAR Size is supported or not.
> 
> Use the new function in pci_resize_resource() and in
> pci_iov_vf_bar_set_size().
> 
> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> ---
>  drivers/pci/iov.c   |  7 +------
>  drivers/pci/rebar.c | 29 +++++++++++++++++++++++------
>  include/linux/pci.h |  1 +
>  3 files changed, 25 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> index ac4375954c94..51844a9176a0 100644
> --- a/drivers/pci/iov.c
> +++ b/drivers/pci/iov.c
> @@ -1334,7 +1334,6 @@ EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);
>   */
>  int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
>  {
> -	u32 sizes;
>  	int ret;
>  
>  	if (!pci_resource_is_iov(resno))
> @@ -1343,11 +1342,7 @@ int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
>  	if (pci_iov_is_memory_decoding_enabled(dev))
>  		return -EBUSY;
>  
> -	sizes = pci_rebar_get_possible_sizes(dev, resno);
> -	if (!sizes)
> -		return -ENOTSUPP;
> -
> -	if (!(sizes & BIT(size)))
> +	if (!pci_rebar_size_supported(dev, resno, size))
>  		return -EINVAL;
>  
>  	ret = pci_rebar_set_size(dev, resno, size);
> diff --git a/drivers/pci/rebar.c b/drivers/pci/rebar.c
> index 64315dd8b6bb..735d9afd6ab1 100644
> --- a/drivers/pci/rebar.c
> +++ b/drivers/pci/rebar.c
> @@ -3,6 +3,7 @@
>   * PCI Resizable BAR Extended Capability handling.
>   */
>  
> +#include <linux/bits.h>
>  #include <linux/bitfield.h>
>  #include <linux/errno.h>
>  #include <linux/export.h>
> @@ -124,6 +125,27 @@ u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
>  }
>  EXPORT_SYMBOL(pci_rebar_get_possible_sizes);
>  
> +/**
> + * pci_rebar_size_supported - check if size is supported for BAR
> + * @pdev: PCI device
> + * @bar: BAR to check
> + * @size: size as defined in the PCIe spec (0=1MB, 31=128TB)
> + *
> + * Return: %true if @bar is resizable and @size is a supported, otherwise
> + *	   %false.
> + */
> +bool pci_rebar_size_supported(struct pci_dev *pdev, int bar, int size)
> +{
> +	u64 sizes;
> +
> +	sizes = pci_rebar_get_possible_sizes(pdev, bar);

> +	if (!sizes)
> +		return false;
> +
> +	return BIT(size) & sizes;

Checking size for zero first looks superfluous.

Regards,
Christian.

> +}
> +EXPORT_SYMBOL_GPL(pci_rebar_size_supported);
> +
>  /**
>   * pci_rebar_get_current_size - get the current size of a Resizable BAR
>   * @pdev: PCI device
> @@ -231,7 +253,6 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
>  	struct resource *res = pci_resource_n(dev, resno);
>  	struct pci_host_bridge *host;
>  	int old, ret;
> -	u32 sizes;
>  
>  	/* Check if we must preserve the firmware's resource assignment */
>  	host = pci_find_host_bridge(dev->bus);
> @@ -245,11 +266,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size)
>  	if (pci_resize_is_memory_decoding_enabled(dev, resno))
>  		return -EBUSY;
>  
> -	sizes = pci_rebar_get_possible_sizes(dev, resno);
> -	if (!sizes)
> -		return -ENOTSUPP;
> -
> -	if (!(sizes & BIT(size)))
> +	if (!pci_rebar_size_supported(dev, resno, size))
>  		return -EINVAL;
>  
>  	old = pci_rebar_get_current_size(dev, resno);
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 6f0c31290675..917c3b897739 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -1423,6 +1423,7 @@ void pci_release_resource(struct pci_dev *dev, int resno);
>  int pci_rebar_bytes_to_size(u64 bytes);
>  resource_size_t pci_rebar_size_to_bytes(int size);
>  u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar);
> +bool pci_rebar_size_supported(struct pci_dev *pdev, int bar, int size);
>  int __must_check pci_resize_resource(struct pci_dev *dev, int i, int size);
>  
>  int pci_select_bars(struct pci_dev *dev, unsigned long flags);