8163715
From 5c4f8b5b68451e5d208a5aefb195fdd108629da4 Mon Sep 17 00:00:00 2001
8163715
From: Tomasz Nowicki <tn@semihalf.com>
8163715
Date: Fri, 9 Sep 2016 21:24:03 +0200
8163715
Subject: [PATCH 1/6] PCI/ACPI: Extend pci_mcfg_lookup() responsibilities
8163715
8163715
In preparation for adding MCFG platform specific quirk handling move
8163715
CFG resource calculation and ECAM ops assignment to pci_mcfg_lookup().
8163715
It becomes the gate for further ops and CFG resource manipulation
8163715
in arch-agnostic code (drivers/acpi/pci_mcfg.c).
8163715
8163715
No functionality changes in this patch.
8163715
8163715
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
8163715
---
8163715
 arch/arm64/kernel/pci.c  | 17 +++++------------
8163715
 drivers/acpi/pci_mcfg.c  | 28 +++++++++++++++++++++++++---
8163715
 include/linux/pci-acpi.h |  4 +++-
8163715
 3 files changed, 33 insertions(+), 16 deletions(-)
8163715
8163715
diff --git a/arch/arm64/kernel/pci.c b/arch/arm64/kernel/pci.c
8163715
index acf3872..fb439c7 100644
8163715
--- a/arch/arm64/kernel/pci.c
8163715
+++ b/arch/arm64/kernel/pci.c
8163715
@@ -125,24 +125,17 @@ pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
8163715
 	u16 seg = root->segment;
8163715
 	struct pci_config_window *cfg;
8163715
 	struct resource cfgres;
8163715
-	unsigned int bsz;
8163715
+	struct pci_ecam_ops *ecam_ops;
8163715
+	int ret;
8163715
 
8163715
-	/* Use address from _CBA if present, otherwise lookup MCFG */
8163715
-	if (!root->mcfg_addr)
8163715
-		root->mcfg_addr = pci_mcfg_lookup(seg, bus_res);
8163715
-
8163715
-	if (!root->mcfg_addr) {
8163715
+	ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
8163715
+	if (ret) {
8163715
 		dev_err(&root->device->dev, "%04x:%pR ECAM region not found\n",
8163715
 			seg, bus_res);
8163715
 		return NULL;
8163715
 	}
8163715
 
8163715
-	bsz = 1 << pci_generic_ecam_ops.bus_shift;
8163715
-	cfgres.start = root->mcfg_addr + bus_res->start * bsz;
8163715
-	cfgres.end = cfgres.start + resource_size(bus_res) * bsz - 1;
8163715
-	cfgres.flags = IORESOURCE_MEM;
8163715
-	cfg = pci_ecam_create(&root->device->dev, &cfgres, bus_res,
8163715
-			      &pci_generic_ecam_ops);
8163715
+	cfg = pci_ecam_create(&root->device->dev, &cfgres, bus_res, ecam_ops);
8163715
 	if (IS_ERR(cfg)) {
8163715
 		dev_err(&root->device->dev, "%04x:%pR error %ld mapping ECAM\n",
8163715
 			seg, bus_res, PTR_ERR(cfg));
8163715
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
8163715
index b5b376e..ffcc651 100644
8163715
--- a/drivers/acpi/pci_mcfg.c
8163715
+++ b/drivers/acpi/pci_mcfg.c
8163715
@@ -22,6 +22,7 @@
8163715
 #include <linux/kernel.h>
8163715
 #include <linux/pci.h>
8163715
 #include <linux/pci-acpi.h>
8163715
+#include <linux/pci-ecam.h>
8163715
 
8163715
 /* Structure to hold entries from the MCFG table */
8163715
 struct mcfg_entry {
8163715
@@ -35,9 +36,18 @@ struct mcfg_entry {
8163715
 /* List to save MCFG entries */
8163715
 static LIST_HEAD(pci_mcfg_list);
8163715
 
8163715
-phys_addr_t pci_mcfg_lookup(u16 seg, struct resource *bus_res)
8163715
+int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
8163715
+		    struct pci_ecam_ops **ecam_ops)
8163715
 {
8163715
+	struct pci_ecam_ops *ops = &pci_generic_ecam_ops;
8163715
+	struct resource *bus_res = &root->secondary;
8163715
+	u16 seg = root->segment;
8163715
 	struct mcfg_entry *e;
8163715
+	struct resource res;
8163715
+
8163715
+	/* Use address from _CBA if present, otherwise lookup MCFG */
8163715
+	if (root->mcfg_addr)
8163715
+		goto skip_lookup;
8163715
 
8163715
 	/*
8163715
 	 * We expect exact match, unless MCFG entry end bus covers more than
8163715
@@ -45,10 +55,22 @@ phys_addr_t pci_mcfg_lookup(u16 seg, struct resource *bus_res)
8163715
 	 */
8163715
 	list_for_each_entry(e, &pci_mcfg_list, list) {
8163715
 		if (e->segment == seg && e->bus_start == bus_res->start &&
8163715
-		    e->bus_end >= bus_res->end)
8163715
-			return e->addr;
8163715
+		    e->bus_end >= bus_res->end) {
8163715
+			root->mcfg_addr = e->addr;
8163715
+		}
8163715
+
8163715
 	}
8163715
 
8163715
+	if (!root->mcfg_addr)
8163715
+		return -ENXIO;
8163715
+
8163715
+skip_lookup:
8163715
+	memset(&res, 0, sizeof(res));
8163715
+	res.start = root->mcfg_addr + (bus_res->start << 20);
8163715
+	res.end = res.start + (resource_size(bus_res) << 20) - 1;
8163715
+	res.flags = IORESOURCE_MEM;
8163715
+	*cfgres = res;
8163715
+	*ecam_ops = ops;
8163715
 	return 0;
8163715
 }
8163715
 
8163715
diff --git a/include/linux/pci-acpi.h b/include/linux/pci-acpi.h
8163715
index 7d63a66..7a4e83a 100644
8163715
--- a/include/linux/pci-acpi.h
8163715
+++ b/include/linux/pci-acpi.h
8163715
@@ -24,7 +24,9 @@ static inline acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
8163715
 }
8163715
 extern phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle);
