Skip to contentSkip to conversation

pci_epf_alloc_space() cleanups

PATCH Applied Niklas Cassel 3 participants 10 messages Feb 7, 2024 – Feb 9, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover pci_epf_alloc_space() cleanups

Hello all,

Here comes some cleanups related to pci_epf_alloc_space().

Changes since v1:
-Picked up Reviewed-by tags.
-Fixed kdoc param name to match the actual param name in patch 1/4. -Split patch "improve pci_epf_alloc_space()" into one patch changing
pci-epf-core.c (patch 2/4 in V2) and pci-epf-test (patch 3/4 in V2).
-Perform the alignment even for fixed size BARs. We need this since:
1) Some platforms have fixed_size_bars that are smaller than the iATU MIN REGION.
2) No longer doing so would be a functional change and not a cleanup.

Kind regards,
Niklas

Niklas Cassel (4):
PCI: endpoint: refactor pci_epf_alloc_space()
PCI: endpoint: improve pci_epf_alloc_space()
PCI: endpoint: pci-epf-test: remove superfluous checks
PCI: endpoint: pci-epf-vntb: remove superfluous checks

 drivers/pci/endpoint/functions/pci-epf-ntb.c  |  2 +-
 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 ++-------------
 drivers/pci/endpoint/pci-epf-core.c           | 15 +++++++++++++--
 include/linux/pci-epf.h                       |  4 +++-
 5 files changed, 21 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 PCI: endpoint: refactor pci_epf_alloc_space()

Refactor pci_epf_alloc_space() to take epc_features as a parameter. This is a preparation patch needed for further cleanups.

---
 drivers/pci/endpoint/functions/pci-epf-ntb.c  | 2 +-
 drivers/pci/endpoint/functions/pci-epf-test.c | 5 ++---
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 4 ++--
 drivers/pci/endpoint/pci-epf-core.c           | 6 ++++--
 include/linux/pci-epf.h                       | 4 +++-
 5 files changed, 12 insertions(+), 9 deletions(-)
drivers/pci/endpoint/functions/pci-epf-ntb.c +1 -1
index 0553946005c4..43cd309ce22f 100644
@@ -1067,7 +1067,7 @@ static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb,
 	else if (size < ctrl_size + spad_size)
 		return -EINVAL;
 
-	base = pci_epf_alloc_space(epf, size, barno, align, type);
+	base = pci_epf_alloc_space(epf, size, barno, epc_features, type);
 	if (!base) {
 		dev_err(dev, "%s intf: Config/Status/SPAD alloc region fail\n",
 			pci_epc_interface_string(type));
drivers/pci/endpoint/functions/pci-epf-test.c +2 -3
index 18c80002d3bd..15bfa7d83489 100644
@@ -848,7 +848,7 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
 	}
 
 	base = pci_epf_alloc_space(epf, test_reg_size, test_reg_bar,
-				   epc_features->align, PRIMARY_INTERFACE);
+				   epc_features, PRIMARY_INTERFACE);
 	if (!base) {
 		dev_err(dev, "Failed to allocated register space\n");
 		return -ENOMEM;
@@ -866,8 +866,7 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
 			continue;
 
 		base = pci_epf_alloc_space(epf, bar_size[bar], bar,
-					   epc_features->align,
-					   PRIMARY_INTERFACE);
+					   epc_features, PRIMARY_INTERFACE);
 		if (!base)
 			dev_err(dev, "Failed to allocate space for BAR%d\n",
 				bar);
drivers/pci/endpoint/functions/pci-epf-vntb.c +2 -2
index e75a2af77328..ba509d67188b 100644
@@ -446,7 +446,7 @@ static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb)
 	else if (size < ctrl_size + spad_size)
 		return -EINVAL;
 
