diff mbox series

[v2,2/4] PCI: endpoint: improve pci_epf_alloc_space()

Message ID 20240207213922.1796533-3-cassel@kernel.org
State New
Headers show
Series pci_epf_alloc_space() cleanups | expand

Commit Message

Niklas Cassel Feb. 7, 2024, 9:39 p.m. UTC
pci_epf_alloc_space() already performs checks on the requested BAR size,
and will allocate and set epf_bar->size to a size higher than the
requested BAR size if some constraint deems it necessary.

However, other than pci_epf_alloc_space() already doing these roundups,
there are additional checks and roundups done in e.g. pci-epf-test.c.

And similar checks are proposed to other endpoint function drivers, see:
https://lore.kernel.org/linux-pci/20240108151015.2030469-1-Frank.Li@nxp.com/

Having these checks scattered over different locations in multiple EPF
drivers is not maintainable and makes the code hard to follow.

Since pci_epf_alloc_space() already performs roundups, add the checks
currently performed by pci-epf-test.c to pci_epf_alloc_space(), such that
a follow up patch can drop these checks from pci-epf-test.c.

Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
 drivers/pci/endpoint/pci-epf-core.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Manivannan Sadhasivam Feb. 9, 2024, 8:37 a.m. UTC | #1
On Wed, Feb 07, 2024 at 10:39:15PM +0100, Niklas Cassel wrote:
> pci_epf_alloc_space() already performs checks on the requested BAR size,
> and will allocate and set epf_bar->size to a size higher than the
> requested BAR size if some constraint deems it necessary.
> 
> However, other than pci_epf_alloc_space() already doing these roundups,
> there are additional checks and roundups done in e.g. pci-epf-test.c.
> 
> And similar checks are proposed to other endpoint function drivers, see:
> https://lore.kernel.org/linux-pci/20240108151015.2030469-1-Frank.Li@nxp.com/
> 
> Having these checks scattered over different locations in multiple EPF
> drivers is not maintainable and makes the code hard to follow.
> 
> Since pci_epf_alloc_space() already performs roundups, add the checks
> currently performed by pci-epf-test.c to pci_epf_alloc_space(), such that
> a follow up patch can drop these checks from pci-epf-test.c.
> 
> Signed-off-by: Niklas Cassel <cassel@kernel.org>

Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

- Mani

> ---
>  drivers/pci/endpoint/pci-epf-core.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> index 1d405fd61a2a..367e029f6716 100644
> --- a/drivers/pci/endpoint/pci-epf-core.c
> +++ b/drivers/pci/endpoint/pci-epf-core.c
> @@ -260,6 +260,7 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
>  			  const struct pci_epc_features *epc_features,
>  			  enum pci_epc_interface_type type)
>  {
> +	u64 bar_fixed_size = epc_features->bar_fixed_size[bar];
>  	size_t align = epc_features->align;
>  	struct pci_epf_bar *epf_bar;
>  	dma_addr_t phys_addr;
> @@ -270,6 +271,14 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
>  	if (size < 128)
>  		size = 128;
>  
> +	if (bar_fixed_size && size > bar_fixed_size) {
> +		dev_err(dev, "requested BAR size is larger than fixed size\n");
> +		return NULL;
> +	}
> +
> +	if (bar_fixed_size)
> +		size = bar_fixed_size;
> +
>  	if (align)
>  		size = ALIGN(size, align);
>  	else
> -- 
> 2.43.0
>
diff mbox series

Patch

diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
index 1d405fd61a2a..367e029f6716 100644
--- a/drivers/pci/endpoint/pci-epf-core.c
+++ b/drivers/pci/endpoint/pci-epf-core.c
@@ -260,6 +260,7 @@  void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
 			  const struct pci_epc_features *epc_features,
 			  enum pci_epc_interface_type type)
 {
+	u64 bar_fixed_size = epc_features->bar_fixed_size[bar];
 	size_t align = epc_features->align;
 	struct pci_epf_bar *epf_bar;
 	dma_addr_t phys_addr;
@@ -270,6 +271,14 @@  void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
 	if (size < 128)
 		size = 128;
 
+	if (bar_fixed_size && size > bar_fixed_size) {
+		dev_err(dev, "requested BAR size is larger than fixed size\n");
+		return NULL;
+	}
+
+	if (bar_fixed_size)
+		size = bar_fixed_size;
+
 	if (align)
 		size = ALIGN(size, align);
 	else