ec226c2
From 767b70aa55d013f0c7589955f410d488fed5776a Mon Sep 17 00:00:00 2001
ec226c2
From: Peter Robinson <pbrobinson@gmail.com>
ec226c2
Date: Tue, 5 Jul 2016 23:49:39 +0100
ec226c2
Subject: [PATCH 1/4] Some platforms may not be fully compliant with generic
ec226c2
 set of PCI config accessors. For these cases we implement the way to
ec226c2
 overwrite accessors set. Algorithm traverses available quirk list, matches
ec226c2
 against <oem_id, oem_table_id, domain, bus number> tuple and returns
ec226c2
 corresponding PCI config ops. oem_id and oem_table_id come from MCFG table
ec226c2
 standard header. All quirks can be defined using DECLARE_ACPI_MCFG_FIXUP()
ec226c2
 macro and kept self contained. Example:
ec226c2
ec226c2
/* Custom PCI config ops */
ec226c2
static struct pci_generic_ecam_ops foo_pci_ops = {
ec226c2
        .bus_shift      = 24,
ec226c2
        .pci_ops = {
ec226c2
                .map_bus = pci_ecam_map_bus,
ec226c2
                .read = foo_ecam_config_read,
ec226c2
                .write = foo_ecam_config_write,
ec226c2
        }
ec226c2
};
ec226c2
ec226c2
DECLARE_ACPI_MCFG_FIXUP(&foo_pci_ops, <oem_id_str>, <oem_table_id>, <domain_nr>, <bus_nr>);
ec226c2
ec226c2
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
ec226c2
Signed-off-by: Dongdong Liu <liudongdong3@huawei.com>
ec226c2
---
ec226c2
 drivers/acpi/pci_mcfg.c           | 41 ++++++++++++++++++++++++++++++++++++---
ec226c2
 include/asm-generic/vmlinux.lds.h |  7 +++++++
ec226c2
 include/linux/pci-acpi.h          | 20 +++++++++++++++++++
ec226c2
 3 files changed, 65 insertions(+), 3 deletions(-)
ec226c2
ec226c2
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
ec226c2
index d3c3e85..deb0077 100644
ec226c2
--- a/drivers/acpi/pci_mcfg.c
ec226c2
+++ b/drivers/acpi/pci_mcfg.c
ec226c2
@@ -22,6 +22,10 @@
ec226c2
 #include <linux/kernel.h>
ec226c2
 #include <linux/pci.h>
ec226c2
 #include <linux/pci-acpi.h>
ec226c2
+#include <linux/pci-ecam.h>
ec226c2
+
ec226c2
+/* Root pointer to the mapped MCFG table */
ec226c2
+static struct acpi_table_mcfg *mcfg_table;
ec226c2
 
ec226c2
 /* Structure to hold entries from the MCFG table */