-	base = pci_epf_alloc_space(epf, size, barno, align, 0);
+	base = pci_epf_alloc_space(epf, size, barno, epc_features, 0);
 	if (!base) {
 		dev_err(dev, "Config/Status/SPAD alloc region fail\n");
 		return -ENOMEM;
@@ -550,7 +550,7 @@ static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
 
 	barno = ntb->epf_ntb_bar[BAR_DB];
 
-	mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, align, 0);
+	mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
 	if (!mw_addr) {
 		dev_err(dev, "Failed to allocate OB address\n");
 		return -ENOMEM;
drivers/pci/endpoint/pci-epf-core.c +4 -2
index 2c32de667937..1d405fd61a2a 100644
@@ -251,14 +251,16 @@ EXPORT_SYMBOL_GPL(pci_epf_free_space);
  * @epf: the EPF device to whom allocate the memory
  * @size: the size of the memory that has to be allocated
  * @bar: the BAR number corresponding to the allocated register space
- * @align: alignment size for the allocation region
+ * @epc_features: the features provided by the EPC specific to this EPF
  * @type: Identifies if the allocation is for primary EPC or secondary EPC
  *
  * Invoke to allocate memory for the PCI EPF register space.
  */
 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
-			  size_t align, enum pci_epc_interface_type type)
+			  const struct pci_epc_features *epc_features,
+			  enum pci_epc_interface_type type)
 {
+	size_t align = epc_features->align;
 	struct pci_epf_bar *epf_bar;
 	dma_addr_t phys_addr;
 	struct pci_epc *epc;
include/linux/pci-epf.h +3 -1
index 77b146e0f672..adee6a1b35db 100644
@@ -15,6 +15,7 @@
 #include <linux/pci.h>
 
 struct pci_epf;
+struct pci_epc_features;
 enum pci_epc_interface_type;
 
 enum pci_barno {
@@ -216,7 +217,8 @@ int __pci_epf_register_driver(struct pci_epf_driver *driver,
 			      struct module *owner);
 void pci_epf_unregister_driver(struct pci_epf_driver *driver);
 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
-			  size_t align, enum pci_epc_interface_type type);
+			  const struct pci_epc_features *epc_features,
+			  enum pci_epc_interface_type type);
 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
 			enum pci_epc_interface_type type);
 int pci_epf_bind(struct pci_epf *epf);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/4 PCI: endpoint: improve pci_epf_alloc_space()

In reply to Niklas Cassel

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/[email protected]/

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.

---
 drivers/pci/endpoint/pci-epf-core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/pci/endpoint/pci-epf-core.c +9 -0
index 1d405fd61a2a..367e029f6716 100644
@@ -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
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 3/4 PCI: endpoint: pci-epf-test: remove superfluous checks

In reply to Niklas Cassel

Remove superfluous alignment checks, these checks are already done by pci_epf_alloc_space().

---
 drivers/pci/endpoint/functions/pci-epf-test.c | 8 --------
 1 file changed, 8 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +0 -8
index 15bfa7d83489..981894e40681 100644
@@ -841,12 +841,6 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
 	}
 	test_reg_size = test_reg_bar_size + msix_table_size + pba_size;
 
-	if (epc_features->bar_fixed_size[test_reg_bar]) {
-		if (test_reg_size > bar_size[test_reg_bar])
-			return -ENOMEM;
-		test_reg_size = bar_size[test_reg_bar];
-	}
-
 	base = pci_epf_alloc_space(epf, test_reg_size, test_reg_bar,
 				   epc_features, PRIMARY_INTERFACE);
 	if (!base) {
@@ -888,8 +882,6 @@ static void pci_epf_configure_bar(struct pci_epf *epf,
 		bar_fixed_64bit = !!(epc_features->bar_fixed_64bit & (1 << i));
 		if (bar_fixed_64bit)
 			epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
-		if (epc_features->bar_fixed_size[i])
-			bar_size[i] = epc_features->bar_fixed_size[i];
 	}
 }
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 4/4 PCI: endpoint: pci-epf-vntb: remove superfluous checks

In reply to Niklas Cassel

Remove superfluous alignment checks, these checks are already done by pci_epf_alloc_space().

---
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +0 -11
index ba509d67188b..eda4b906868b 100644
@@ -527,7 +527,6 @@ static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
 static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
 {
 	const struct pci_epc_features *epc_features;
-	u32 align;
 	struct device *dev = &ntb->epf->dev;
 	int ret;
 	struct pci_epf_bar *epf_bar;
@@ -538,16 +537,6 @@ static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
 	epc_features = pci_epc_get_features(ntb->epf->epc,
 					    ntb->epf->func_no,
 					    ntb->epf->vfunc_no);
-	align = epc_features->align;
-
-	if (size < 128)
-		size = 128;
-
-	if (align)
-		size = ALIGN(size, align);
-	else
-		size = roundup_pow_of_two(size);
-
 	barno = ntb->epf_ntb_bar[BAR_DB];
 
 	mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
Signature · 1 line
2.43.0
Reviewed-by on the v2 series
#8 lore raw
In reply to Niklas Cassel
Quoted 6 lines · Niklas Cassel

Hello all,

Here comes some cleanups related to pci_epf_alloc_space().

Changes since v1:

Review

Quoted 8 lines
(quoted diff) +0 -4
-Picked up Reviewed-by tags.
-Fixed kdoc param name to match the actual param name in patch 1/4.
-Split patch "improve pci_epf_alloc_space()" into one patch changing
 pci-epf-core.c (patch 2/4 in V2) and pci-epf-test (patch 3/4 in V2).
-Perform the alignment even for fixed size BARs. We need this since:
 1) Some platforms have fixed_size_bars that are smaller than the
    iATU MIN REGION.
 2) No longer doing so would be a functional change and not a cleanup.
Quoted 21 lines

Kind regards,
Niklas