8163715
 
8163715
-extern phys_addr_t pci_mcfg_lookup(u16 domain, struct resource *bus_res);
8163715
+struct pci_ecam_ops;
8163715
+extern int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
8163715
+			   struct pci_ecam_ops **ecam_ops);
8163715
 
8163715
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
8163715
 {
8163715
-- 
8163715
2.9.3
8163715
8163715
From 16c02d9cc0e67b48c343aecc4b5566e729a97683 Mon Sep 17 00:00:00 2001
8163715
From: Tomasz Nowicki <tn@semihalf.com>
8163715
Date: Fri, 9 Sep 2016 21:24:04 +0200
8163715
Subject: [PATCH 2/6] PCI/ACPI: Check platform specific ECAM quirks
8163715
8163715
Some platforms may not be fully compliant with generic set of PCI config
8163715
accessors. For these cases we implement the way to overwrite CFG accessors
8163715
set and configuration space range.
8163715
8163715
In first place pci_mcfg_parse() saves machine's IDs and revision number
8163715
(these come from MCFG header) in order to match against known quirk entries.
8163715
Then the algorithm traverses available quirk list (static array),
8163715
matches against <oem_id, oem_table_id, rev, domain, bus number range> and
8163715
returns custom PCI config ops and/or CFG resource structure.
8163715
8163715
When adding new quirk there are two possibilities:
8163715
1. Override default pci_generic_ecam_ops ops but CFG resource comes from MCFG
8163715
{ "OEM_ID", "OEM_TABLE_ID", <REV>, <DOMAIN>, <BUS_NR>, &foo_ops, MCFG_RES_EMPTY },
8163715
2. Override default pci_generic_ecam_ops ops and CFG resource. For this case
8163715
it is also allowed get CFG resource from quirk entry w/o having it in MCFG.
8163715
{ "OEM_ID", "OEM_TABLE_ID", <REV>, <DOMAIN>, <BUS_NR>, &boo_ops,
8163715
  DEFINE_RES_MEM(START, SIZE) },
8163715
8163715
pci_generic_ecam_ops and MCFG entries will be used for platforms
8163715
free from quirks.
8163715
8163715
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
8163715
Signed-off-by: Dongdong Liu <liudongdong3@huawei.com>
8163715
Signed-off-by: Christopher Covington <cov@codeaurora.org>
8163715
---
8163715
 drivers/acpi/pci_mcfg.c | 80 +++++++++++++++++++++++++++++++++++++++++++++----
8163715
 1 file changed, 74 insertions(+), 6 deletions(-)
8163715
8163715
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
8163715
index ffcc651..2b8acc7 100644
8163715
--- a/drivers/acpi/pci_mcfg.c
8163715
+++ b/drivers/acpi/pci_mcfg.c
8163715
@@ -32,6 +32,59 @@ struct mcfg_entry {
8163715
 	u8			bus_start;
8163715
 	u8			bus_end;
8163715
 };
8163715
+struct mcfg_fixup {
8163715
+	char oem_id[ACPI_OEM_ID_SIZE + 1];
8163715
+	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
8163715
+	u32 oem_revision;
8163715
+	u16 seg;
8163715
+	struct resource bus_range;
8163715
+	struct pci_ecam_ops *ops;
8163715
+	struct resource cfgres;
8163715
+};
8163715
+
8163715
+#define MCFG_DOM_ANY			(-1)
8163715
+#define MCFG_BUS_RANGE(start, end)	DEFINE_RES_NAMED((start),	\
8163715
+						((end) - (start) + 1),	\
8163715
+						NULL, IORESOURCE_BUS)
8163715
+#define MCFG_BUS_ANY		MCFG_BUS_RANGE(0x0, 0xff)
8163715
+#define MCFG_RES_EMPTY		DEFINE_RES_NAMED(0, 0, NULL, 0)
8163715
+
8163715
+static struct mcfg_fixup mcfg_quirks[] = {
8163715
+/*	{ OEM_ID, OEM_TABLE_ID, REV, DOMAIN, BUS_RANGE, cfgres, ops }, */
8163715
+};
8163715
+
8163715
+static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
8163715
+static char mcfg_oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
8163715
+static u32 mcfg_oem_revision;
8163715
+
8163715
+static void pci_mcfg_match_quirks(struct acpi_pci_root *root,
8163715
+				  struct resource *cfgres,
8163715
+				  struct pci_ecam_ops **ecam_ops)
8163715
+{
8163715
+	struct mcfg_fixup *f;
8163715
+	int i;
8163715
+
8163715
+	/*
8163715
+	 * First match against PCI topology <domain:bus> then use OEM ID, OEM
8163715
+	 * table ID, and OEM revision from MCFG table standard header.
8163715
+	 */
8163715
+	for (i = 0, f = mcfg_quirks; i < ARRAY_SIZE(mcfg_quirks); i++, f++) {
8163715
+		if (f->seg == root->segment &&
8163715
+		    resource_contains(&f->bus_range, &root->secondary) &&
8163715
+		    !memcmp(f->oem_id, mcfg_oem_id, ACPI_OEM_ID_SIZE) &&
8163715
+		    !memcmp(f->oem_table_id, mcfg_oem_table_id,
8163715
+		            ACPI_OEM_TABLE_ID_SIZE) &&
8163715
+		    f->oem_revision == mcfg_oem_revision) {
8163715
+			if (f->cfgres.start)
8163715
+				*cfgres = f->cfgres;
8163715
+			if (f->ops)
8163715
+				*ecam_ops =  f->ops;
8163715
+			dev_info(&root->device->dev, "Applying PCI MCFG quirks for %s %s rev: %d\n",
8163715
+				 f->oem_id, f->oem_table_id, f->oem_revision);
8163715
+			return;
8163715
+		}
8163715
+	}
8163715
+}
8163715
 