ec226c2
 struct mcfg_entry {
ec226c2
@@ -35,6 +39,38 @@ struct mcfg_entry {
ec226c2
 /* List to save mcfg entries */
ec226c2
 static LIST_HEAD(pci_mcfg_list);
ec226c2
 
ec226c2
+extern struct pci_cfg_fixup __start_acpi_mcfg_fixups[];
ec226c2
+extern struct pci_cfg_fixup __end_acpi_mcfg_fixups[];
ec226c2
+
ec226c2
+struct pci_ecam_ops *pci_mcfg_get_ops(struct acpi_pci_root *root)
ec226c2
+{
ec226c2
+       int bus_num = root->secondary.start;
ec226c2
+       int domain = root->segment;
ec226c2
+       struct pci_cfg_fixup *f;
ec226c2
+
ec226c2
+       if (!mcfg_table)
ec226c2
+               return &pci_generic_ecam_ops;
ec226c2
+
ec226c2
+       /*
ec226c2
+        * Match against platform specific quirks and return corresponding
ec226c2
+        * CAM ops.
ec226c2
+        *
ec226c2
+        * First match against PCI topology <domain:bus> then use OEM ID and
ec226c2
+        * OEM revision from MCFG table standard header.
ec226c2
+        */
ec226c2
+       for (f = __start_acpi_mcfg_fixups; f < __end_acpi_mcfg_fixups; f++) {
ec226c2
+               if ((f->domain == domain || f->domain == PCI_MCFG_DOMAIN_ANY) &&
ec226c2
+                   (f->bus_num == bus_num || f->bus_num == PCI_MCFG_BUS_ANY) &&
ec226c2
+                   (!strncmp(f->oem_id, mcfg_table->header.oem_id,
ec226c2
+                             ACPI_OEM_ID_SIZE)) &&
ec226c2
+                   (!strncmp(f->oem_table_id, mcfg_table->header.oem_table_id,
ec226c2
+                             ACPI_OEM_TABLE_ID_SIZE)))
ec226c2
+                       return f->ops;
ec226c2
+       }
ec226c2
+       /* No quirks, use ECAM */
ec226c2
+       return &pci_generic_ecam_ops;
ec226c2
+}
ec226c2
+
ec226c2
 phys_addr_t pci_mcfg_lookup(u16 seg, struct resource *bus_res)
ec226c2
 {
ec226c2
 	struct mcfg_entry *e;
ec226c2
@@ -54,7 +90,6 @@ phys_addr_t pci_mcfg_lookup(u16 seg, struct resource *bus_res)
ec226c2
 
ec226c2
 static __init int pci_mcfg_parse(struct acpi_table_header *header)
ec226c2
 {
ec226c2
-	struct acpi_table_mcfg *mcfg;
ec226c2
 	struct acpi_mcfg_allocation *mptr;
ec226c2
 	struct mcfg_entry *e, *arr;
ec226c2
 	int i, n;
ec226c2
@@ -64,8 +99,8 @@ static __init int pci_mcfg_parse(struct acpi_table_header *header)
ec226c2
 
ec226c2
 	n = (header->length - sizeof(struct acpi_table_mcfg)) /
ec226c2
 					sizeof(struct acpi_mcfg_allocation);
ec226c2
-	mcfg = (struct acpi_table_mcfg *)header;
ec226c2
-	mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
ec226c2
+	mcfg_table = (struct acpi_table_mcfg *)header;
ec226c2
+	mptr = (struct acpi_mcfg_allocation *) &mcfg_table[1];
ec226c2
 
ec226c2
 	arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
ec226c2
 	if (!arr)
ec226c2
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
ec226c2
index 6a67ab9..43604fc 100644
ec226c2
--- a/include/asm-generic/vmlinux.lds.h
ec226c2
+++ b/include/asm-generic/vmlinux.lds.h
ec226c2
@@ -300,6 +300,13 @@
ec226c2
 		VMLINUX_SYMBOL(__end_pci_fixups_suspend_late) = .;	\
ec226c2
 	}								\
ec226c2
 									\
ec226c2
+	/* ACPI MCFG quirks */						\
ec226c2
+	.acpi_fixup        : AT(ADDR(.acpi_fixup) - LOAD_OFFSET) {	\
ec226c2
+		VMLINUX_SYMBOL(__start_acpi_mcfg_fixups) = .;		\
ec226c2
+		*(.acpi_fixup_mcfg)					\
ec226c2
+		VMLINUX_SYMBOL(__end_acpi_mcfg_fixups) = .;		\
ec226c2
+	}								\
ec226c2
+									\
ec226c2
 	/* Built-in firmware blobs */					\
ec226c2
 	.builtin_fw        : AT(ADDR(.builtin_fw) - LOAD_OFFSET) {	\
ec226c2
 		VMLINUX_SYMBOL(__start_builtin_fw) = .;			\
ec226c2
diff --git a/include/linux/pci-acpi.h b/include/linux/pci-acpi.h
ec226c2
index 7d63a66..c8a6559 100644
ec226c2
--- a/include/linux/pci-acpi.h
ec226c2
+++ b/include/linux/pci-acpi.h
ec226c2
@@ -25,6 +25,7 @@ static inline acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
ec226c2
 extern phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle);
ec226c2
 
ec226c2
 extern phys_addr_t pci_mcfg_lookup(u16 domain, struct resource *bus_res);
ec226c2
+extern struct pci_ecam_ops *pci_mcfg_get_ops(struct acpi_pci_root *root);
ec226c2
 
ec226c2
 static inline acpi_handle acpi_find_root_bridge_handle(struct pci_dev *pdev)
ec226c2
 {
ec226c2
@@ -72,6 +73,25 @@ struct acpi_pci_root_ops {
ec226c2
 	int (*prepare_resources)(struct acpi_pci_root_info *info);
ec226c2
 };
ec226c2
 
ec226c2
+struct pci_cfg_fixup {
ec226c2
+       struct pci_ecam_ops *ops;
ec226c2
+       char *oem_id;
ec226c2
+       char *oem_table_id;
ec226c2
+       int domain;
ec226c2
+       int bus_num;
ec226c2
+};
ec226c2
+
ec226c2
+#define PCI_MCFG_DOMAIN_ANY    -1
ec226c2
+#define PCI_MCFG_BUS_ANY       -1
ec226c2
+
ec226c2
+/* Designate a routine to fix up buggy MCFG */
ec226c2
+#define DECLARE_ACPI_MCFG_FIXUP(ops, oem_id, oem_table_id, dom, bus)   \
ec226c2
+       static const struct pci_cfg_fixup                               \
ec226c2
+       __mcfg_fixup_##oem_id##oem_table_id##dom##bus                   \
ec226c2
+       __used  __attribute__((__section__(".acpi_fixup_mcfg"),         \
ec226c2
+                               aligned((sizeof(void *))))) =           \
ec226c2
+       { ops, oem_id, oem_table_id, dom, bus };
ec226c2
+
ec226c2
 extern int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info);
ec226c2
 extern struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
ec226c2
 					    struct acpi_pci_root_ops *ops,
ec226c2
-- 
ec226c2
2.7.4
ec226c2
ec226c2
From 4f86a9b006b25dd7336043dab26058ed6fb2802d Mon Sep 17 00:00:00 2001
ec226c2
From: Peter Robinson <pbrobinson@gmail.com>
ec226c2
Date: Tue, 5 Jul 2016 23:52:46 +0100
ec226c2
Subject: [PATCH 2/4] pci_generic_ecam_ops is used by default. Since there are
ec226c2
 platforms which have non-compliant ECAM space we need to overwrite these
ec226c2
 accessors prior to PCI buses enumeration. In order to do that we call
ec226c2
 pci_mcfg_get_ops to retrieve pci_ecam_ops structure so that we can use proper
ec226c2
 PCI config space accessors and bus_shift.
ec226c2
ec226c2
pci_generic_ecam_ops is still used for platforms free from quirks.
ec226c2
ec226c2
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
ec226c2
---
ec226c2
 arch/arm64/kernel/pci.c | 7 ++++---
ec226c2
 1 file changed, 4 insertions(+), 3 deletions(-)
ec226c2
ec226c2
diff --git a/arch/arm64/kernel/pci.c b/arch/arm64/kernel/pci.c
ec226c2
index 94cd43c..a891bda 100644
ec226c2
--- a/arch/arm64/kernel/pci.c
ec226c2
+++ b/arch/arm64/kernel/pci.c
ec226c2
@@ -139,6 +139,7 @@ pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
ec226c2
 	struct pci_config_window *cfg;
ec226c2
 	struct resource cfgres;
ec226c2
 	unsigned int bsz;
ec226c2
+	struct pci_ecam_ops *ops;
ec226c2
 
ec226c2
 	/* Use address from _CBA if present, otherwise lookup MCFG */
ec226c2
 	if (!root->mcfg_addr)
ec226c2
@@ -150,12 +151,12 @@ pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
ec226c2
 		return NULL;
ec226c2
 	}
ec226c2
 
ec226c2
-	bsz = 1 << pci_generic_ecam_ops.bus_shift;
ec226c2
+	ops = pci_mcfg_get_ops(root);
ec226c2
+	bsz = 1 << ops->bus_shift;
ec226c2
 	cfgres.start = root->mcfg_addr + bus_res->start * bsz;
ec226c2
 	cfgres.end = cfgres.start + resource_size(bus_res) * bsz - 1;
ec226c2
 	cfgres.flags = IORESOURCE_MEM;
ec226c2
-	cfg = pci_ecam_create(&root->device->dev, &cfgres, bus_res,
ec226c2
-			      &pci_generic_ecam_ops);
ec226c2
+	cfg = pci_ecam_create(&root->device->dev, &cfgres, bus_res, ops);
ec226c2
 	if (IS_ERR(cfg)) {
ec226c2
 		dev_err(&root->device->dev, "%04x:%pR error %ld mapping ECAM\n",
ec226c2
 			seg, bus_res, PTR_ERR(cfg));
ec226c2
-- 
ec226c2
2.7.4
ec226c2
ec226c2
From cbdbd697bd6d716eb9d1705ee55445432e73eabb Mon Sep 17 00:00:00 2001
ec226c2
From: Peter Robinson <pbrobinson@gmail.com>
ec226c2
Date: Tue, 5 Jul 2016 23:53:59 +0100
ec226c2
Subject: [PATCH 3/4] The ECAM quirk matching criteria per the discussion on
ec226c2
 https://lkml.org/lkml/2016/6/13/944 includes: OEM ID, OEM Table ID and OEM
ec226c2
 Revision. So this patch adds OEM Table ID into the check to match platform
ec226c2
 specific ECAM quirks as well.
ec226c2
ec226c2
This patch also improve strncmp check using strlen and
ec226c2
min_t to ignore the padding spaces in OEM ID and OEM
ec226c2
Table ID.
ec226c2
ec226c2
Signed-off-by: Duc Dang <dhdang@apm.com>
ec226c2
---
ec226c2
 drivers/acpi/pci_mcfg.c  | 7 +++++--
ec226c2
 include/linux/pci-acpi.h | 7 ++++---
ec226c2
 2 files changed, 9 insertions(+), 5 deletions(-)
ec226c2
ec226c2
diff --git a/drivers/acpi/pci_mcfg.c b/drivers/acpi/pci_mcfg.c
ec226c2
index deb0077..307ca9a 100644
ec226c2
--- a/drivers/acpi/pci_mcfg.c
ec226c2
+++ b/drivers/acpi/pci_mcfg.c
ec226c2
@@ -62,9 +62,12 @@ struct pci_ecam_ops *pci_mcfg_get_ops(struct acpi_pci_root *root)
ec226c2
                if ((f->domain == domain || f->domain == PCI_MCFG_DOMAIN_ANY) &&
ec226c2
                    (f->bus_num == bus_num || f->bus_num == PCI_MCFG_BUS_ANY) &&
ec226c2
                    (!strncmp(f->oem_id, mcfg_table->header.oem_id,
ec226c2
-                             ACPI_OEM_ID_SIZE)) &&
ec226c2
+                             min_t(size_t, strlen(f->oem_id),
ec226c2
+                                   ACPI_OEM_ID_SIZE))) &&
ec226c2
                    (!strncmp(f->oem_table_id, mcfg_table->header.oem_table_id,
ec226c2
-                             ACPI_OEM_TABLE_ID_SIZE)))
ec226c2
+                             min_t(size_t, strlen(f->oem_table_id),
ec226c2
+                                   ACPI_OEM_TABLE_ID_SIZE))) &&
ec226c2
+                   (f->oem_revision == mcfg_table->header.oem_revision))
ec226c2
                        return f->ops;