Niklas Cassel (4):
PCI: endpoint: refactor pci_epf_alloc_space()
PCI: endpoint: improve pci_epf_alloc_space()
PCI: endpoint: pci-epf-test: remove superfluous checks
PCI: endpoint: pci-epf-vntb: remove superfluous checks

 drivers/pci/endpoint/functions/pci-epf-ntb.c  |  2 +-
 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 ++-------------
 drivers/pci/endpoint/pci-epf-core.c           | 15 +++++++++++++--
 include/linux/pci-epf.h                       |  4 +++-
 5 files changed, 21 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
Reviewed-by on v2 2/4
#3 lore raw

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

In reply to Niklas Cassel
Quoted 10 lines · Niklas Cassel

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/[email protected]/

Quoted 8 lines

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.

- Mani

Quoted 34 lines
---
 drivers/pci/endpoint/pci-epf-core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/pci/endpoint/pci-epf-core.c +9 -0
index 1d405fd61a2a..367e029f6716 100644
@@ -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
Signature · 1 line
2.43.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Reviewed-by on v2 3/4
#5 lore raw

Re: [PATCH v2 3/4] PCI: endpoint: pci-epf-test: remove superfluous checks

In reply to Niklas Cassel
Quoted 4 lines · Niklas Cassel

Remove superfluous alignment checks, these checks are already done by pci_epf_alloc_space().

- Mani

Quoted 33 lines
---
 drivers/pci/endpoint/functions/pci-epf-test.c | 8 --------
 1 file changed, 8 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +0 -8
index 15bfa7d83489..981894e40681 100644
@@ -841,12 +841,6 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
 	}
 	test_reg_size = test_reg_bar_size + msix_table_size + pba_size;
 
-	if (epc_features->bar_fixed_size[test_reg_bar]) {
-		if (test_reg_size > bar_size[test_reg_bar])
-			return -ENOMEM;
-		test_reg_size = bar_size[test_reg_bar];
-	}
-
 	base = pci_epf_alloc_space(epf, test_reg_size, test_reg_bar,
 				   epc_features, PRIMARY_INTERFACE);
 	if (!base) {
@@ -888,8 +882,6 @@ static void pci_epf_configure_bar(struct pci_epf *epf,
 		bar_fixed_64bit = !!(epc_features->bar_fixed_64bit & (1 << i));
 		if (bar_fixed_64bit)
 			epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
-		if (epc_features->bar_fixed_size[i])
-			bar_size[i] = epc_features->bar_fixed_size[i];
 	}
 }
 
Signature · 1 line
2.43.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Reviewed-by on v2 4/4
#7 lore raw

Re: [PATCH v2 4/4] PCI: endpoint: pci-epf-vntb: remove superfluous checks

In reply to Niklas Cassel
Quoted 4 lines · Niklas Cassel

Remove superfluous alignment checks, these checks are already done by pci_epf_alloc_space().

- Mani

Quoted 36 lines
---
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +0 -11
index ba509d67188b..eda4b906868b 100644
@@ -527,7 +527,6 @@ static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
 static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
 {
 	const struct pci_epc_features *epc_features;
-	u32 align;
 	struct device *dev = &ntb->epf->dev;
 	int ret;
 	struct pci_epf_bar *epf_bar;
@@ -538,16 +537,6 @@ static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
 	epc_features = pci_epc_get_features(ntb->epf->epc,
 					    ntb->epf->func_no,
 					    ntb->epf->vfunc_no);
-	align = epc_features->align;
-
-	if (size < 128)
-		size = 128;
-
-	if (align)
-		size = ALIGN(size, align);
-	else
-		size = roundup_pow_of_two(size);
-
 	barno = ntb->epf_ntb_bar[BAR_DB];
 
 	mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
Signature · 1 line
2.43.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#9 lore raw
In reply to Niklas Cassel
Quoted 4 lines · Niklas Cassel

Hello all,

Here comes some cleanups related to pci_epf_alloc_space().

Applied to pci/endpoint!

- Mani

Quoted 31 lines

Changes since v1:

(quoted diff) +0 -4
-Picked up Reviewed-by tags.
-Fixed kdoc param name to match the actual param name in patch 1/4.
-Split patch "improve pci_epf_alloc_space()" into one patch changing
 pci-epf-core.c (patch 2/4 in V2) and pci-epf-test (patch 3/4 in V2).
-Perform the alignment even for fixed size BARs. We need this since:
 1) Some platforms have fixed_size_bars that are smaller than the
    iATU MIN REGION.
 2) No longer doing so would be a functional change and not a cleanup.

Kind regards,
Niklas

Niklas Cassel (4):
PCI: endpoint: refactor pci_epf_alloc_space()
PCI: endpoint: improve pci_epf_alloc_space()
PCI: endpoint: pci-epf-test: remove superfluous checks
PCI: endpoint: pci-epf-vntb: remove superfluous checks

 drivers/pci/endpoint/functions/pci-epf-ntb.c  |  2 +-
 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 ++-------------
 drivers/pci/endpoint/pci-epf-core.c           | 15 +++++++++++++--
 include/linux/pci-epf.h                       |  4 +++-
 5 files changed, 21 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்