8163715
 /* List to save MCFG entries */
8163715
 static LIST_HEAD(pci_mcfg_list);
8163715
@@ -61,14 +114,24 @@ int pci_mcfg_lookup(struct acpi_pci_root *root, struct resource *cfgres,
8163715
 
8163715
 	}
8163715
 
8163715
-	if (!root->mcfg_addr)
8163715
-		return -ENXIO;
8163715
-
8163715
 skip_lookup:
8163715
 	memset(&res, 0, sizeof(res));
8163715
-	res.start = root->mcfg_addr + (bus_res->start << 20);
8163715
-	res.end = res.start + (resource_size(bus_res) << 20) - 1;
8163715
-	res.flags = IORESOURCE_MEM;
8163715
+	if (root->mcfg_addr) {
8163715
+		res.start = root->mcfg_addr + (bus_res->start << 20);
8163715
+		res.end = res.start + (resource_size(bus_res) << 20) - 1;
8163715
+		res.flags = IORESOURCE_MEM;
8163715
+	}
8163715
+
8163715
+	/*
8163715
+	 * Let to override default ECAM ops and CFG resource range.
8163715
+	 * Also, this might even retrieve CFG resource range in case MCFG
8163715
+	 * does not have it. Invalid CFG start address means MCFG firmware bug
8163715
+	 * or we need another quirk in array.
8163715
+	 */
8163715
+	pci_mcfg_match_quirks(root, &res, &ops;;
8163715
+	if (!res.start)
8163715
+		return -ENXIO;
8163715
+
8163715
 	*cfgres = res;
8163715
 	*ecam_ops = ops;
8163715
 	return 0;
8163715
@@ -101,6 +164,11 @@ static __init int pci_mcfg_parse(struct acpi_table_header *header)
8163715
 		list_add(&e->list, &pci_mcfg_list);
8163715
 	}
8163715
 
8163715
+	/* Save MCFG IDs and revision for quirks matching */
8163715
+	memcpy(mcfg_oem_id, header->oem_id, ACPI_OEM_ID_SIZE);
8163715
+	memcpy(mcfg_oem_table_id, header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
8163715
+	mcfg_oem_revision = header->revision;
8163715
+
8163715
 	pr_info("MCFG table detected, %d entries\n", n);
8163715
 	return 0;
8163715
 }
8163715
-- 
8163715
2.9.3
8163715
8163715
From 2243ab64c12a873e47b72c8e636b40ed09c5f0d4 Mon Sep 17 00:00:00 2001
8163715
From: Tomasz Nowicki <tn@semihalf.com>
8163715
Date: Fri, 9 Sep 2016 21:24:05 +0200
8163715
Subject: [PATCH 3/6] PCI: thunder-pem: Allow to probe PEM-specific register
8163715
 range for ACPI case
8163715
8163715
thunder-pem driver stands for being ACPI based PCI host controller.
8163715
However, there is no standard way to describe its PEM-specific register
8163715
ranges in ACPI tables. Thus we add thunder_pem_init() ACPI extension
8163715
to obtain hardcoded addresses from static resource array.
8163715
Although it is not pretty, it prevents from creating standard mechanism to
8163715
handle similar cases in future.
8163715
8163715
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
8163715
---
8163715
 drivers/pci/host/pci-thunder-pem.c | 61 ++++++++++++++++++++++++++++++--------
8163715
 1 file changed, 48 insertions(+), 13 deletions(-)
8163715
8163715
diff --git a/drivers/pci/host/pci-thunder-pem.c b/drivers/pci/host/pci-thunder-pem.c
8163715
index 6abaf80..b048761 100644
8163715
--- a/drivers/pci/host/pci-thunder-pem.c
8163715
+++ b/drivers/pci/host/pci-thunder-pem.c
8163715
@@ -18,6 +18,7 @@
8163715
 #include <linux/init.h>
8163715
 #include <linux/of_address.h>
8163715
 #include <linux/of_pci.h>
8163715
+#include <linux/pci-acpi.h>
8163715
 #include <linux/pci-ecam.h>
8163715
 #include <linux/platform_device.h>
8163715
 
8163715
@@ -284,6 +285,40 @@ static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
8163715
 	return pci_generic_config_write(bus, devfn, where, size, val);
8163715
 }
8163715
 
8163715
+#ifdef CONFIG_ACPI
8163715
+static struct resource thunder_pem_reg_res[] = {
8163715
+	[4] = DEFINE_RES_MEM(0x87e0c0000000UL, SZ_16M),
8163715
+	[5] = DEFINE_RES_MEM(0x87e0c1000000UL, SZ_16M),
8163715
+	[6] = DEFINE_RES_MEM(0x87e0c2000000UL, SZ_16M),
8163715
+	[7] = DEFINE_RES_MEM(0x87e0c3000000UL, SZ_16M),
8163715
+	[8] = DEFINE_RES_MEM(0x87e0c4000000UL, SZ_16M),
8163715
+	[9] = DEFINE_RES_MEM(0x87e0c5000000UL, SZ_16M),
8163715
+	[14] = DEFINE_RES_MEM(0x97e0c0000000UL, SZ_16M),
8163715
+	[15] = DEFINE_RES_MEM(0x97e0c1000000UL, SZ_16M),
8163715
+	[16] = DEFINE_RES_MEM(0x97e0c2000000UL, SZ_16M),
8163715
+	[17] = DEFINE_RES_MEM(0x97e0c3000000UL, SZ_16M),
8163715
+	[18] = DEFINE_RES_MEM(0x97e0c4000000UL, SZ_16M),
8163715
+	[19] = DEFINE_RES_MEM(0x97e0c5000000UL, SZ_16M),
8163715
+};
8163715
+
8163715
+static struct resource *thunder_pem_acpi_res(struct pci_config_window *cfg)
8163715
+{
8163715
+	struct acpi_device *adev = to_acpi_device(cfg->parent);
8163715
+	struct acpi_pci_root *root = acpi_driver_data(adev);
8163715
+
8163715
+	if ((root->segment >= 4 && root->segment <= 9) ||
8163715
+	    (root->segment >= 14 && root->segment <= 19))
8163715
+		return &thunder_pem_reg_res[root->segment];
8163715
+
8163715
+	return NULL;
8163715
+}
8163715
+#else
8163715
+static struct resource *thunder_pem_acpi_res(struct pci_config_window *cfg)
8163715
+{
8163715
+	return NULL;
8163715
+}
8163715
+#endif
8163715
+
8163715
 static int thunder_pem_init(struct pci_config_window *cfg)