ec226c2
        }
ec226c2
        /* No quirks, use ECAM */
ec226c2
diff --git a/include/linux/pci-acpi.h b/include/linux/pci-acpi.h
ec226c2
index c8a6559..5148c8d 100644
ec226c2
--- a/include/linux/pci-acpi.h
ec226c2
+++ b/include/linux/pci-acpi.h
ec226c2
@@ -77,6 +77,7 @@ struct pci_cfg_fixup {
ec226c2
        struct pci_ecam_ops *ops;
ec226c2
        char *oem_id;
ec226c2
        char *oem_table_id;
ec226c2
+       u32 oem_revision;
ec226c2
        int domain;
ec226c2
        int bus_num;
ec226c2
 };
ec226c2
@@ -85,12 +86,12 @@ struct pci_cfg_fixup {
ec226c2
 #define PCI_MCFG_BUS_ANY       -1
ec226c2
 
ec226c2
 /* Designate a routine to fix up buggy MCFG */
ec226c2
-#define DECLARE_ACPI_MCFG_FIXUP(ops, oem_id, oem_table_id, dom, bus)   \
ec226c2
+#define DECLARE_ACPI_MCFG_FIXUP(ops, oem_id, oem_table_id, rev, dom, bus) \
ec226c2
        static const struct pci_cfg_fixup                               \
ec226c2
-       __mcfg_fixup_##oem_id##oem_table_id##dom##bus                   \
ec226c2
+       __mcfg_fixup_##oem_id##oem_table_id##rev##dom##bus              \
ec226c2
        __used  __attribute__((__section__(".acpi_fixup_mcfg"),         \
ec226c2
                                aligned((sizeof(void *))))) =           \
ec226c2
-       { ops, oem_id, oem_table_id, dom, bus };
ec226c2
+       { ops, oem_id, oem_table_id, rev, dom, bus };
ec226c2
 
ec226c2
 extern int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info);
