Skip to contentSkip to conversation

mcb: enable register PCI devices using any BAR

PATCH In review Filip Jensen 2 participants 4 messages Mar 4 – Apr 24 Men chameleon bus (mcb)

lore
Share & tools
lore
#0 lore raw

v3 cover mcb: enable register PCI devices using any BAR

V2 -> V3: Thanks for second review. Fixed declaring pci_dev outside of the only block where it is needed in patch 1/2.
V1 -> V2: Thanks to the review from Johannes Thumshirn, the patch 2/3 of the first version has been removed and the cleaner solution from patch 3/3 is then kept.

The PCI mcb driver is currently only able to register devices that use the first PCI BAR. That is hardcoded in mcb-parse.c in lines 156-161:

                c = kzalloc_obj(struct chameleon_bar);
                if (!c)
                        return -ENOMEM;

                bar_count = 1;
                c->addr = mapbase;

However, mcb devices can use other indexes of the 6 PCI BARs. This serie of patches allows to use these devices by adding the relevant PCI BAR info to the chameleon table. As this code must also work for lpc devices, it has been done a refactoring in order to keep the two buses dependent operations regarding bars (get_bar and is_io_mapped_bar) outside of the common parsing code. These callback operations are stored in a structure and passed over to the parser, thus rendering each module, the mcb-lpc and mcb-pci, only with their own bus relevant logic.

This patch depends on the previously accepted patch:

Filip Jensen (2):
mcb: Use more than the first bar in pci devices
mcb: refactor bus operations out of common code

 drivers/mcb/mcb-internal.h | 10 ++++-
 drivers/mcb/mcb-lpc.c      | 56 ++++++++++++++++++++++++++-
 drivers/mcb/mcb-parse.c    | 77 +++++---------------------------------
 drivers/mcb/mcb-pci.c      | 40 +++++++++++++++++++-
 4 files changed, 111 insertions(+), 72 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 mcb: Use more than the first bar in pci devices

Currently The mcb driver in a PCI bus only uses the first BAR when parsing the table of attached devices and thus cannot map devices using other BARS («No BAR for 16z» error will be thrown in method chameleon_parse_gdd if assigned to a bar higher than the first).

This patch allows the parsing of devices in BARS other than the first using the standard PCI helpers for accessing PCI BAR info as described in pci.h

---
V2 -> V3: Move declaration of pci_dev inside the only block where it is needed in function chameleon_get_bar.
V1 -> V2: No changes in this patch

V2: https://lore.kernel.org/lkml/[email protected]/
V1: https://lore.kernel.org/lkml/[email protected]/

 drivers/mcb/mcb-internal.h |  3 +--
 drivers/mcb/mcb-lpc.c      |  2 +-
 drivers/mcb/mcb-parse.c    | 22 +++++++++++++---------
 drivers/mcb/mcb-pci.c      |  2 +-
 4 files changed, 16 insertions(+), 13 deletions(-)
drivers/mcb/mcb-internal.h +1 -2
index 3602cb3b2021..180ba97cfc08 100644
@@ -122,7 +122,6 @@ struct chameleon_bar {
 #define CHAMELEON_BAR_MAX	6
 #define BAR_DESC_SIZE(x)	((x) * sizeof(struct chameleon_bar) + sizeof(__le32))
 
-int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
-			  void __iomem *base);
+int chameleon_parse_cells(struct mcb_bus *bus, void __iomem *base);
 
 #endif
drivers/mcb/mcb-lpc.c +1 -1
index 070aa787abc6..d3f48e1ea08b 100644
@@ -56,7 +56,7 @@ static int mcb_lpc_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->bus))
 		return PTR_ERR(priv->bus);
 
-	ret = chameleon_parse_cells(priv->bus, priv->mem->start, priv->base);
+	ret = chameleon_parse_cells(priv->bus, priv->base);
 	if (ret < 0) {
 		goto out_mcb_bus;
 	}