8163715
 {
8163715
 	struct device *dev = cfg->parent;
8163715
@@ -292,24 +327,24 @@ static int thunder_pem_init(struct pci_config_window *cfg)
8163715
 	struct thunder_pem_pci *pem_pci;
8163715
 	struct platform_device *pdev;
8163715
 
8163715
-	/* Only OF support for now */
8163715
-	if (!dev->of_node)
8163715
-		return -EINVAL;
8163715
-
8163715
 	pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
8163715
 	if (!pem_pci)
8163715
 		return -ENOMEM;
8163715
 
8163715
-	pdev = to_platform_device(dev);
8163715
-
8163715
-	/*
8163715
-	 * The second register range is the PEM bridge to the PCIe
8163715
-	 * bus.  It has a different config access method than those
8163715
-	 * devices behind the bridge.
8163715
-	 */
8163715
-	res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8163715
+	if (acpi_disabled) {
8163715
+		pdev = to_platform_device(dev);
8163715
+
8163715
+		/*
8163715
+		 * The second register range is the PEM bridge to the PCIe
8163715
+		 * bus.  It has a different config access method than those
8163715
+		 * devices behind the bridge.
8163715
+		 */
8163715
+		res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8163715
+	} else {
8163715
+		res_pem = thunder_pem_acpi_res(cfg);
8163715
+	}
8163715
 	if (!res_pem) {
8163715
-		dev_err(dev, "missing \"reg[1]\"property\n");
8163715
+		dev_err(dev, "missing configuration region\n");
8163715
 		return -EINVAL;
8163715
 	}
8163715
 
8163715
-- 
8163715
2.9.3
8163715
8163715
From 443d85d47ee00b3f0b6f39d470a11e7eb116817d Mon Sep 17 00:00:00 2001
8163715
From: Tomasz Nowicki <tn@semihalf.com>
8163715
Date: Fri, 9 Sep 2016 21:24:06 +0200
8163715
Subject: [PATCH 4/6] PCI: thunder: Enable ACPI PCI controller for ThunderX
8163715
 pass2.x silicon version
8163715
8163715
ThunderX PCIe controller to off-chip devices (so-called PEM) is not fully
8163715
compliant with ECAM standard. It uses non-standard configuration space
8163715
accessors (see pci_thunder_pem_ops) and custom configuration space granulation
8163715
(see bus_shift = 24). In order to access configuration space and
8163715
probe PEM as ACPI based PCI host controller we need to add MCFG quirk
8163715
infrastructure. This involves:
8163715
1. Export PEM pci_thunder_pem_ops structure so it is visible to MCFG quirk
8163715
   code.
8163715
2. New quirk entries for each PEM segment. Each contains platform IDs,
8163715
   mentioned pci_thunder_pem_ops and CFG resources.
8163715
8163715
Quirk is considered for ThunderX silicon pass2.x only which is identified
8163715
via MCFG revision 1.
8163715
8163715
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
8163715
---
8163715
 drivers/acpi/pci_mcfg.c            | 27 +++++++++++++++++++++++++++
8163715
 drivers/pci/host/pci-thunder-pem.c |  2 +-
8163715
 include/linux/pci-ecam.h           |  4 ++++
8163715
 3 files changed, 32 insertions(+), 1 deletion(-)
8163715
8163715
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
8163715
index 2b8acc7..1f73d7b 100644
8163715
--- a/drivers/acpi/pci_mcfg.c
8163715
+++ b/drivers/acpi/pci_mcfg.c
8163715
@@ -51,6 +51,33 @@ struct mcfg_fixup {
8163715
 
8163715
 static struct mcfg_fixup mcfg_quirks[] = {
8163715
 /*	{ OEM_ID, OEM_TABLE_ID, REV, DOMAIN, BUS_RANGE, cfgres, ops }, */
8163715
+#ifdef CONFIG_PCI_HOST_THUNDER_PEM
8163715
+	/* SoC pass2.x */
8163715
+	{ "CAVIUM", "THUNDERX", 1, 4, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x88001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 5, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x884057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 6, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x88808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 7, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x89001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 8, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x894057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 9, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x89808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 14, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x98001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 15, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x984057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 16, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x98808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 17, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x99001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 18, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x994057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 1, 19, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x99808f000000UL, 0x39 * SZ_16M) },
8163715
+#endif
8163715
 };
8163715
 
8163715
 static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
8163715
diff --git a/drivers/pci/host/pci-thunder-pem.c b/drivers/pci/host/pci-thunder-pem.c
8163715
index b048761..d7c10cc 100644
8163715
--- a/drivers/pci/host/pci-thunder-pem.c
8163715
+++ b/drivers/pci/host/pci-thunder-pem.c
8163715
@@ -367,7 +367,7 @@ static int thunder_pem_init(struct pci_config_window *cfg)
8163715
 	return 0;
8163715
 }
8163715
 