ec226c2
 extern struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
ec226c2
-- 
ec226c2
2.7.4
ec226c2
ec226c2
From 78766cf255bc6aafac2f57372a0446f78322da19 Mon Sep 17 00:00:00 2001
ec226c2
From: Peter Robinson <pbrobinson@gmail.com>
ec226c2
Date: Tue, 5 Jul 2016 23:55:11 +0100
ec226c2
Subject: [PATCH 4/4] X-Gene PCIe controller does not fully support ECAM. This
ec226c2
 patch adds required ECAM fixup to allow X-Gene PCIe controller to be
ec226c2
 functional in ACPI boot mode.
ec226c2
ec226c2
Signed-off-by: Duc Dang <dhdang@apm.com>
ec226c2
---
ec226c2
 drivers/pci/host/Makefile         |   2 +-
ec226c2
 drivers/pci/host/pci-xgene-ecam.c | 194 ++++++++++++++++++++++++++++++++++++++
ec226c2
 2 files changed, 195 insertions(+), 1 deletion(-)
ec226c2
 create mode 100644 drivers/pci/host/pci-xgene-ecam.c
ec226c2
ec226c2
diff --git a/drivers/pci/host/Makefile b/drivers/pci/host/Makefile
ec226c2
index 9c8698e..3480696 100644
ec226c2
--- a/drivers/pci/host/Makefile
ec226c2
+++ b/drivers/pci/host/Makefile
ec226c2
@@ -14,7 +14,7 @@ obj-$(CONFIG_PCIE_SPEAR13XX) += pcie-spear13xx.o
ec226c2
 obj-$(CONFIG_PCI_KEYSTONE) += pci-keystone-dw.o pci-keystone.o