drivers/mcb/mcb-parse.c +13 -9
index 2b115cb0e5d9..529ac7fe2ca8 100644
@@ -6,6 +6,7 @@
 #include <linux/io.h>
 #include <linux/mcb.h>
 
+#include <linux/pci.h>
 #include "mcb-internal.h"
 
 #define for_each_chameleon_cell(dtype, p)	\
@@ -123,8 +124,8 @@ static void chameleon_parse_bar(void __iomem *base,
 	}
 }
 
-static int chameleon_get_bar(void __iomem **base, phys_addr_t mapbase,
-			     struct chameleon_bar **cb)
+static int chameleon_get_bar(void __iomem **base, struct chameleon_bar **cb,
+			     struct device *dev)
 {
 	struct chameleon_bar *c;
 	int bar_count;
@@ -153,12 +154,16 @@ static int chameleon_get_bar(void __iomem **base, phys_addr_t mapbase,
 		chameleon_parse_bar(*base, c, bar_count);
 		*base += BAR_DESC_SIZE(bar_count);
 	} else {
-		c = kzalloc_obj(struct chameleon_bar);
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		bar_count = PCI_STD_NUM_BARS;
+		c = kzalloc_objs(struct chameleon_bar, bar_count);
 		if (!c)
 			return -ENOMEM;
-
-		bar_count = 1;
-		c->addr = mapbase;
+		for (int i = 0; i < bar_count; ++i) {
+			c[i].addr = pci_resource_start(pdev, i);
+			c[i].size = pci_resource_len(pdev, i);
+		}
 	}
 
 	*cb = c;
@@ -166,8 +171,7 @@ static int chameleon_get_bar(void __iomem **base, phys_addr_t mapbase,
 	return bar_count;
 }
 
-int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
-			void __iomem *base)
+int chameleon_parse_cells(struct mcb_bus *bus, void __iomem *base)
 {
 	struct chameleon_fpga_header *header;
 	struct chameleon_bar *cb;
@@ -203,7 +207,7 @@ int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
 	memcpy(bus->name, header->filename, CHAMELEON_FILENAME_LEN);
 	bus->name[CHAMELEON_FILENAME_LEN] = '\0';
 
-	bar_count = chameleon_get_bar(&p, mapbase, &cb);
+	bar_count = chameleon_get_bar(&p, &cb, bus->carrier);
 	if (bar_count < 0) {
 		ret = bar_count;
 		goto free_header;
drivers/mcb/mcb-pci.c +1 -1
index f1353da6ef4f..2401c19a8830 100644
@@ -86,7 +86,7 @@ static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	priv->bus->get_irq = mcb_pci_get_irq;
 
-	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
+	ret = chameleon_parse_cells(priv->bus, priv->base);
 	if (ret < 0)
 		goto out_mcb_bus;
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/2 mcb: refactor bus operations out of common code

In reply to Filip Jensen

Two operations can be identified that are bus dependent and are needed in both modules mcb-pci and mcb-lpc. In order to avoid having info of both buses in common code, two callbacks operations (is_bar_iomapped and get_bars) are defined and passed as parameters to the mcb-parser.

---
V2 -> V3: Adapt to line moved in patch V3 1/2
V1 -> V2: Patch V1 2/3 is removed as its changes were removed in V1 3/3. V2 has the refactor of the previous V1 3/3

V2: https://lore.kernel.org/lkml/[email protected]/
V1: https://lore.kernel.org/lkml/[email protected]/

 drivers/mcb/mcb-internal.h |  9 ++++-
 drivers/mcb/mcb-lpc.c      | 56 +++++++++++++++++++++++++-
 drivers/mcb/mcb-parse.c    | 81 +++++---------------------------------
 drivers/mcb/mcb-pci.c      | 40 ++++++++++++++++++-
 4 files changed, 111 insertions(+), 75 deletions(-)
drivers/mcb/mcb-internal.h +8 -1
index 180ba97cfc08..3152cb4bd1b5 100644
@@ -122,6 +122,13 @@ struct chameleon_bar {
 #define CHAMELEON_BAR_MAX	6
 #define BAR_DESC_SIZE(x)	((x) * sizeof(struct chameleon_bar) + sizeof(__le32))
 
-int chameleon_parse_cells(struct mcb_bus *bus, void __iomem *base);
+struct chameleon_parse_ops {
+	bool (*is_bar_iomapped)(struct device *dev, struct chameleon_bar *cb,
+				  int bar);
+	int (*get_bars)(void __iomem **base, struct chameleon_bar **cb,
+			struct device *dev);
+};
 
+int chameleon_parse_cells(struct mcb_bus *bus, void __iomem *base,
+			  struct chameleon_parse_ops *cham_bus_ops);
 #endif
drivers/mcb/mcb-lpc.c +55 -1

68 lines not on this page · raw message

drivers/mcb/mcb-parse.c +9 -72

117 lines not on this page · raw message

drivers/mcb/mcb-pci.c +39 -1
index 2401c19a8830..6245714646a7 100644
@@ -18,6 +18,44 @@ struct priv {
 	void __iomem *base;
 };
 
+static int pci_get_bars(void __iomem **base, struct chameleon_bar **cb,
+			struct device *dev)
+{
+	struct pci_dev *pdev;
+	struct chameleon_bar *c;
+	int bar_count;
+
+	pdev = to_pci_dev(dev);
+	bar_count = PCI_STD_NUM_BARS;
+	c = kzalloc_objs(struct chameleon_bar, bar_count);
+	if (!c)
+		return -ENOMEM;
+	for (int i = 0; i < bar_count; ++i) {
+		c[i].addr = pci_resource_start(pdev, i);
+		c[i].size = pci_resource_len(pdev, i);
+	}
+
+	*cb = c;
+
+	return bar_count;
+}
+
+static bool is_pci_bar_iomapped(struct device *dev, struct chameleon_bar *cb,
+				  int bar)
+{
+	struct pci_dev *pdev;
+
+	pdev = to_pci_dev(dev);
+	if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
+		return true;
+	return false;
+}
+
+static struct chameleon_parse_ops cham_pci_ops = {
+	.is_bar_iomapped = is_pci_bar_iomapped,
+	.get_bars = pci_get_bars,
+};
+
 static int mcb_pci_get_irq(struct mcb_device *mdev)
 {
 	struct mcb_bus *mbus = mdev->bus;
@@ -86,7 +124,7 @@ static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	priv->bus->get_irq = mcb_pci_get_irq;
 
-	ret = chameleon_parse_cells(priv->bus, priv->base);
+	ret = chameleon_parse_cells(priv->bus, priv->base, &cham_pci_ops);
 	if (ret < 0)
 		goto out_mcb_bus;
 
Signature · 1 line
2.34.1
#3 lore raw
In reply to Filip Jensen
Quoted 25 lines · Filip Jensen

V2 -> V3: Thanks for second review. Fixed declaring pci_dev outside of the only block where it is needed in patch 1/2.
V1 -> V2: Thanks to the review from Johannes Thumshirn, the patch 2/3 of the first version has been removed and the cleaner solution from patch 3/3 is then kept.

The PCI mcb driver is currently only able to register devices that use the first PCI BAR. That is hardcoded in mcb-parse.c in lines 156-161:

                 c = kzalloc_obj(struct chameleon_bar);
                 if (!c)
                         return -ENOMEM;

                 bar_count = 1;
                 c->addr = mapbase;

However, mcb devices can use other indexes of the 6 PCI BARs. This serie of patches allows to use these devices by adding the relevant PCI BAR info to the chameleon table. As this code must also work for lpc devices, it has been done a refactoring in order to keep the two buses dependent operations regarding bars (get_bar and is_io_mapped_bar) outside of the common parsing code. These callback operations are stored in a structure and passed over to the parser, thus rendering each module, the mcb-lpc and mcb-pci, only with their own bus relevant logic.

Quoted 4 lines

This patch depends on the previously accepted patch:

Added to mcb-for-next.

Sorry for the delay.

Byte,

    Johannes