8163715
-static struct pci_ecam_ops pci_thunder_pem_ops = {
8163715
+struct pci_ecam_ops pci_thunder_pem_ops = {
8163715
 	.bus_shift	= 24,
8163715
 	.init		= thunder_pem_init,
8163715
 	.pci_ops	= {
8163715
diff --git a/include/linux/pci-ecam.h b/include/linux/pci-ecam.h
8163715
index 7adad20..65505ea 100644
8163715
--- a/include/linux/pci-ecam.h
8163715
+++ b/include/linux/pci-ecam.h
8163715
@@ -58,6 +58,10 @@ void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
8163715
 			       int where);
8163715
 /* default ECAM ops */
8163715
 extern struct pci_ecam_ops pci_generic_ecam_ops;
8163715
+/* ECAM ops for known quirks */
8163715
+#ifdef CONFIG_PCI_HOST_THUNDER_PEM
8163715
+extern struct pci_ecam_ops pci_thunder_pem_ops;
8163715
+#endif
8163715
 
8163715
 #ifdef CONFIG_PCI_HOST_GENERIC
8163715
 /* for DT-based PCI controllers that support ECAM */
8163715
-- 
8163715
2.9.3
8163715
8163715
From 6eca99cc392a11bb07b9ef88bca71a85f8bbe273 Mon Sep 17 00:00:00 2001
8163715
From: Tomasz Nowicki <tn@semihalf.com>
8163715
Date: Fri, 9 Sep 2016 21:24:07 +0200
8163715
Subject: [PATCH 5/6] PCI: thunder: Enable ACPI PCI controller for ThunderX
8163715
 pass1.x silicon version
8163715
8163715
ThunderX pass1.x requires to emulate the EA headers for on-chip devices
8163715
hence it has to use custom pci_thunder_ecam_ops for accessing PCI config
8163715
space (pci-thuner-ecam.c). Add new entries to MCFG quirk array where they
8163715
can be applied while probing ACPI based PCI host controller.
8163715
8163715
ThunderX pass1.x is using the same way for accessing off-chip devices
8163715
(so-called PEM) as silicon pass-2.x so we need to add PEM quirk
8163715
entries too.
8163715
8163715
Quirk is considered for ThunderX silicon pass1.x only which is identified
8163715
via MCFG revision 2.
8163715
8163715
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
8163715
---
8163715
 drivers/acpi/pci_mcfg.c             | 45 +++++++++++++++++++++++++++++++++++++
8163715
 drivers/pci/host/pci-thunder-ecam.c |  2 +-
8163715
 include/linux/pci-ecam.h            |  3 +++
8163715
 3 files changed, 49 insertions(+), 1 deletion(-)
8163715
8163715
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
8163715
index 1f73d7b..eb14f74 100644
8163715
--- a/drivers/acpi/pci_mcfg.c
8163715
+++ b/drivers/acpi/pci_mcfg.c
8163715
@@ -77,6 +77,51 @@ static struct mcfg_fixup mcfg_quirks[] = {
8163715
 	  DEFINE_RES_MEM(0x994057000000UL, 0x39 * SZ_16M) },
8163715
 	{ "CAVIUM", "THUNDERX", 1, 19, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
 	  DEFINE_RES_MEM(0x99808f000000UL, 0x39 * SZ_16M) },
8163715
+
8163715
+	/* SoC pass1.x */
8163715
+	{ "CAVIUM", "THUNDERX", 2, 4, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x88001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 5, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x884057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 6, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x88808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 7, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x89001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 8, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x894057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 9, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x89808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 14, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x98001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 15, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x984057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 16, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x98808f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 17, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x99001f000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 18, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x994057000000UL, 0x39 * SZ_16M) },
8163715
+	{ "CAVIUM", "THUNDERX", 2, 19, MCFG_BUS_ANY, &pci_thunder_pem_ops,
8163715
+	  DEFINE_RES_MEM(0x99808f000000UL, 0x39 * SZ_16M) },
8163715
+#endif
8163715
+#ifdef CONFIG_PCI_HOST_THUNDER_ECAM
8163715
+	/* SoC pass1.x */
8163715
+	{ "CAVIUM", "THUNDERX", 2, 0, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 1, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 2, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 3, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 10, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 11, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 12, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
+	{ "CAVIUM", "THUNDERX", 2, 13, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
+	  MCFG_RES_EMPTY},
8163715
 #endif
8163715
 };
8163715
 
8163715
diff --git a/drivers/pci/host/pci-thunder-ecam.c b/drivers/pci/host/pci-thunder-ecam.c
8163715
index d50a3dc..b6c17e2 100644
8163715
--- a/drivers/pci/host/pci-thunder-ecam.c
8163715
+++ b/drivers/pci/host/pci-thunder-ecam.c
8163715
@@ -346,7 +346,7 @@ static int thunder_ecam_config_write(struct pci_bus *bus, unsigned int devfn,
8163715
 	return pci_generic_config_write(bus, devfn, where, size, val);
8163715
 }
8163715
 
8163715
-static struct pci_ecam_ops pci_thunder_ecam_ops = {
8163715
+struct pci_ecam_ops pci_thunder_ecam_ops = {
8163715
 	.bus_shift	= 20,
8163715
 	.pci_ops	= {
8163715
 		.map_bus        = pci_ecam_map_bus,
8163715
diff --git a/include/linux/pci-ecam.h b/include/linux/pci-ecam.h
8163715
index 65505ea..35f0e81 100644
8163715
--- a/include/linux/pci-ecam.h
8163715
+++ b/include/linux/pci-ecam.h
8163715
@@ -62,6 +62,9 @@ extern struct pci_ecam_ops pci_generic_ecam_ops;
8163715
 #ifdef CONFIG_PCI_HOST_THUNDER_PEM
8163715
 extern struct pci_ecam_ops pci_thunder_pem_ops;
8163715
 #endif
8163715
+#ifdef CONFIG_PCI_HOST_THUNDER_ECAM
8163715
+extern struct pci_ecam_ops pci_thunder_ecam_ops;
8163715
+#endif
8163715
 
8163715
 #ifdef CONFIG_PCI_HOST_GENERIC
8163715
 /* for DT-based PCI controllers that support ECAM */
8163715
-- 
8163715
2.9.3
8163715
8163715
From 3080ac5bb527155ccdf8490ce221b1c6ad01f502 Mon Sep 17 00:00:00 2001
8163715
From: Duc Dang <dhdang@apm.com>
8163715
Date: Sat, 17 Sep 2016 07:24:38 -0700
8163715
Subject: [PATCH 6/6] PCI/ACPI: xgene: Add ECAM quirk for X-Gene PCIe
8163715
 controller
8163715
8163715
PCIe controller in X-Gene SoCs is not ECAM compliant: software
8163715
needs to configure additional concontroller register to address
8163715
device at bus:dev:function.
8163715
8163715
This patch depends on "ECAM quirks handling for ARM64 platforms"
8163715
series (http://www.spinics.net/lists/arm-kernel/msg530692.html)
8163715
to address the limitation above for X-Gene PCIe controller.
8163715
8163715
The quirk will only be applied for X-Gene PCIe MCFG table with
8163715
OEM revison 1, 2, 3 or 4 (PCIe controller v1 and v2 on X-Gene SoCs).
8163715
8163715
Signed-off-by: Duc Dang <dhdang@apm.com>
8163715
---
8163715
 drivers/acpi/pci_mcfg.c           |  32 +++++
8163715
 drivers/pci/host/Makefile         |   2 +-
8163715
 drivers/pci/host/pci-xgene-ecam.c | 280 ++++++++++++++++++++++++++++++++++++++
8163715
 include/linux/pci-ecam.h          |   5 +
8163715
 4 files changed, 318 insertions(+), 1 deletion(-)
8163715
 create mode 100644 drivers/pci/host/pci-xgene-ecam.c
8163715
8163715
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
8163715
index eb14f74..635ab24 100644
8163715
--- a/drivers/acpi/pci_mcfg.c
8163715
+++ b/drivers/acpi/pci_mcfg.c
8163715
@@ -123,6 +123,38 @@ static struct mcfg_fixup mcfg_quirks[] = {
8163715
 	{ "CAVIUM", "THUNDERX", 2, 13, MCFG_BUS_ANY, &pci_thunder_ecam_ops,
8163715
 	  MCFG_RES_EMPTY},
8163715
 #endif
8163715
+#ifdef CONFIG_PCI_XGENE
8163715
+	{"APM   ", "XGENE   ", 1, 0, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 1, 1, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 1, 2, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 1, 3, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 1, 4, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 2, 0, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 2, 1, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 2, 2, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 2, 3, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 2, 4, MCFG_BUS_ANY,
8163715
+		&xgene_v1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 3, 0, MCFG_BUS_ANY,
8163715
+		&xgene_v2_1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 3, 1, MCFG_BUS_ANY,
8163715
+		&xgene_v2_1_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 4, 0, MCFG_BUS_ANY,
8163715
+		&xgene_v2_2_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 4, 1, MCFG_BUS_ANY,
8163715
+		&xgene_v2_2_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+	{"APM   ", "XGENE   ", 4, 2, MCFG_BUS_ANY,
8163715
+		&xgene_v2_2_pcie_ecam_ops, MCFG_RES_EMPTY},
8163715
+#endif
8163715
 };
8163715
 
8163715
 static char mcfg_oem_id[ACPI_OEM_ID_SIZE];
8163715
diff --git a/drivers/pci/host/Makefile b/drivers/pci/host/Makefile
8163715
index 8843410..af4f505 100644
8163715
--- a/drivers/pci/host/Makefile
8163715
+++ b/drivers/pci/host/Makefile
8163715
@@ -15,7 +15,7 @@ obj-$(CONFIG_PCIE_SPEAR13XX) += pcie-spear13xx.o
8163715
 obj-$(CONFIG_PCI_KEYSTONE) += pci-keystone-dw.o pci-keystone.o
8163715
 obj-$(CONFIG_PCIE_XILINX) += pcie-xilinx.o
8163715
 obj-$(CONFIG_PCIE_XILINX_NWL) += pcie-xilinx-nwl.o
8163715
-obj-$(CONFIG_PCI_XGENE) += pci-xgene.o
8163715
+obj-$(CONFIG_PCI_XGENE) += pci-xgene.o pci-xgene-ecam.o
8163715
 obj-$(CONFIG_PCI_XGENE_MSI) += pci-xgene-msi.o
8163715
 obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o
8163715
 obj-$(CONFIG_PCI_VERSATILE) += pci-versatile.o
8163715
diff --git a/drivers/pci/host/pci-xgene-ecam.c b/drivers/pci/host/pci-xgene-ecam.c
8163715
new file mode 100644
8163715
index 0000000..b66a04f
8163715
--- /dev/null
8163715
+++ b/drivers/pci/host/pci-xgene-ecam.c
8163715
@@ -0,0 +1,280 @@
8163715
+/*
8163715
+ * APM X-Gene PCIe ECAM fixup driver
8163715
+ *
8163715
+ * Copyright (c) 2016, Applied Micro Circuits Corporation
8163715
+ * Author:
8163715
+ *	Duc Dang <dhdang@apm.com>
8163715
+ *
8163715
+ * This program is free software; you can redistribute it and/or modify
8163715
+ * it under the terms of the GNU General Public License version 2 as
8163715
+ * published by the Free Software Foundation.
8163715
+ *
8163715
+ * This program is distributed in the hope that it will be useful,
8163715
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8163715
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8163715
+ * GNU General Public License for more details.
8163715
+ *
8163715
+ * You should have received a copy of the GNU General Public License
8163715
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8163715
+ */
8163715
+
8163715
+#include <linux/kernel.h>
8163715
+#include <linux/module.h>
8163715
+#include <linux/of_address.h>
8163715
+#include <linux/of_pci.h>
8163715
+#include <linux/pci-acpi.h>
8163715
+#include <linux/platform_device.h>
8163715
+#include <linux/pci-ecam.h>
8163715
+
8163715
+#ifdef CONFIG_ACPI
8163715
+#define RTDID			0x160
8163715
+#define ROOT_CAP_AND_CTRL	0x5C
8163715
+
8163715
+/* PCIe IP version */
8163715
+#define XGENE_PCIE_IP_VER_UNKN	0
8163715
+#define XGENE_PCIE_IP_VER_1	1
8163715
+#define XGENE_PCIE_IP_VER_2	2
8163715
+
8163715
+#define XGENE_CSR_LENGTH	0x10000
8163715
+
8163715
+struct xgene_pcie_acpi_root {
8163715
+	void __iomem *csr_base;
8163715
+	u32 version;
8163715
+};
8163715
+
8163715
+static int xgene_v1_pcie_ecam_init(struct pci_config_window *cfg)
8163715
+{
8163715
+	struct xgene_pcie_acpi_root *xgene_root;
8163715
+	struct device *dev = cfg->parent;
8163715
+	u32 csr_base;
8163715
+
8163715
+	xgene_root = devm_kzalloc(dev, sizeof(*xgene_root), GFP_KERNEL);
8163715
+	if (!xgene_root)
8163715
+		return -ENOMEM;
8163715
+
8163715
+	switch (cfg->res.start) {
8163715
+	case 0xE0D0000000ULL:
8163715
+		csr_base = 0x1F2B0000;
8163715
+		break;
8163715
+	case 0xD0D0000000ULL:
8163715
+		csr_base = 0x1F2C0000;
8163715
+		break;
8163715
+	case 0x90D0000000ULL:
8163715
+		csr_base = 0x1F2D0000;
8163715
+		break;
8163715
+	case 0xA0D0000000ULL:
8163715
+		csr_base = 0x1F500000;
8163715
+		break;
8163715
+	case 0xC0D0000000ULL:
8163715
+		csr_base = 0x1F510000;
8163715
+		break;
8163715
+	default:
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->csr_base = ioremap(csr_base, XGENE_CSR_LENGTH);
8163715
+	if (!xgene_root->csr_base) {
8163715
+		kfree(xgene_root);
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->version = XGENE_PCIE_IP_VER_1;
8163715
+
8163715
+	cfg->priv = xgene_root;
8163715
+
8163715
+	return 0;
8163715
+}
8163715
+
8163715
+static int xgene_v2_1_pcie_ecam_init(struct pci_config_window *cfg)
8163715
+{
8163715
+	struct xgene_pcie_acpi_root *xgene_root;
8163715
+	struct device *dev = cfg->parent;
8163715
+	resource_size_t csr_base;
8163715
+
8163715
+	xgene_root = devm_kzalloc(dev, sizeof(*xgene_root), GFP_KERNEL);
8163715
+	if (!xgene_root)
8163715
+		return -ENOMEM;
8163715
+
8163715
+	switch (cfg->res.start) {
8163715
+	case 0xC0D0000000ULL:
8163715
+		csr_base = 0x1F2B0000;
8163715
+		break;
8163715
+	case 0xA0D0000000ULL:
8163715
+		csr_base = 0x1F2C0000;
8163715
+		break;
8163715
+	default:
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->csr_base = ioremap(csr_base, XGENE_CSR_LENGTH);
8163715
+	if (!xgene_root->csr_base) {
8163715
+		kfree(xgene_root);
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->version = XGENE_PCIE_IP_VER_2;
8163715
+
8163715
+	cfg->priv = xgene_root;
8163715
+
8163715
+	return 0;
8163715
+}
8163715
+
8163715
+static int xgene_v2_2_pcie_ecam_init(struct pci_config_window *cfg)
8163715
+{
8163715
+	struct xgene_pcie_acpi_root *xgene_root;
8163715
+	struct device *dev = cfg->parent;
8163715
+	resource_size_t csr_base;
8163715
+
8163715
+	xgene_root = devm_kzalloc(dev, sizeof(*xgene_root), GFP_KERNEL);
8163715
+	if (!xgene_root)
8163715
+		return -ENOMEM;
8163715
+
8163715
+	switch (cfg->res.start) {
8163715
+	case 0xE0D0000000ULL:
8163715
+		csr_base = 0x1F2B0000;
8163715
+		break;
8163715
+	case 0xA0D0000000ULL:
8163715
+		csr_base = 0x1F500000;
8163715
+		break;
8163715
+	case 0x90D0000000ULL:
8163715
+		csr_base = 0x1F2D0000;
8163715
+		break;
8163715
+	default:
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->csr_base = ioremap(csr_base, XGENE_CSR_LENGTH);
8163715
+	if (!xgene_root->csr_base) {
8163715
+		kfree(xgene_root);
8163715
+		return -ENODEV;
8163715
+	}
8163715
+
8163715
+	xgene_root->version = XGENE_PCIE_IP_VER_2;
8163715
+
8163715
+	cfg->priv = xgene_root;
8163715
+
8163715
+	return 0;
8163715
+}
8163715
+/*
8163715
+ * For Configuration request, RTDID register is used as Bus Number,
8163715
+ * Device Number and Function number of the header fields.
8163715
+ */
8163715
+static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
8163715
+{
8163715
+	struct pci_config_window *cfg = bus->sysdata;
8163715
+	struct xgene_pcie_acpi_root *port = cfg->priv;
8163715
+	unsigned int b, d, f;
8163715
+	u32 rtdid_val = 0;
8163715
+
8163715
+	b = bus->number;
8163715
+	d = PCI_SLOT(devfn);
8163715
+	f = PCI_FUNC(devfn);
8163715
+
8163715
+	if (!pci_is_root_bus(bus))
8163715
+		rtdid_val = (b << 8) | (d << 3) | f;
8163715
+
8163715
+	writel(rtdid_val, port->csr_base + RTDID);
8163715
+	/* read the register back to ensure flush */
8163715
+	readl(port->csr_base + RTDID);
8163715
+}
8163715
+
8163715
+/*
8163715
+ * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
8163715
+ * the translation from PCI bus to native BUS.  Entire DDR region
8163715
+ * is mapped into PCIe space using these registers, so it can be
8163715
+ * reached by DMA from EP devices.  The BAR0/1 of bridge should be
8163715
+ * hidden during enumeration to avoid the sizing and resource allocation
8163715
+ * by PCIe core.
8163715
+ */
8163715
+static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
8163715
+{
8163715
+	if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
8163715
+				     (offset == PCI_BASE_ADDRESS_1)))
8163715
+		return true;
8163715
+
8163715
+	return false;
8163715
+}
8163715
+
8163715
+void __iomem *xgene_pcie_ecam_map_bus(struct pci_bus *bus,
8163715
+				      unsigned int devfn, int where)
8163715
+{
8163715
+	struct pci_config_window *cfg = bus->sysdata;
8163715
+	unsigned int busn = bus->number;
8163715
+	void __iomem *base;
8163715
+
8163715
+	if (busn < cfg->busr.start || busn > cfg->busr.end)
8163715
+		return NULL;
8163715
+
8163715
+	if ((pci_is_root_bus(bus) && devfn != 0) ||
8163715
+	    xgene_pcie_hide_rc_bars(bus, where))
8163715
+		return NULL;
8163715
+
8163715
+	xgene_pcie_set_rtdid_reg(bus, devfn);
8163715
+
8163715
+	if (busn > cfg->busr.start)
8163715
+		base = cfg->win + (1 << cfg->ops->bus_shift);
8163715
+	else
8163715
+		base = cfg->win;
8163715
+
8163715
+	return base + where;
8163715
+}
8163715
+
8163715
+static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
8163715
+				    int where, int size, u32 *val)
8163715
+{
8163715
+	struct pci_config_window *cfg = bus->sysdata;
8163715
+	struct xgene_pcie_acpi_root *port = cfg->priv;
8163715
+
8163715
+	if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
8163715
+	    PCIBIOS_SUCCESSFUL)
8163715
+		return PCIBIOS_DEVICE_NOT_FOUND;
8163715
+
8163715
+	/*
8163715
+	* The v1 controller has a bug in its Configuration Request
8163715
+	* Retry Status (CRS) logic: when CRS is enabled and we read the
8163715
+	* Vendor and Device ID of a non-existent device, the controller
8163715
+	* fabricates return data of 0xFFFF0001 ("device exists but is not
8163715
+	* ready") instead of 0xFFFFFFFF ("device does not exist").  This
8163715
+	* causes the PCI core to retry the read until it times out.
8163715
+	* Avoid this by not claiming to support CRS.
8163715
+	*/
8163715
+	if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
8163715
+	    ((where & ~0x3) == ROOT_CAP_AND_CTRL))
8163715
+		*val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
8163715
+
8163715
+	if (size <= 2)
8163715
+		*val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
8163715
+
8163715
+	return PCIBIOS_SUCCESSFUL;
8163715
+}
8163715
+
8163715
+struct pci_ecam_ops xgene_v1_pcie_ecam_ops = {
8163715
+	.bus_shift	= 16,
8163715
+	.init		= xgene_v1_pcie_ecam_init,
8163715
+	.pci_ops	= {
8163715
+		.map_bus	= xgene_pcie_ecam_map_bus,
8163715
+		.read		= xgene_pcie_config_read32,
8163715
+		.write		= pci_generic_config_write,
8163715
+	}
8163715
+};
8163715
+
8163715
+struct pci_ecam_ops xgene_v2_1_pcie_ecam_ops = {
8163715
+	.bus_shift	= 16,
8163715
+	.init		= xgene_v2_1_pcie_ecam_init,
8163715
+	.pci_ops	= {
8163715
+		.map_bus	= xgene_pcie_ecam_map_bus,
8163715
+		.read		= xgene_pcie_config_read32,
8163715
+		.write		= pci_generic_config_write,
8163715
+	}
8163715
+};
8163715
+
8163715
+struct pci_ecam_ops xgene_v2_2_pcie_ecam_ops = {
8163715
+	.bus_shift	= 16,
8163715
+	.init		= xgene_v2_2_pcie_ecam_init,
8163715
+	.pci_ops	= {
8163715
+		.map_bus	= xgene_pcie_ecam_map_bus,
8163715
+		.read		= xgene_pcie_config_read32,
8163715
+		.write		= pci_generic_config_write,
8163715
+	}
8163715
+};
8163715
+#endif
8163715
diff --git a/include/linux/pci-ecam.h b/include/linux/pci-ecam.h
8163715
index 35f0e81..40da3e7 100644
8163715
--- a/include/linux/pci-ecam.h
8163715
+++ b/include/linux/pci-ecam.h
8163715
@@ -65,6 +65,11 @@ extern struct pci_ecam_ops pci_thunder_pem_ops;
8163715
 #ifdef CONFIG_PCI_HOST_THUNDER_ECAM
8163715
 extern struct pci_ecam_ops pci_thunder_ecam_ops;
8163715
 #endif
8163715
+#ifdef CONFIG_PCI_XGENE
8163715
+extern struct pci_ecam_ops xgene_v1_pcie_ecam_ops;
8163715
+extern struct pci_ecam_ops xgene_v2_1_pcie_ecam_ops;
8163715
+extern struct pci_ecam_ops xgene_v2_2_pcie_ecam_ops;
8163715
+#endif
8163715
 
8163715
 #ifdef CONFIG_PCI_HOST_GENERIC
8163715
 /* for DT-based PCI controllers that support ECAM */
8163715
-- 
8163715
2.9.3
8163715