ec226c2
 obj-$(CONFIG_PCIE_XILINX) += pcie-xilinx.o
ec226c2
 obj-$(CONFIG_PCIE_XILINX_NWL) += pcie-xilinx-nwl.o
ec226c2
-obj-$(CONFIG_PCI_XGENE) += pci-xgene.o
ec226c2
+obj-$(CONFIG_PCI_XGENE) += pci-xgene.o pci-xgene-ecam.o
ec226c2
 obj-$(CONFIG_PCI_XGENE_MSI) += pci-xgene-msi.o
ec226c2
 obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o
ec226c2
 obj-$(CONFIG_PCI_VERSATILE) += pci-versatile.o
ec226c2
diff --git a/drivers/pci/host/pci-xgene-ecam.c b/drivers/pci/host/pci-xgene-ecam.c
ec226c2
new file mode 100644
ec226c2
index 0000000..1bea63f
ec226c2
--- /dev/null
ec226c2
+++ b/drivers/pci/host/pci-xgene-ecam.c
ec226c2
@@ -0,0 +1,194 @@
ec226c2
+/*
ec226c2
+ * APM X-Gene PCIe ECAM fixup driver
ec226c2
+ *
ec226c2
+ * Copyright (c) 2016, Applied Micro Circuits Corporation
ec226c2
+ * Author:
ec226c2
+ *     Duc Dang <dhdang@apm.com>
ec226c2
+ *
ec226c2
+ * This program is free software; you can redistribute it and/or modify
ec226c2
+ * it under the terms of the GNU General Public License version 2 as
ec226c2
+ * published by the Free Software Foundation.
ec226c2
+ *
ec226c2
+ * This program is distributed in the hope that it will be useful,
ec226c2
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ec226c2
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ec226c2
+ * GNU General Public License for more details.
ec226c2
+ *
ec226c2
+ * You should have received a copy of the GNU General Public License
ec226c2
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
ec226c2
+ */
ec226c2
+
ec226c2
+#include <linux/kernel.h>
ec226c2
+#include <linux/module.h>
ec226c2
+#include <linux/of_address.h>
ec226c2
+#include <linux/of_pci.h>
ec226c2
+#include <linux/pci-acpi.h>
ec226c2
+#include <linux/platform_device.h>
ec226c2
+#include <linux/pci-ecam.h>
ec226c2
+
ec226c2
+#ifdef CONFIG_ACPI
ec226c2
+#define RTDID                  0x160
ec226c2
+#define ROOT_CAP_AND_CTRL      0x5C
ec226c2
+
ec226c2
+/* PCIe IP version */
ec226c2
+#define XGENE_PCIE_IP_VER_UNKN 0
ec226c2
+#define XGENE_PCIE_IP_VER_1    1
ec226c2
+
ec226c2
+#define APM_OEM_ID             "APM"
ec226c2
+#define APM_XGENE_OEM_TABLE_ID "XGENE"
ec226c2
+#define APM_XGENE_OEM_REV      0x00000002
ec226c2
+
ec226c2
+struct xgene_pcie_acpi_root {
ec226c2
+       void __iomem *csr_base;
ec226c2
+       u32 version;
ec226c2
+};
ec226c2
+
ec226c2
+static acpi_status xgene_pcie_find_csr_base(struct acpi_resource *acpi_res,
ec226c2
+                                           void *data)
ec226c2
+{
ec226c2
+       struct xgene_pcie_acpi_root *root = data;
ec226c2
+       struct acpi_resource_fixed_memory32 *fixed32;
ec226c2
+
ec226c2
+       if (acpi_res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
ec226c2
+               fixed32 = &acpi_res->data.fixed_memory32;
ec226c2
+               root->csr_base = ioremap(fixed32->address,
ec226c2
+                                        fixed32->address_length);
ec226c2
+               return AE_CTRL_TERMINATE;
ec226c2
+       }
ec226c2
+
ec226c2
+       return AE_OK;
ec226c2
+}
ec226c2
+
ec226c2
+static int xgene_pcie_ecam_init(struct pci_config_window *cfg)
ec226c2
+{
ec226c2
+       struct xgene_pcie_acpi_root *xgene_root;
ec226c2
+       struct device *dev = cfg->parent;
ec226c2
+       struct acpi_device *adev = to_acpi_device(dev);
ec226c2
+       acpi_handle handle = acpi_device_handle(adev);
ec226c2
+
ec226c2
+       xgene_root = devm_kzalloc(dev, sizeof(*xgene_root), GFP_KERNEL);
ec226c2
+       if (!xgene_root)
ec226c2
+               return -ENOMEM;
ec226c2
+
ec226c2
+       acpi_walk_resources(handle, METHOD_NAME__CRS,
ec226c2
+                           xgene_pcie_find_csr_base, xgene_root);
ec226c2
+
ec226c2
+       if (!xgene_root->csr_base) {
ec226c2
+               kfree(xgene_root);
ec226c2
+               return -ENODEV;
ec226c2
+       }
ec226c2
+
ec226c2
+       xgene_root->version = XGENE_PCIE_IP_VER_1;
ec226c2
+
ec226c2
+       cfg->priv = xgene_root;
ec226c2
+
ec226c2
+       return 0;
ec226c2
+}
ec226c2
+
ec226c2
+/*
ec226c2
+ * For Configuration request, RTDID register is used as Bus Number,
ec226c2
+ * Device Number and Function number of the header fields.
ec226c2
+ */
ec226c2
+static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
ec226c2
+{
ec226c2
+       struct pci_config_window *cfg = bus->sysdata;
ec226c2
+       struct xgene_pcie_acpi_root *port = cfg->priv;
ec226c2
+       unsigned int b, d, f;
ec226c2
+       u32 rtdid_val = 0;
ec226c2
+
ec226c2
+       b = bus->number;
ec226c2
+       d = PCI_SLOT(devfn);
ec226c2
+       f = PCI_FUNC(devfn);
ec226c2
+
ec226c2
+       if (!pci_is_root_bus(bus))
ec226c2
+               rtdid_val = (b << 8) | (d << 3) | f;
ec226c2
+
ec226c2
+       writel(rtdid_val, port->csr_base + RTDID);
ec226c2
+       /* read the register back to ensure flush */
ec226c2
+       readl(port->csr_base + RTDID);
ec226c2
+}
ec226c2
+
ec226c2
+/*
ec226c2
+ * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
ec226c2
+ * the translation from PCI bus to native BUS.  Entire DDR region
ec226c2
+ * is mapped into PCIe space using these registers, so it can be
ec226c2
+ * reached by DMA from EP devices.  The BAR0/1 of bridge should be
ec226c2
+ * hidden during enumeration to avoid the sizing and resource allocation
ec226c2
+ * by PCIe core.
ec226c2
+ */
ec226c2
+static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
ec226c2
+{
ec226c2
+       if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
ec226c2
+                                    (offset == PCI_BASE_ADDRESS_1)))
ec226c2
+               return true;
ec226c2
+
ec226c2
+       return false;
ec226c2
+}
ec226c2
+
ec226c2
+void __iomem *xgene_pcie_ecam_map_bus(struct pci_bus *bus,
ec226c2
+                                     unsigned int devfn, int where)
ec226c2
+{
ec226c2
+       struct pci_config_window *cfg = bus->sysdata;
ec226c2
+       unsigned int busn = bus->number;
ec226c2
+       void __iomem *base;
ec226c2
+
ec226c2
+       if (busn < cfg->busr.start || busn > cfg->busr.end)
ec226c2
+               return NULL;
ec226c2
+
ec226c2
+       if ((pci_is_root_bus(bus) && devfn != 0) ||
ec226c2
+           xgene_pcie_hide_rc_bars(bus, where))
ec226c2
+               return NULL;
ec226c2
+
ec226c2
+       xgene_pcie_set_rtdid_reg(bus, devfn);
ec226c2
+
ec226c2
+       if (busn > cfg->busr.start)
ec226c2
+               base = cfg->win + (1 << cfg->ops->bus_shift);
ec226c2
+       else
ec226c2
+               base = cfg->win;
ec226c2
+
ec226c2
+       return base + where;
ec226c2
+}
ec226c2
+
ec226c2
+static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
ec226c2
+                                   int where, int size, u32 *val)
ec226c2
+{
ec226c2
+       struct pci_config_window *cfg = bus->sysdata;
ec226c2
+       struct xgene_pcie_acpi_root *port = cfg->priv;
ec226c2
+
ec226c2
+       if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
ec226c2
+           PCIBIOS_SUCCESSFUL)
ec226c2
+               return PCIBIOS_DEVICE_NOT_FOUND;
ec226c2
+
ec226c2
+       /*
ec226c2
+       * The v1 controller has a bug in its Configuration Request
ec226c2
+       * Retry Status (CRS) logic: when CRS is enabled and we read the
ec226c2
+       * Vendor and Device ID of a non-existent device, the controller
ec226c2
+       * fabricates return data of 0xFFFF0001 ("device exists but is not
ec226c2
+       * ready") instead of 0xFFFFFFFF ("device does not exist").  This
ec226c2
+       * causes the PCI core to retry the read until it times out.
ec226c2
+       * Avoid this by not claiming to support CRS.
ec226c2
+       */
ec226c2
+       if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
ec226c2
+           ((where & ~0x3) == ROOT_CAP_AND_CTRL))
ec226c2
+               *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
ec226c2
+
ec226c2
+       if (size <= 2)
ec226c2
+               *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
ec226c2
+
ec226c2
+       return PCIBIOS_SUCCESSFUL;
ec226c2
+}
ec226c2
+
ec226c2
+static struct pci_ecam_ops xgene_pcie_ecam_ops = {
ec226c2
+       .bus_shift      = 16,
ec226c2
+       .init           = xgene_pcie_ecam_init,
ec226c2
+       .pci_ops        = {
ec226c2
+               .map_bus        = xgene_pcie_ecam_map_bus,
ec226c2
+               .read           = xgene_pcie_config_read32,
ec226c2
+               .write          = pci_generic_config_write,
ec226c2
+       }
ec226c2
+};
ec226c2
+
ec226c2
+DECLARE_ACPI_MCFG_FIXUP(&xgene_pcie_ecam_ops, APM_OEM_ID,
ec226c2
+                       APM_XGENE_OEM_TABLE_ID, APM_XGENE_OEM_REV,
ec226c2
+                       PCI_MCFG_DOMAIN_ANY, PCI_MCFG_BUS_ANY);
ec226c2
+#endif
ec226c2
-- 
ec226c2
2.7.4
ec226c2