Skip to contentSkip to conversation

PCI: Add support to the Cadence PCIe controller

PATCH Superseded Cyrille Pitchen 8 participants 39 messages Nov 23, 2017 – Dec 15, 2017

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover PCI: Add support to the Cadence PCIe controller

Hi all,

this series of patches adds support to the Cadence PCIe controller. It was tested on a ARM64 platform emulated by a Palladium running both linux-next (next-20171123) and pci-next kernels.

The host mode was tested with some PCIe devices connected to the Palladium through a speed-bridge. Some of those devices were a USB host controller and a SATA controller. The PCIe host controller was also tested with a second controller configured in endpoint mode and connected back to back to the first controller.

The EndPoint Controller (EPC) driver of this series was tested with the pci-epf-test.c EndPoint Function (EPF) driver and the pcitest userspace program.

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.
I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Best regards,

Cyrille

Cyrille Pitchen (4):
PCI: Add vendor ID for Cadence
PCI: cadence: Add host driver for Cadence PCIe controller
dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe endpoint
controller
PCI: cadence: add EndPoint Controller driver for Cadence PCIe
controller

Scott Telford (1):
dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe host
controller

 .../devicetree/bindings/pci/cdns,cdns-pcie-ep.txt  |  20 +
 .../bindings/pci/cdns,cdns-pcie-host.txt           |  54 ++
 drivers/Makefile                                   |   1 +
 drivers/pci/Kconfig                                |   1 +
 drivers/pci/cadence/Kconfig                        |  33 ++
 drivers/pci/cadence/Makefile                       |   3 +
 drivers/pci/cadence/pcie-cadence-ep.c              | 553 +++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence-host.c            | 425 ++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c                 | 110 ++++
 drivers/pci/cadence/pcie-cadence.h                 | 325 ++++++++++++
 include/linux/pci_ids.h                            |   2 +
 11 files changed, 1527 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-ep.txt
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/5 PCI: Add vendor ID for Cadence

This patch adds a new PCI vendor ID for Cadence.

---
 include/linux/pci_ids.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/pci_ids.h +2 -0
index ab20dc5db423..eb13e84e1fef 100644
@@ -2381,6 +2381,8 @@
 
 #define PCI_VENDOR_ID_LENOVO		0x17aa
 
+#define PCI_VENDOR_ID_CDNS		0x17cd
+
 #define PCI_VENDOR_ID_ARECA		0x17d3
 #define PCI_DEVICE_ID_ARECA_1110	0x1110
 #define PCI_DEVICE_ID_ARECA_1120	0x1120
Signature · 6 lines
2.11.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

PATCH 2/5 dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe host controller

In reply to Cyrille Pitchen

From: Scott Telford <[email protected]>

This patch adds documentation for the DT bindings of the Cadence PCIe controller when configured in host (Root Complex) mode.

dt-bindings: PCI: cadence: host fixup

---
 .../bindings/pci/cdns,cdns-pcie-host.txt           | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +54 -0
new file mode 100644
index 000000000000..4b3df8ffd5e1
@@ -0,0 +1,54 @@
+* Cadence PCIe host controller
+
+This PCIe controller inherits the base properties defined in
+host-generic-pci.txt.
+
+Required properties:
+- compatible: should contain "cdns,cdns-pcie-host" to identify the IP used.
+- reg: Should contain the PCIe configuration window base address, controller
+  register base address, and AXI interface region base address respectively.
+- reg-names: Must be "cfg", "reg" and "mem" respectively.
+- #address-cells: set to <3>
+- #size-cells: set to <2>
+- device_type: set to "pci"
+- ranges: ranges for the PCI memory and I/O regions
+- #interrupt-cells: set to <1>
+- interrupt-map-mask and interrupt-map: standard PCI
+	properties to define the mapping of the PCIe interface to interrupt
+	numbers.
+
+Example:
+
+	pci@fb000000 {
+		compatible = "cdns,cdns-pcie-host";
+		device_type = "pci";
+		#address-cells = <3>;
+		#size-cells = <2>;
+		bus-range = <0x0 0xff>;
+		linux,pci-domain = <0>;
+
+		// CPU_PHYSICAL(2)  SIZE(2)
+		reg = <0x0 0x41000000  0x0 0x00001000>, /* Config space */
+		      <0x0 0xfb000000  0x0 0x01000000>, /* Base Address */
+		      <0x0 0x40000000  0x0 0x04000000>; /* AXI region */
+		reg-names = "cfg", "reg", "mem";
+
+		// BUS_ADDRESS(3)  CPU_PHYSICAL(1)  SIZE(2)
+		ranges =
+			<0x02000000 0x0 0x42000000  0x0 0x42000000  0x0 0x1000000>,
+			<0x01000000 0x0 0x43000000  0x0 0x43000000  0x0 0x0010000>;
+
+
+		#interrupt-cells = <0x1>;
+
+		// PCI_DEVICE(3)  INT#(1)  CONTROLLER(PHANDLE)  CONTROLLER_DATA(5)
+		interrupt-map = <0x0 0x0 0x0  0x1  &gic  0x0 0x0 0x0 14 0x1
+				 0x0 0x0 0x0  0x2  &gic  0x0 0x0 0x0 15 0x1
+				 0x0 0x0 0x0  0x3  &gic  0x0 0x0 0x0 16 0x1
+				 0x0 0x0 0x0  0x4  &gic  0x0 0x0 0x0 17 0x1>;
+
+		// PCI_DEVICE(3)  INT#(1)
+		interrupt-map-mask = <0x0000 0x0 0x0  0x7>;
+
+		msi-parent = <&its_pci>;
+	};
Signature · 1 line
2.11.0
#5 lore raw

PATCH 3/5 PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
drivers/Makefile +1 -0
index 1d034b680431..27bdd98784d9 100644
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
 
drivers/pci/Kconfig +1 -0
index 90944667ccea..2471b2e36b8b 100644
@@ -144,6 +144,7 @@ config PCI_HYPERV
           PCI devices from a PCI backend to support PCI driver domains.
 
 source "drivers/pci/hotplug/Kconfig"
+source "drivers/pci/cadence/Kconfig"
 source "drivers/pci/dwc/Kconfig"
 source "drivers/pci/host/Kconfig"
 source "drivers/pci/endpoint/Kconfig"
drivers/pci/cadence/Kconfig new file +24 -0
new file mode 100644
index 000000000000..120306cae2aa
@@ -0,0 +1,24 @@
+menuconfig PCI_CADENCE
+	bool "Cadence PCI controllers support"
+	depends on PCI && HAS_IOMEM
+	help
+	  Say Y here if you want to support some Cadence PCI controller.
+
+	  When in doubt, say N.
+
+if PCI_CADENCE
+
+config PCIE_CADENCE
+	bool
+
+config PCIE_CADENCE_HOST
+	bool "Cadence PCIe host controller"
+	depends on OF
+	depends on PCI_MSI_IRQ_DOMAIN
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe controller in host
+	  mode. This PCIe controller may be embedded into many different vendors
+	  SoCs.
+
+endif # PCI_CADENCE
drivers/pci/cadence/Makefile new file +2 -0
new file mode 100644
index 000000000000..d57d192d2595
@@ -0,0 +1,2 @@
+obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
+obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
drivers/pci/cadence/pcie-cadence-host.c new file +425 -0

425 lines not on this page · raw message

drivers/pci/cadence/pcie-cadence.c new file +110 -0

110 lines not on this page · raw message

drivers/pci/cadence/pcie-cadence.h new file +325 -0

325 lines not on this page · raw message

Signature · 6 lines
2.11.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#21 lore raw

PATCH 4/5 dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe endpoint controller

In reply to Cyrille Pitchen

This patch documents the DT bindings for the Cadence PCIe controller when configured in endpoint mode.

---
 .../devicetree/bindings/pci/cdns,cdns-pcie-ep.txt    | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-ep.txt
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-ep.txt new file +20 -0
new file mode 100644
index 000000000000..d4168d79cceb
@@ -0,0 +1,20 @@
+* Cadence PCIe endpoint controller
+
+Required properties:
+- compatible: should contain "cdns,cdns-pcie-ep" to identify the IP used.
+- reg: should contain the controller register base address and AXI interface
+  region base address respectively.
+- reg-names: Must be "reg" and "mem" respectively.
+
+Optional properties:
+- max-functions: maximum number of functions that can be configured.
+
+Example:
+
+	pci@fc000000 {
+		compatible = "cdns,cdns-pcie-ep";
+		reg = <0x0 0xfc000000 0x0 0x01000000>, /* Base Address */
+		      <0x0 0x80000000 0x0 0x40000000>; /* AXI region */
+		reg-names = "reg", "mem";
+		max-functions = /bits/ 8 <8>;
+	};
Signature · 1 line
2.11.0
#23 lore raw

PATCH 5/5 PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
 	  mode. This PCIe controller may be embedded into many different vendors
 	  SoCs.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on PCI_ENDPOINT
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +553 -0

553 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer Acked-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe host controller

In reply to Cyrille Pitchen
Quoted 5 lines · Cyrille Pitchen

From: Scott Telford <[email protected]>

This patch adds documentation for the DT bindings of the Cadence PCIe controller when configured in host (Root Complex) mode.

Quoted 8 lines

dt-bindings: PCI: cadence: host fixup

---
 .../bindings/pci/cdns,cdns-pcie-host.txt           | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt

A few nits, otherwise:

Quoted 19 lines
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +12 -0
new file mode 100644
index 000000000000..4b3df8ffd5e1
@@ -0,0 +1,54 @@
+* Cadence PCIe host controller
+
+This PCIe controller inherits the base properties defined in
+host-generic-pci.txt.
+
+Required properties:
+- compatible: should contain "cdns,cdns-pcie-host" to identify the IP used.
+- reg: Should contain the PCIe configuration window base address, controller
+  register base address, and AXI interface region base address respectively.
+- reg-names: Must be "cfg", "reg" and "mem" respectively.
+- #address-cells: set to <3>
+- #size-cells: set to <2>

Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt line 22

Quoted 10 lines
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +10 -0
+- device_type: set to "pci"
+- ranges: ranges for the PCI memory and I/O regions
+- #interrupt-cells: set to <1>
+- interrupt-map-mask and interrupt-map: standard PCI
+	properties to define the mapping of the PCIe interface to interrupt
+	numbers.
+
+Example:
+
+	pci@fb000000 {

pcie@...

Usually the first address is the unit-address, so either change the order in reg or change this.

Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt line 30

Quoted 8 lines
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +8 -0
+		compatible = "cdns,cdns-pcie-host";
+		device_type = "pci";
+		#address-cells = <3>;
+		#size-cells = <2>;
+		bus-range = <0x0 0xff>;
+		linux,pci-domain = <0>;
+
+		// CPU_PHYSICAL(2)  SIZE(2)

Drop the comment. If you know what 'reg' is then it is obvious. Also, don't use C++ style comments.

Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt line 36

Quoted 6 lines
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +6 -0
+		reg = <0x0 0x41000000  0x0 0x00001000>, /* Config space */
+		      <0x0 0xfb000000  0x0 0x01000000>, /* Base Address */
+		      <0x0 0x40000000  0x0 0x04000000>; /* AXI region */
+		reg-names = "cfg", "reg", "mem";
+
+		// BUS_ADDRESS(3)  CPU_PHYSICAL(1)  SIZE(2)

ditto

Quoted 25 lines
Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt new file +18 -0
+		ranges =
+			<0x02000000 0x0 0x42000000  0x0 0x42000000  0x0 0x1000000>,
+			<0x01000000 0x0 0x43000000  0x0 0x43000000  0x0 0x0010000>;
+
+
+		#interrupt-cells = <0x1>;
+
+		// PCI_DEVICE(3)  INT#(1)  CONTROLLER(PHANDLE)  CONTROLLER_DATA(5)
+		interrupt-map = <0x0 0x0 0x0  0x1  &gic  0x0 0x0 0x0 14 0x1
+				 0x0 0x0 0x0  0x2  &gic  0x0 0x0 0x0 15 0x1
+				 0x0 0x0 0x0  0x3  &gic  0x0 0x0 0x0 16 0x1
+				 0x0 0x0 0x0  0x4  &gic  0x0 0x0 0x0 17 0x1>;
+
+		// PCI_DEVICE(3)  INT#(1)
+		interrupt-map-mask = <0x0000 0x0 0x0  0x7>;
+
+		msi-parent = <&its_pci>;
+	};
Signature · 6 lines
2.11.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer Acked-by on v1 4/5
#22 lore raw

Re: [PATCH 4/5] dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe endpoint controller

In reply to Cyrille Pitchen
Quoted 8 lines · Cyrille Pitchen

This patch documents the DT bindings for the Cadence PCIe controller when configured in endpoint mode.

---
 .../devicetree/bindings/pci/cdns,cdns-pcie-ep.txt    | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-ep.txt
#33 lore raw
In reply to Cyrille Pitchen
Quoted 20 lines · Cyrille Pitchen

Hi all,

this series of patches adds support to the Cadence PCIe controller. It was tested on a ARM64 platform emulated by a Palladium running both linux-next (next-20171123) and pci-next kernels.

The host mode was tested with some PCIe devices connected to the Palladium through a speed-bridge. Some of those devices were a USB host controller and a SATA controller. The PCIe host controller was also tested with a second controller configured in endpoint mode and connected back to back to the first controller.

The EndPoint Controller (EPC) driver of this series was tested with the pci-epf-test.c EndPoint Function (EPF) driver and the pcitest userspace program.

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Quoted 5 lines

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.

Nice :( - isn't there a way to improve this (ie probe deferral or registering the EPF bus earlier) ?

Quoted 2 lines

I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Or probably the other way around - see commit 5e8cb4033807

@Kishon, thoughts ?

Thanks,
Lorenzo

Quoted 40 lines

Best regards,

Cyrille

Cyrille Pitchen (4):
PCI: Add vendor ID for Cadence
PCI: cadence: Add host driver for Cadence PCIe controller
dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe endpoint
controller
PCI: cadence: add EndPoint Controller driver for Cadence PCIe
controller

Scott Telford (1):
dt-bindings: PCI: cadence: Add DT bindings for Cadence PCIe host
controller

 .../devicetree/bindings/pci/cdns,cdns-pcie-ep.txt  |  20 +
 .../bindings/pci/cdns,cdns-pcie-host.txt           |  54 ++
 drivers/Makefile                                   |   1 +
 drivers/pci/Kconfig                                |   1 +
 drivers/pci/cadence/Kconfig                        |  33 ++
 drivers/pci/cadence/Makefile                       |   3 +
 drivers/pci/cadence/pcie-cadence-ep.c              | 553 +++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence-host.c            | 425 ++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c                 | 110 ++++
 drivers/pci/cadence/pcie-cadence.h                 | 325 ++++++++++++
 include/linux/pci_ids.h                            |   2 +
 11 files changed, 1527 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-ep.txt
 create mode 100644 Documentation/devicetree/bindings/pci/cdns,cdns-pcie-host.txt
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
Signature · 1 line
2.11.0
#6 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen
Quoted 17 lines · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h

I prefer a single file per driver. I assume you're anticipating something like dwc, where the DesignWare core is incorporated into several devices in slightly different ways. But it doesn't look like that's here yet, and personally, I'd rather split out the required things when they actually become required, not ahead of time.

drivers/Makefile obj-y += pwm/ · line 21

Quoted 9 lines
drivers/Makefile +1 -0
index 1d034b680431..27bdd98784d9 100644
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/

I can't remember why we added CONFIG_PCI_ENDPOINT here instead of in drivers/pci/Makefile. Is there any way to move both CONFIG_PCI_ENDPOINT and CONFIG_PCI_CADENCE into drivers/pci/Makefile so this is better encapsulated?

drivers/Makefile obj-y += pwm/

Quoted 5 lines
drivers/Makefile +1 -0
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
...
 
+ * struct cdns_pcie_rc_data - hardware specific data

"cdns" is a weird abbreviation for "Cadence", since "Cadence" doesn't contain an "s".

Quoted 35 lines

...

drivers/Makefile +34 -0
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+						 struct list_head *resources,
+						 struct resource **bus_range)
+{
+	int err, res_valid = 0;
+	struct device_node *np = dev->of_node;
+	resource_size_t iobase;
+	struct resource_entry *win, *tmp;
+
+	err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+	if (err)
+		return err;
+
+	err = devm_request_pci_bus_resources(dev, resources);
+	if (err)
+		return err;
+
+	resource_list_for_each_entry_safe(win, tmp, resources) {
+		struct resource *res = win->res;
+
+		switch (resource_type(res)) {
+		case IORESOURCE_IO:
+			err = pci_remap_iospace(res, iobase);
+			if (err) {
+				dev_warn(dev, "error %d: failed to map resource %pR\n",
+					 err, res);
+				resource_list_destroy_entry(win);
+			}
+			break;
+		case IORESOURCE_MEM:
+			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+			break;
+		case IORESOURCE_BUS:
+			*bus_range = res;

drivers/Makefile

Quoted 10 lines
drivers/Makefile +10 -0
+			break;
+		}
+	}
+
+	if (res_valid)
+		return 0;
+
+	dev_err(dev, "non-prefetchable memory resource required\n");
+	return -EINVAL;
+}

The code above is starting to look awfully familiar. I wonder if it's time to think about some PCI-internal interface that can encapsulate this. In this case, there's really nothing Cadence-specific here. There are other callers where there *is* vendor-specific code, but possibly that could be handled by returning pointers to bus number, I/O port, and MMIO resources so the caller could do the vendor-specific stuff?

Bjorn

#7 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

Quoted 7 lines · Bjorn Helgaas
Quoted 6 lines · Cyrille Pitchen
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
Quoted 8 lines

drivers/Makefile obj-y += pwm/ · line 21

Quoted 3 lines
drivers/Makefile +1 -0
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/

I can't remember why we added CONFIG_PCI_ENDPOINT here instead of in drivers/pci/Makefile. Is there any way to move both CONFIG_PCI_ENDPOINT and CONFIG_PCI_CADENCE into drivers/pci/Makefile so this is better encapsulated?

I now see the note in your cover letter. If there is a reason for this, it should go in the changelog to forestall questions like this. But what's in the cover letter isn't quite enough because it doesn't answer the question of why drivers/pci/Makefile isn't sufficient to control the ordering of pci/dwc and pci/endpoint.

And sorry, I now see Lorenzo's similar query so he's already jumped on this :)

Quoted 2 lines
Quoted 2 lines
drivers/Makefile +0 -0
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
#8 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Bjorn Helgaas

Hello,

Quoted 4 lines · Bjorn Helgaas
Quoted 1 line

+ * struct cdns_pcie_rc_data - hardware specific data

"cdns" is a weird abbreviation for "Cadence", since "Cadence" doesn't contain an "s".

cdns is the official Device Tree binding vendor prefix for Cadence:

$ grep Cadence Documentation/devicetree/bindings/vendor-prefixes.txt
cdns	Cadence Design Systems Inc.

And it is already widely used throughout the kernel for Cadence drivers. See drivers/watchdog/cadence_wdt.c, drivers/spi/spi-cadence.c, drivers/i2c/busses/i2c-cadence.c, etc.

Best regards,

Thomas Petazzoni

Signature · 7 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Bjorn Helgaas

On Tue, Nov 28, 2017 at 02:41:14PM -0600, Bjorn Helgaas wrote:

[...]

Quoted 45 lines
Quoted 34 lines
drivers/pci/cadence/pcie-cadence-host.c new file +34 -0
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+						 struct list_head *resources,
+						 struct resource **bus_range)
+{
+	int err, res_valid = 0;
+	struct device_node *np = dev->of_node;
+	resource_size_t iobase;
+	struct resource_entry *win, *tmp;
+
+	err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+	if (err)
+		return err;
+
+	err = devm_request_pci_bus_resources(dev, resources);
+	if (err)
+		return err;
+
+	resource_list_for_each_entry_safe(win, tmp, resources) {
+		struct resource *res = win->res;
+
+		switch (resource_type(res)) {
+		case IORESOURCE_IO:
+			err = pci_remap_iospace(res, iobase);
+			if (err) {
+				dev_warn(dev, "error %d: failed to map resource %pR\n",
+					 err, res);
+				resource_list_destroy_entry(win);
+			}
+			break;
+		case IORESOURCE_MEM:
+			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+			break;
+		case IORESOURCE_BUS:
+			*bus_range = res;

drivers/pci/cadence/pcie-cadence-host.c line 167

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+			break;
+		}
+	}
+
+	if (res_valid)
+		return 0;
+
+	dev_err(dev, "non-prefetchable memory resource required\n");
+	return -EINVAL;
+}
Quoted 7 lines

The code above is starting to look awfully familiar. I wonder if it's time to think about some PCI-internal interface that can encapsulate this. In this case, there's really nothing Cadence-specific here. There are other callers where there *is* vendor-specific code, but possibly that could be handled by returning pointers to bus number, I/O port, and MMIO resources so the caller could do the vendor-specific stuff?

Yes and that's not the only one, pattern below is duplicated (with some minor differences across host bridges that I think can be managed through function parameters), it is probably worth moving them both into a core code helper.

list_splice_init(&resources, &bridge->windows);
bridge->dev.parent = dev;
bridge->busnr = bus;
bridge->ops = &pci_ops;
bridge->map_irq = of_irq_parse_and_map_pci;
bridge->swizzle_irq = pci_common_swizzle;

ret = pci_scan_root_bus_bridge(bridge);

if (ret < 0) {
	dev_err(dev, "Scanning root bridge failed");
	goto err_init;
}

bus = bridge->bus;
pci_bus_size_bridges(bus);
pci_bus_assign_resources(bus);

list_for_each_entry(child, &bus->children, node)

	pcie_bus_configure_settings(child);

pci_bus_add_devices(bus);

#9 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Thomas Petazzoni
Quoted 9 lines · Thomas Petazzoni

Hello,

Quoted 4 lines · Bjorn Helgaas
Quoted 1 line

+ * struct cdns_pcie_rc_data - hardware specific data

"cdns" is a weird abbreviation for "Cadence", since "Cadence" doesn't contain an "s".

Quoted 8 lines

cdns is the official Device Tree binding vendor prefix for Cadence:

$ grep Cadence Documentation/devicetree/bindings/vendor-prefixes.txt
cdns	Cadence Design Systems Inc.

And it is already widely used throughout the kernel for Cadence drivers. See drivers/watchdog/cadence_wdt.c, drivers/spi/spi-cadence.c, drivers/i2c/busses/i2c-cadence.c, etc.

Hard to argue with that! Thanks!

#13 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

Bjorn already commented on this, it would be good to add some of the cover letter details in this log.

Quoted 7 lines
---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +

drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++=

++++++

You should also update the MAINTAINERS file.

Quoted 14 lines
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644

drivers/Makefile obj-y +=3D pwm/ · line 21

Quoted 4 lines
drivers/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-y                               +=3D pwm/
 
 obj-$(CONFIG_PCI)            +=3D pci/
 obj-$(CONFIG_PCI_ENDPOINT)   +=3D pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)    +=3D pci/cadence/

Already commented on the cover letter.

Quoted 28 lines
drivers/Makefile +0 -0
 # PCI dwc controller drivers
 obj-y                                +=3D pci/dwc/
 
drivers/pci/Kconfig +1 -0
index 90944667ccea..2471b2e36b8b 100644
@@ -144,6 +144,7 @@ config PCI_HYPERV
           PCI devices from a PCI backend to support PCI driver domains.
 
 source "drivers/pci/hotplug/Kconfig"
+source "drivers/pci/cadence/Kconfig"
 source "drivers/pci/dwc/Kconfig"
 source "drivers/pci/host/Kconfig"
 source "drivers/pci/endpoint/Kconfig"
drivers/pci/cadence/Kconfig new file +7 -0
new file mode 100644
index 000000000000..120306cae2aa
@@ -0,0 +1,24 @@
+menuconfig PCI_CADENCE
+     bool "Cadence PCI controllers support"
+     depends on PCI && HAS_IOMEM
+     help
+       Say Y here if you want to support some Cadence PCI controller.
+
+       When in doubt, say N.

drivers/pci/cadence/Kconfig line 17

Quoted 10 lines
drivers/pci/cadence/Kconfig new file +10 -0
+
+if PCI_CADENCE
+
+config PCIE_CADENCE
+     bool
+
+config PCIE_CADENCE_HOST
+     bool "Cadence PCIe host controller"
+     depends on OF
+     depends on PCI_MSI_IRQ_DOMAIN

I do not see the reason for this dependency in the code.

drivers/pci/cadence/Kconfig

Quoted 3 lines
drivers/pci/cadence/Kconfig new file +3 -0
+     select PCIE_CADENCE
+     help
+       Say Y here if you want to support the Cadence PCIe controller in =

host

drivers/pci/cadence/Kconfig line 21

Quoted 1 line
drivers/pci/cadence/Kconfig new file +1 -0
+       mode. This PCIe controller may be embedded into many different ve=

ndors

drivers/pci/cadenc=

Quoted 12 lines
drivers/pci/cadence/Kconfig new file +3 -0
+       SoCs.
+
+endif # PCI_CADENCE
drivers/pci/cadence/Makefile new file +2 -0
new file mode 100644
index 000000000000..d57d192d2595
@@ -0,0 +1,2 @@
+obj-$(CONFIG_PCIE_CADENCE) +=3D pcie-cadence.o
+obj-$(CONFIG_PCIE_CADENCE_HOST) +=3D pcie-cadence-host.o
drivers/pci/cadenc= renamed from drivers/pci/cadence/pcie-cadence-host.c +0 -0

e/pcie-cadence-host.c

Quoted 12 lines

new file mode 100644
index 000000000000..252471e72a93

drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
@@ -0,0 +1,425 @@
+/*
+ * Cadence PCIe host controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *

drivers/pci/cadence/pcie-cadence-host.c line 17

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License alo=

ng with

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of_address.h>
+#include <linux/of_pci.h>
+#include <linux/platform_device.h>

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+#include <linux/pm_runtime.h>
+
+#include "pcie-cadence.h"
+
+/**
+ * struct cdns_pcie_rc_data - hardware specific data
+ * @max_regions: maximum number of regions supported by the hardware
+ * @vendor_id: PCI vendor ID
+ * @device_id: PCI device ID
+ * @no_bar_nbits: Number of bits to keep for inbound (PCIe -> CPU) addre=

ss

drivers/pci/cadence/pcie-cadence-host.c line 34

Quoted 1 line
drivers/pci/cadence/pcie-cadence-host.c new file +1 -0
+ *                translation (nbits sets into the "no BAR match" regist=

er).

drivers/pci/cadence/pcie-cadence-host.c line 37

Quoted 3 lines
drivers/pci/cadence/pcie-cadence-host.c new file +3 -0
+ */
+struct cdns_pcie_rc_data {
+     size_t                  max_regions;

Reason for it to be size_t ?

drivers/pci/cadence/pcie-cadence-host.c line 41

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+     u16                     vendor_id;
+     u16                     device_id;
+     u8                      no_bar_nbits;
+};

I think that this data should come from DT (?) more below.

drivers/pci/cadence/pcie-cadence-host.c

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-host.c new file +9 -0
+
+/**
+ * struct cdns_pcie_rc - private data for this PCIe Root Complex driver
+ * @pcie: Cadence PCIe controller
+ * @dev: pointer to PCIe device
+ * @cfg_res: start/end offsets in the physical system memory to map PCI
+ *           configuration space accesses
+ * @bus_range: first/last buses behind the PCIe host controller
+ * @cfg_base: IO mapped window to access the PCI configuration space of =

a

drivers/pci/cadence/pcie-cadence-host.c line 63

Quoted 13 lines
drivers/pci/cadence/pcie-cadence-host.c new file +13 -0
+ *            single function at a time
+ * @data: pointer to a 'struct cdns_pcie_rc_data'
+ */
+struct cdns_pcie_rc {
+     struct cdns_pcie        pcie;
+     struct device           *dev;
+     struct resource         *cfg_res;
+     struct resource         *bus_range;
+     void __iomem            *cfg_base;
+     const struct cdns_pcie_rc_data  *data;
+};
+
+static void __iomem *

Please do not split lines like this, storage class and return type should be in the same line as the name, move parameter(s) to a new line.

static void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int dev= fn,
int where)

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+     struct pci_host_bridge *bridge =3D pci_find_host_bridge(bus);
+     struct cdns_pcie_rc *rc =3D pci_host_bridge_priv(bridge);
+     struct cdns_pcie *pcie =3D &rc->pcie;
+     unsigned int busn =3D bus->number;
+     u32 addr0, desc0;
+
+     if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+             return NULL;

It does not hurt but I wonder whether you really need this check.

drivers/pci/cadence/pcie-cadence-host.c line 75

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     if (busn =3D=3D rc->bus_range->start) {
+             if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

drivers/pci/cadence/pcie-cadence-host.c

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+                     return NULL;
+
+             return pcie->reg_base + (where & 0xfff);
+     }
+
+     /* Update Output registers for AXI region 0. */
+     addr0 =3D CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+     /* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     desc0 =3D CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+             CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+     /*
+      * The bus number was already set once for all in desc1 by
+      * cdns_pcie_host_init_address_translation().
+      */
+     if (busn =3D=3D rc->bus_range->start + 1)
+             desc0 |=3D CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+     else
+             desc0 |=3D CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+     return rc->cfg_base + (where & 0xfff);
+}
+
+static struct pci_ops cdns_pcie_host_ops =3D {
+     .map_bus        =3D cdns_pci_map_bus,

drivers/pci/cadence/pcie-cadence-host.c line 114

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     .read           =3D pci_generic_config_read,
+     .write          =3D pci_generic_config_write,
+};
+
+static const struct cdns_pcie_rc_data cdns_pcie_rc_data =3D {
+     .max_regions    =3D 32,
+     .vendor_id      =3D PCI_VENDOR_ID_CDNS,
+     .device_id      =3D 0x0200,
+     .no_bar_nbits   =3D 32,
+};

Should (some of) these parameters be retrieved through a DT binding ?

drivers/pci/cadence/pcie-cadence-host.c

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-host.c new file +9 -0
+static const struct of_device_id cdns_pcie_host_of_match[] =3D {
+     { .compatible =3D "cdns,cdns-pcie-host",
+       .data =3D &cdns_pcie_rc_data },
+
+     { },
+};
+
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+                                              struct list_head *resource=

s,

drivers/pci/cadence/pcie-cadence-host.c line 124

Quoted 1 line
drivers/pci/cadence/pcie-cadence-host.c new file +1 -0
+                                              struct resource **bus_rang=

e)

drivers/pci/cadence/pcie-cadence-host.c

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+{
+     int err, res_valid =3D 0;
+     struct device_node *np =3D dev->of_node;
+     resource_size_t iobase;
+     struct resource_entry *win, *tmp;
+
+     err =3D of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &i=

obase);

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+     if (err)
+             return err;
+
+     err =3D devm_request_pci_bus_resources(dev, resources);
+     if (err)

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+             return err;
+
+     resource_list_for_each_entry_safe(win, tmp, resources) {
+             struct resource *res =3D win->res;
+
+             switch (resource_type(res)) {
+             case IORESOURCE_IO:
+                     err =3D pci_remap_iospace(res, iobase);
+                     if (err) {
+                             dev_warn(dev, "error %d: failed to map reso=

urce %pR\n",

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-host.c new file +8 -0
+                                      err, res);
+                             resource_list_destroy_entry(win);
+                     }
+                     break;
+             case IORESOURCE_MEM:
+                     res_valid |=3D !(res->flags & IORESOURCE_PREFETCH);
+                     break;
+             case IORESOURCE_BUS:

drivers/pci/cadence/pcie-cadence-host.c line 166

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+                     *bus_range =3D res;
+                     break;
+             }
+     }
+
+     if (res_valid)
+             return 0;
+
+     dev_err(dev, "non-prefetchable memory resource required\n");
+     return -EINVAL;

Nit, I prefer you swap these two as it is done in pci-aardvark.c:

if (!res_valid) {
dev_err(dev, "non-prefetchable memory resource required\n")=

;
                return -EINVAL;
        }

        return 0;

but as per previous replies this function can be factorized in core PCI code - I would not bother unless you are willing to write the patch series that does the refactoring yourself :)

Quoted 32 lines
drivers/pci/cadence/pcie-cadence-host.c new file +32 -0
+}
+
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data =3D rc->data;
+     struct cdns_pcie *pcie =3D &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl =3D CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value =3D CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id !=3D 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id !=3D 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn =3D rc->bus_range->start;
+     sbn =3D pbn + 1; /* Single root port. */
+     subn =3D rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

drivers/pci/cadence/pcie-cadence-host.c

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+     return 0;
+}
+
+static int cdns_pcie_host_init_address_translation(struct cdns_pcie_rc *=

rc)

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+{
+     struct cdns_pcie *pcie =3D &rc->pcie;
+     struct resource *cfg_res =3D rc->cfg_res;
+     struct resource *mem_res =3D pcie->mem_res;
+     struct resource *bus_range =3D rc->bus_range;
+     struct device *dev =3D rc->dev;

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     struct device_node *np =3D dev->of_node;
+     struct of_pci_range_parser parser;
+     struct of_pci_range range;
+     u32 addr0, addr1, desc1;
+     u64 cpu_addr;
+     int r, err;
+
+     /*
+      * Reserve region 0 for PCI configure space accesses:
+      * OB_REGION_PCI_ADDR0 and OB_REGION_DESC0 are updated dynamically =

by

drivers/pci/cadence/pcie-cadence-host.c line 227

Quoted 1 line
drivers/pci/cadence/pcie-cadence-host.c new file +1 -0
+      * cdns_pci_map_bus(), other region registers are set here once for=

all.

Quoted 15 lines
drivers/pci/cadence/pcie-cadence-host.c new file +15 -0
+      */
+     addr1 =3D 0; /* Should be programmed to zero. */
+     desc1 =3D CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus_range->start);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(0), addr1);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(0), desc1);
+
+     cpu_addr =3D cfg_res->start - mem_res->start;
+     addr0 =3D CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(12) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 =3D upper_32_bits(cpu_addr);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(0), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(0), addr1);
+
+     err =3D of_pci_range_parser_init(&parser, np);
+     if (err)

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+             return err;
+
+     r =3D 1;
+     for_each_of_pci_range(&parser, &range) {
+             bool is_io;
+
+             if (r >=3D rc->data->max_regions)
+                     break;
+
+             if ((range.flags & IORESOURCE_TYPE_BITS) =3D=3D IORESOURCE_=

MEM)

drivers/pci/cadence/pcie-cadence-host.c line 254

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+                     is_io =3D false;
+             else if ((range.flags & IORESOURCE_TYPE_BITS) =3D=3D IORESO=

URCE_IO)

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+                     is_io =3D true;
+             else
+                     continue;
+

drivers/pci/cadence/pcie-cadence-host.c line 271

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+             cdns_pcie_set_outbound_region(pcie, r, is_io,
+                                           range.cpu_addr,
+                                           range.pci_addr,
+                                           range.size);
+             r++;
+     }
+
+     /*
+      * Set Root Port no BAR match Inbound Translation registers:
+      * needed for MSI.

And DMA :) if I understand what this is doing correctly, ie setting the root complex decoding for incoming memory traffic.

drivers/pci/cadence/pcie-cadence-host.c

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+      * Root Port BAR0 and BAR1 are disabled, hence no need to set their
+      * inbound translation registers.
+      */
+     addr0 =3D CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(rc->data->no_bar_nbits=

);

drivers/pci/cadence/pcie-cadence-host.c line 274

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     addr1 =3D 0;
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR0(RP_NO_BAR), add=

r0);

drivers/pci/cadence/pcie-cadence-host.c line 275

Quoted 1 line
drivers/pci/cadence/pcie-cadence-host.c new file +1 -0
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR1(RP_NO_BAR), add=

r1);

drivers/pci/cadence/pcie-cadence-host.c

Quoted 13 lines
drivers/pci/cadence/pcie-cadence-host.c new file +13 -0
+
+     return 0;
+}
+
+static int cdns_pcie_host_init(struct device *dev,
+                            struct list_head *resources,
+                            struct cdns_pcie_rc *rc)
+{
+     struct resource *bus_range =3D NULL;
+     int err;
+
+     /* Parse our PCI ranges and request their resources */
+     err =3D cdns_pcie_parse_request_of_pci_ranges(dev, resources, &bus_=

range);

drivers/pci/cadence/pcie-cadence-host.c line 290

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     if (err)
+             goto err_out;

I think that the err_out path should be part of:

cdns_pcie_parse_request_of_pci_ranges()

implementation and here you would just return.

drivers/pci/cadence/pcie-cadence-host.c line 298

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+
+     if (bus_range->start > bus_range->end) {
+             err =3D -EINVAL;
+             goto err_out;
+     }

Add a space here; this check seems useless to me anyway.

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+     rc->bus_range =3D bus_range;
+     rc->pcie.bus =3D bus_range->start;
+
+     err =3D cdns_pcie_host_init_root_port(rc);
+     if (err)

drivers/pci/cadence/pcie-cadence-host.c line 313

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+             goto err_out;
+
+     err =3D cdns_pcie_host_init_address_translation(rc);
+     if (err)
+             goto err_out;
+
+     return 0;
+
+ err_out:
+     pci_free_resource_list(resources);

See above.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-host.c new file +14 -0
+     return err;
+}
+
+static int cdns_pcie_host_probe(struct platform_device *pdev)
+{
+     const struct of_device_id *of_id;
+     const char *type;
+     struct device *dev =3D &pdev->dev;
+     struct device_node *np =3D dev->of_node;
+     struct pci_bus *bus, *child;
+     struct pci_host_bridge *bridge;
+     struct list_head resources;
+     struct cdns_pcie_rc *rc;
+     struct cdns_pcie *pcie;

drivers/pci/cadence/pcie-cadence-host.c line 337

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     struct resource *res;
+     int ret;
+
+     bridge =3D devm_pci_alloc_host_bridge(dev, sizeof(*rc));
+     if (!bridge)
+             return -ENOMEM;
+
+     rc =3D pci_host_bridge_priv(bridge);
+     rc->dev =3D dev;
+     platform_set_drvdata(pdev, rc);

I do not think it is needed.

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+     pcie =3D &rc->pcie;
+     pcie->is_rc =3D true;
+
+     of_id =3D of_match_node(cdns_pcie_host_of_match, np);
+     rc->data =3D (const struct cdns_pcie_rc_data *)of_id->data;
+
+     type =3D of_get_property(np, "device_type", NULL);
+     if (!type || strcmp(type, "pci")) {
+             dev_err(dev, "invalid \"device_type\" %s\n", type);
+             return -EINVAL;
+     }

drivers/pci/cadence/pcie-cadence-host.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+
+     res =3D platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+     pcie->reg_base =3D devm_ioremap_resource(dev, res);
+     if (IS_ERR(pcie->reg_base)) {
+             dev_err(dev, "missing \"reg\"\n");
+             return PTR_ERR(pcie->reg_base);
+     }
+
+     res =3D platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
+     rc->cfg_base =3D devm_ioremap_resource(dev, res);

devm_pci_remap_cfg_resource() please.

drivers/pci/cadence/pci=

Quoted 67 lines
drivers/pci/cadence/pcie-cadence-host.c new file +66 -0
+     if (IS_ERR(rc->cfg_base)) {
+             dev_err(dev, "missing \"cfg\"\n");
+             return PTR_ERR(rc->cfg_base);
+     }
+     rc->cfg_res =3D res;
+
+     res =3D platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+     if (!res) {
+             dev_err(dev, "missing \"mem\"\n");
+             return -EINVAL;
+     }
+     pcie->mem_res =3D res;
+
+     pm_runtime_enable(dev);
+     ret =3D pm_runtime_get_sync(dev);
+     if (ret < 0) {
+             dev_err(dev, "pm_runtime_get_sync() failed\n");
+             goto err_get_sync;
+     }
+
+     INIT_LIST_HEAD(&resources);
+     ret =3D cdns_pcie_host_init(dev, &resources, rc);
+     if (ret)
+             goto err_init;
+
+     list_splice_init(&resources, &bridge->windows);
+     bridge->dev.parent =3D dev;
+     bridge->busnr =3D pcie->bus;
+     bridge->ops =3D &cdns_pcie_host_ops;
+     bridge->map_irq =3D of_irq_parse_and_map_pci;
+     bridge->swizzle_irq =3D pci_common_swizzle;
+
+     ret =3D pci_scan_root_bus_bridge(bridge);
+     if (ret < 0) {
+             dev_err(dev, "Scanning root bridge failed");
+             goto err_init;
+     }
+
+     bus =3D bridge->bus;
+     pci_bus_size_bridges(bus);
+     pci_bus_assign_resources(bus);
+
+     list_for_each_entry(child, &bus->children, node)
+             pcie_bus_configure_settings(child);
+
+     pci_bus_add_devices(bus);
+
+     return 0;
+
+ err_init:
+     pm_runtime_put_sync(dev);
+
+ err_get_sync:
+     pm_runtime_disable(dev);
+
+     return ret;
+}
+
+static struct platform_driver cdns_pcie_host_driver =3D {
+     .driver =3D {
+             .name =3D "cdns-pcie-host",
+             .of_match_table =3D cdns_pcie_host_of_match,
+     },
+     .probe =3D cdns_pcie_host_probe,
+};
+builtin_platform_driver(cdns_pcie_host_driver);
drivers/pci/cadence/pci= renamed from drivers/pci/cadence/pcie-cadence.c +0 -0

e-cadence.c

Quoted 12 lines

new file mode 100644
index 000000000000..5c10879d5e96

drivers/pci/cadence/pcie-cadence.c new file +7 -0
@@ -0,0 +1,110 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *

drivers/pci/cadence/pcie-cadence.c line 17

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.c new file +10 -0
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License alo=

ng with

drivers/pci/cadence/pcie-cadence.c

Quoted 8 lines
drivers/pci/cadence/pcie-cadence.c new file +8 -0
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/kernel.h>
+
+#include "pcie-cadence.h"
+
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool i=

s_io,

drivers/pci/cadence/pcie-cadence.c

Quoted 4 lines
drivers/pci/cadence/pcie-cadence.c new file +4 -0
+                                u64 cpu_addr, u64 pci_addr, size_t size)
+{
+     /*
+      * roundup_pow_of_two() returns an unsigned long, which is not suit=

ed

Quoted 34 lines
drivers/pci/cadence/pcie-cadence.c new file +34 -0
+      * for 64bit values.
+      */
+     u64 sz =3D 1ULL << fls64(size - 1);
+     int nbits =3D ilog2(sz);
+     u32 addr0, addr1, desc0, desc1;
+
+     if (nbits < 8)
+             nbits =3D 8;
+
+     /* Set the PCI address */
+     addr0 =3D CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
+             (lower_32_bits(pci_addr) & GENMASK(31, 8));
+     addr1 =3D upper_32_bits(pci_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
+
+     /* Set the PCIe header descriptor */
+     if (is_io)
+             desc0 =3D CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
+     else
+             desc0 =3D CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
+     desc1 =3D 0;
+
+     if (pcie->is_rc) {
+             desc0 |=3D CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+                      CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+             desc1 |=3D CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+     }
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+
+     /* Set the CPU address */

drivers/pci/cadence/pcie-cadence.c

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.c new file +10 -0
+     cpu_addr -=3D pcie->mem_res->start;
+     addr0 =3D CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 =3D upper_32_bits(cpu_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie=

, u32 r,

drivers/pci/cadence/pcie-cadence.c line 74

Quoted 1 line
drivers/pci/cadence/pcie-cadence.c new file +1 -0
+                                               u64 cpu_addr)

Not used in this patch, you should split it out.

drivers/pci/cadence/pci=

Quoted 37 lines
drivers/pci/cadence/pcie-cadence.c new file +36 -0
+{
+     u32 addr0, addr1, desc0, desc1;
+
+     desc0 =3D CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
+     desc1 =3D 0;
+     if (pcie->is_rc) {
+             desc0 |=3D CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+                      CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+             desc1 |=3D CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+     }
+
+     /* Set the CPU address */
+     cpu_addr -=3D pcie->mem_res->start;
+     addr0 =3D CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 =3D upper_32_bits(cpu_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
+{
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
+}
drivers/pci/cadence/pci= renamed from drivers/pci/cadence/pcie-cadence.h +0 -0

e-cadence.h

Quoted 12 lines

new file mode 100644
index 000000000000..195e23b7d4fe

drivers/pci/cadence/pcie-cadence.h new file +7 -0
@@ -0,0 +1,325 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *

drivers/pci/cadence/pcie-cadence.h line 17

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License alo=

ng with

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PCIE_CADENCE_H
+#define _PCIE_CADENCE_H
+
+#include <linux/kernel.h>
+#include <linux/pci.h>
+
+/*

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+ * Local Management Registers
+ */
+#define CDNS_PCIE_LM_BASE    0x00100000
+
+/* Vendor ID Register */
+#define CDNS_PCIE_LM_ID              (CDNS_PCIE_LM_BASE + 0x0044)
+#define  CDNS_PCIE_LM_ID_VENDOR_MASK GENMASK(15, 0)
+#define  CDNS_PCIE_LM_ID_VENDOR_SHIFT        0
+#define  CDNS_PCIE_LM_ID_VENDOR(vid) \
+     (((vid) << CDNS_PCIE_LM_ID_VENDOR_SHIFT) & CDNS_PCIE_LM_ID_VENDOR_M=

ASK)

drivers/pci/cadence/pcie-cadence.h

Quoted 4 lines
drivers/pci/cadence/pcie-cadence.h new file +4 -0
+#define  CDNS_PCIE_LM_ID_SUBSYS_MASK GENMASK(31, 16)
+#define  CDNS_PCIE_LM_ID_SUBSYS_SHIFT        16
+#define  CDNS_PCIE_LM_ID_SUBSYS(sub) \
+     (((sub) << CDNS_PCIE_LM_ID_SUBSYS_SHIFT) & CDNS_PCIE_LM_ID_SUBSYS_M=

ASK)

Quoted 14 lines
drivers/pci/cadence/pcie-cadence.h new file +14 -0
+
+/* Root Port Requestor ID Register */
+#define CDNS_PCIE_LM_RP_RID  (CDNS_PCIE_LM_BASE + 0x0228)
+#define  CDNS_PCIE_LM_RP_RID_MASK    GENMASK(15, 0)
+#define  CDNS_PCIE_LM_RP_RID_SHIFT   0
+#define  CDNS_PCIE_LM_RP_RID_(rid) \
+     (((rid) << CDNS_PCIE_LM_RP_RID_SHIFT) & CDNS_PCIE_LM_RP_RID_MASK)
+
+/* Endpoint Bus and Device Number Register */
+#define CDNS_PCIE_LM_EP_ID   (CDNS_PCIE_LM_BASE + 0x022c)
+#define  CDNS_PCIE_LM_EP_ID_DEV_MASK GENMASK(4, 0)
+#define  CDNS_PCIE_LM_EP_ID_DEV_SHIFT        0
+#define  CDNS_PCIE_LM_EP_ID_BUS_MASK GENMASK(15, 8)
+#define  CDNS_PCIE_LM_EP_ID_BUS_SHIFT        8

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+
+/* Endpoint Function f BAR b Configuration Registers */
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn) \
+     (CDNS_PCIE_LM_BASE + 0x0240 + (fn) * 0x0008)
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn) \
+     (CDNS_PCIE_LM_BASE + 0x0244 + (fn) * 0x0008)
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) \
+     (GENMASK(4, 0) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, a) \
+     (((a) << ((b) * 8)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MAS=

K(b))

drivers/pci/cadence/pcie-cadence.h

Quoted 4 lines
drivers/pci/cadence/pcie-cadence.h new file +4 -0
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b) \
+     (GENMASK(7, 5) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, c) \
+     (((c) << ((b) * 8 + 5)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MAS=

K(b))

drivers/pci/cadence/pcie-cadence.h line 72

Quoted 3 lines
drivers/pci/cadence/pcie-cadence.h new file +3 -0
+
+/* Endpoint Function Configuration Register */
+#define CDNS_PCIE_LM_EP_FUNC_CFG     (CDNS_PCIE_LM_BASE + 0x02c0)

All endpoint defines should be moved to the patch that needs them.

drivers/pci/cadence/pcie-cadence.h

Quoted 7 lines
drivers/pci/cadence/pcie-cadence.h new file +7 -0
+
+/* Root Complex BAR Configuration Register */
+#define CDNS_PCIE_LM_RC_BAR_CFG      (CDNS_PCIE_LM_BASE + 0x0300)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK  GENMASK(5, 0)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE(a) \
+     (((a) << 0) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK              GENMASK(8, =

6)

drivers/pci/cadence/pcie-cadence.h

Quoted 6 lines
drivers/pci/cadence/pcie-cadence.h new file +6 -0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(c) \
+     (((c) << 6) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK  GENMASK(13, 9)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE(a) \
+     (((a) << 9) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK              GENMASK(16,=

14)

drivers/pci/cadence/pcie-cadence.h

Quoted 11 lines
drivers/pci/cadence/pcie-cadence.h new file +11 -0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(c) \
+     (((c) << 14) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE BIT(17)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_32BITS 0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS BIT(18)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE           BIT(19)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_16BITS           0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS           BIT(20)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_CHECK_ENABLE                BIT(31)
+
+/* BAR control values applicable to both Endpoint Function and Root Comp=

lex */

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED          0x0
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS         0x1
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS                0x4
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS       0x5
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS                0x6
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS       0x7
+
+
+/*
+ * Endpoint Function Registers (PCI configuration space for endpoint fun=

ctions)

drivers/pci/cadence/pcie-cadence.h

Quoted 7 lines
drivers/pci/cadence/pcie-cadence.h new file +7 -0
+ */
+#define CDNS_PCIE_EP_FUNC_BASE(fn)   (((fn) << 12) & GENMASK(19, 12))
+
+#define CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET     0x90
+
+/*
+ * Root Port Registers (PCI configuration space for the root port functi=

on)

Quoted 20 lines
drivers/pci/cadence/pcie-cadence.h new file +20 -0
+ */
+#define CDNS_PCIE_RP_BASE    0x00200000
+
+
+/*
+ * Address Translation Registers
+ */
+#define CDNS_PCIE_AT_BASE    0x00400000
+
+/* Region r Outbound AXI to PCIe Address Translation Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0000 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK GENMASK(19, 12)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) \
+     (((devfn) << 12) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK   GENMASK(27, 20)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(bus) \

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+     (((bus) << 20) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK)
+
+/* Region r Outbound AXI to PCIe Address Translation Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r) \
+     (CDNS_PCIE_AT_BASE + 0x0004 + ((r) & 0x1f) * 0x0020)
+
+/* Region r Outbound PCIe Descriptor Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_DESC0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0008 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MASK              GENMASK(3, =

0)

Quoted 36 lines
drivers/pci/cadence/pcie-cadence.h new file +36 -0
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM               0x2
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO                0x6
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0        0xa
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1        0xb
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG        0xc
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_VENDOR_MSG        0xd
+/* Bit 23 MUST be set in RC mode. */
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID  BIT(23)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK     GENMASK(31, 24)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(devfn) \
+     (((devfn) << 24) & CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK)
+
+/* Region r Outbound PCIe Descriptor Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_DESC1(r)      \
+     (CDNS_PCIE_AT_BASE + 0x000c + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK       GENMASK(7, 0)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus) \
+     ((bus) & CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK)
+
+/* Region r AXI Region Base Address Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0018 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK)
+
+/* Region r AXI Region Base Address Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r) \
+     (CDNS_PCIE_AT_BASE + 0x001c + ((r) & 0x1f) * 0x0020)
+
+/* Root Port BAR Inbound PCIe to AXI Address Translation Register */
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR0(bar) \
+     (CDNS_PCIE_AT_BASE + 0x0800 + (bar) * 0x0008)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK     GENMASK(5, 0)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK)

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR1(bar) \
+     (CDNS_PCIE_AT_BASE + 0x0804 + (bar) * 0x0008)
+
+enum cdns_pcie_rp_bar {
+     RP_BAR0,
+     RP_BAR1,
+     RP_NO_BAR
+};
+
+/* Endpoint Function BAR Inbound PCIe to AXI Address Translation Registe=

r */

drivers/pci/cadence/pcie-cadence.h

Quoted 6 lines
drivers/pci/cadence/pcie-cadence.h new file +6 -0
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar) \
+     (CDNS_PCIE_AT_BASE + 0x0840 + (fn) * 0x0040 + (bar) * 0x0008)
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar) \
+     (CDNS_PCIE_AT_BASE + 0x0844 + (fn) * 0x0040 + (bar) * 0x0008)
+
+/* Normal/Vendor specific message access: offset inside some outbound re=

gion */

Quoted 33 lines
drivers/pci/cadence/pcie-cadence.h new file +33 -0
+#define CDNS_PCIE_NORMAL_MSG_ROUTING_MASK    GENMASK(7, 5)
+#define CDNS_PCIE_NORMAL_MSG_ROUTING(route) \
+     (((route) << 5) & CDNS_PCIE_NORMAL_MSG_ROUTING_MASK)
+#define CDNS_PCIE_NORMAL_MSG_CODE_MASK               GENMASK(15, 8)
+#define CDNS_PCIE_NORMAL_MSG_CODE(code) \
+     (((code) << 8) & CDNS_PCIE_NORMAL_MSG_CODE_MASK)
+#define CDNS_PCIE_MSG_NO_DATA                        BIT(16)
+
+enum cdns_pcie_msg_code {
+     MSG_CODE_ASSERT_INTA    =3D 0x20,
+     MSG_CODE_ASSERT_INTB    =3D 0x21,
+     MSG_CODE_ASSERT_INTC    =3D 0x22,
+     MSG_CODE_ASSERT_INTD    =3D 0x23,
+     MSG_CODE_DEASSERT_INTA  =3D 0x24,
+     MSG_CODE_DEASSERT_INTB  =3D 0x25,
+     MSG_CODE_DEASSERT_INTC  =3D 0x26,
+     MSG_CODE_DEASSERT_INTD  =3D 0x27,
+};
+
+enum cdns_pcie_msg_routing {
+     /* Route to Root Complex */
+     MSG_ROUTING_TO_RC,
+
+     /* Use Address Routing */
+     MSG_ROUTING_BY_ADDR,
+
+     /* Use ID Routing */
+     MSG_ROUTING_BY_ID,
+
+     /* Route as Broadcast Message from Root Complex */
+     MSG_ROUTING_BCAST,
+
+     /* Local message; terminate at receiver (INTx messages) */

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+     MSG_ROUTING_LOCAL,
+
+     /* Gather & route to Root Complex (PME_TO_Ack message) */
+     MSG_ROUTING_GATHER,
+};
+
+/**
+ * struct cdns_pcie - private data for Cadence PCIe controller drivers
+ * @reg_base: IO mapped register base
+ * @mem_res: start/end offsets in the physical system memory to map PCI =

accesses

drivers/pci/cadence/pcie-cadence.h line 239

Quoted 1 line
drivers/pci/cadence/pcie-cadence.h new file +1 -0
+ * @is_rc: tell whether the PCIe controller mode is Root Complex or Endp=

oint.

drivers/pci/cadence/pcie-cadence.h

Quoted 11 lines
drivers/pci/cadence/pcie-cadence.h new file +11 -0
+ * @bus: In Root Complex mode, the bus number
+ */
+struct cdns_pcie {
+     void __iomem            *reg_base;
+     struct resource         *mem_res;
+     bool                    is_rc;
+     u8                      bus;
+};
+
+/* Register access */
+static inline void cdns_pcie_writeb(struct cdns_pcie *pcie, u32 reg, u8 =

value)

drivers/pci/cadence/pcie-cadence.h

Quoted 5 lines
drivers/pci/cadence/pcie-cadence.h new file +5 -0
+{
+     writeb(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writew(struct cdns_pcie *pcie, u32 reg, u16=

value)

drivers/pci/cadence/pcie-cadence.h

Quoted 5 lines
drivers/pci/cadence/pcie-cadence.h new file +5 -0
+{
+     writew(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writel(struct cdns_pcie *pcie, u32 reg, u32=

value)

Quoted 32 lines
drivers/pci/cadence/pcie-cadence.h new file +32 -0
+{
+     writel(value, pcie->reg_base + reg);
+}
+
+static inline u32 cdns_pcie_readl(struct cdns_pcie *pcie, u32 reg)
+{
+     return readl(pcie->reg_base + reg);
+}
+
+/* Root Port register access */
+static inline void cdns_pcie_rp_writeb(struct cdns_pcie *pcie,
+                                    u32 reg, u8 value)
+{
+     writeb(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+static inline void cdns_pcie_rp_writew(struct cdns_pcie *pcie,
+                                    u32 reg, u16 value)
+{
+     writew(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+/* Endpoint Function register access */
+static inline void cdns_pcie_ep_fn_writeb(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u8 value)
+{
+     writeb(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writew(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u16 value)
+{

drivers/pci/cadence/pcie-cadence.h

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+     writew(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writel(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u16 value)
+{
+     writel(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u8 cdns_pcie_ep_fn_readb(struct cdns_pcie *pcie, u8 fn, u3=

2 reg)

drivers/pci/cadence/pcie-cadence.h

Quoted 5 lines
drivers/pci/cadence/pcie-cadence.h new file +5 -0
+{
+     return readb(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u16 cdns_pcie_ep_fn_readw(struct cdns_pcie *pcie, u8 fn, u=

32 reg)

drivers/pci/cadence/pcie-cadence.h

Quoted 5 lines
drivers/pci/cadence/pcie-cadence.h new file +5 -0
+{
+     return readw(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u32 cdns_pcie_ep_fn_readl(struct cdns_pcie *pcie, u8 fn, u=

32 reg)

drivers/pci/cadence/pcie-cadence.h line 315

Quoted 3 lines
drivers/pci/cadence/pcie-cadence.h new file +3 -0
+{
+     return readl(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}

Same comments for all endpoint related functions and defines above.

Thanks,
Lorenzo

drivers/pci/cadence/pcie-cadence.h line 316

Quoted 1 line
drivers/pci/cadence/pcie-cadence.h new file +1 -0
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool i=

s_io,

drivers/pci/cadence/pcie-cadence.h line 317

Quoted 1 line
drivers/pci/cadence/pcie-cadence.h new file +1 -0
+                                u64 cpu_addr, u64 pci_addr, size_t size)=

;

drivers/pci/cadence/pcie-cadence.h line 319

Quoted 2 lines
drivers/pci/cadence/pcie-cadence.h new file +2 -0
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie=

, u32 r,

Quoted 8 lines
drivers/pci/cadence/pcie-cadence.h new file +5 -1
+                                               u64 cpu_addr);
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r);
+
+#endif /* _PCIE_CADENCE_H */
--

2.11.0

IMPORTANT NOTICE: The contents of this email and any attachments are confid= ential and may also be privileged. If you are not the intended recipient, p= lease notify the sender immediately and do not disclose the contents to any=
other person, use it for any purpose, or store or copy the information in =
any medium. Thank you.

#19 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen

[w/o unintended disclaimer]

Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

Bjorn already commented on this, it would be good to add some of the cover letter details in this log.

Quoted 7 lines
---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++

You should also update the MAINTAINERS file.

Quoted 14 lines
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644

drivers/Makefile obj-y += pwm/ · line 21

Quoted 4 lines
drivers/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/

Already commented on the cover letter.

Quoted 28 lines
drivers/Makefile +0 -0
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
 
drivers/pci/Kconfig +1 -0
index 90944667ccea..2471b2e36b8b 100644
@@ -144,6 +144,7 @@ config PCI_HYPERV
           PCI devices from a PCI backend to support PCI driver domains.
 
 source "drivers/pci/hotplug/Kconfig"
+source "drivers/pci/cadence/Kconfig"
 source "drivers/pci/dwc/Kconfig"
 source "drivers/pci/host/Kconfig"
 source "drivers/pci/endpoint/Kconfig"
drivers/pci/cadence/Kconfig new file +7 -0
new file mode 100644
index 000000000000..120306cae2aa
@@ -0,0 +1,24 @@
+menuconfig PCI_CADENCE
+	bool "Cadence PCI controllers support"
+	depends on PCI && HAS_IOMEM
+	help
+	  Say Y here if you want to support some Cadence PCI controller.
+
+	  When in doubt, say N.

drivers/pci/cadence/Kconfig line 17

Quoted 10 lines
drivers/pci/cadence/Kconfig new file +10 -0
+
+if PCI_CADENCE
+
+config PCIE_CADENCE
+	bool
+
+config PCIE_CADENCE_HOST
+	bool "Cadence PCIe host controller"
+	depends on OF
+	depends on PCI_MSI_IRQ_DOMAIN

I do not see the reason for this dependency in the code.

Quoted 48 lines
drivers/pci/cadence/Kconfig new file +7 -0
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe controller in host
+	  mode. This PCIe controller may be embedded into many different vendors
+	  SoCs.
+
+endif # PCI_CADENCE
drivers/pci/cadence/Makefile new file +2 -0
new file mode 100644
index 000000000000..d57d192d2595
@@ -0,0 +1,2 @@
+obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
+obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
drivers/pci/cadence/pcie-cadence-host.c new file +27 -0
new file mode 100644
index 000000000000..252471e72a93
@@ -0,0 +1,425 @@
+/*
+ * Cadence PCIe host controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of_address.h>
+#include <linux/of_pci.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+
+#include "pcie-cadence.h"
+

drivers/pci/cadence/pcie-cadence-host.c line 37

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+/**
+ * struct cdns_pcie_rc_data - hardware specific data
+ * @max_regions: maximum number of regions supported by the hardware
+ * @vendor_id: PCI vendor ID
+ * @device_id: PCI device ID
+ * @no_bar_nbits: Number of bits to keep for inbound (PCIe -> CPU) address
+ *                translation (nbits sets into the "no BAR match" register).
+ */
+struct cdns_pcie_rc_data {
+	size_t			max_regions;

Reason for it to be size_t ?

drivers/pci/cadence/pcie-cadence-host.c line 41

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+	u16			vendor_id;
+	u16			device_id;
+	u8			no_bar_nbits;
+};

I think that this data should come from DT (?) more below.

Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +12 -0
+
+/**
+ * struct cdns_pcie_rc - private data for this PCIe Root Complex driver
+ * @pcie: Cadence PCIe controller
+ * @dev: pointer to PCIe device
+ * @cfg_res: start/end offsets in the physical system memory to map PCI
+ *           configuration space accesses
+ * @bus_range: first/last buses behind the PCIe host controller
+ * @cfg_base: IO mapped window to access the PCI configuration space of a
+ *            single function at a time
+ * @data: pointer to a 'struct cdns_pcie_rc_data'
+ */

drivers/pci/cadence/pcie-cadence-host.c line 63

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+struct cdns_pcie_rc {
+	struct cdns_pcie	pcie;
+	struct device		*dev;
+	struct resource		*cfg_res;
+	struct resource		*bus_range;
+	void __iomem		*cfg_base;
+	const struct cdns_pcie_rc_data	*data;
+};
+
+static void __iomem *

Please do not split lines like this, storage class and return type should be in the same line as the name, move parameter(s) to a new line.

static void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
int where)

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
+	struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge);
+	struct cdns_pcie *pcie = &rc->pcie;
+	unsigned int busn = bus->number;
+	u32 addr0, desc0;
+
+	if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+		return NULL;

It does not hurt but I wonder whether you really need this check.

drivers/pci/cadence/pcie-cadence-host.c line 76

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+	if (busn == rc->bus_range->start) {
+		if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

drivers/pci/cadence/pcie-cadence-host.c line 83

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+			return NULL;
+
+		return pcie->reg_base + (where & 0xfff);
+	}
+
+	/* Update Output registers for AXI region 0. */
+	addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+		CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+		CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+	/* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c line 98

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+	desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+		CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+	/*
+	 * The bus number was already set once for all in desc1 by
+	 * cdns_pcie_host_init_address_translation().
+	 */
+	if (busn == rc->bus_range->start + 1)
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+	else
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+	return rc->cfg_base + (where & 0xfff);
+}
+
+static struct pci_ops cdns_pcie_host_ops = {
+	.map_bus	= cdns_pci_map_bus,

drivers/pci/cadence/pcie-cadence-host.c line 115

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+	.read		= pci_generic_config_read,
+	.write		= pci_generic_config_write,
+};
+
+static const struct cdns_pcie_rc_data cdns_pcie_rc_data = {
+	.max_regions	= 32,
+	.vendor_id	= PCI_VENDOR_ID_CDNS,
+	.device_id	= 0x0200,
+	.no_bar_nbits	= 32,
+};

Should (some of) these parameters be retrieved through a DT binding ?

Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+static const struct of_device_id cdns_pcie_host_of_match[] = {
+	{ .compatible = "cdns,cdns-pcie-host",
+	  .data = &cdns_pcie_rc_data },
+
+	{ },
+};
+
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+						 struct list_head *resources,
+						 struct resource **bus_range)
+{
+	int err, res_valid = 0;
+	struct device_node *np = dev->of_node;
+	resource_size_t iobase;
+	struct resource_entry *win, *tmp;
+
+	err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+	if (err)
+		return err;
+
+	err = devm_request_pci_bus_resources(dev, resources);
+	if (err)
+		return err;
+
+	resource_list_for_each_entry_safe(win, tmp, resources) {
+		struct resource *res = win->res;
+
+		switch (resource_type(res)) {
+		case IORESOURCE_IO:
+			err = pci_remap_iospace(res, iobase);
+			if (err) {
+				dev_warn(dev, "error %d: failed to map resource %pR\n",
+					 err, res);
+				resource_list_destroy_entry(win);
+			}
+			break;
+		case IORESOURCE_MEM:
+			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+			break;
+		case IORESOURCE_BUS:

drivers/pci/cadence/pcie-cadence-host.c line 166

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+			*bus_range = res;
+			break;
+		}
+	}
+
+	if (res_valid)
+		return 0;
+
+	dev_err(dev, "non-prefetchable memory resource required\n");
+	return -EINVAL;

Nit, I prefer you swap these two as it is done in pci-aardvark.c:

	if (!res_valid) {
		dev_err(dev, "non-prefetchable memory resource required\n");
		return -EINVAL;
	}

	return 0;

but as per previous replies this function can be factorized in core PCI code - I would not bother unless you are willing to write the patch series that does the refactoring yourself :)

Quoted 32 lines
drivers/pci/cadence/pcie-cadence-host.c new file +32 -0
+}
+
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+	const struct cdns_pcie_rc_data *data = rc->data;
+	struct cdns_pcie *pcie = &rc->pcie;
+	u8 pbn, sbn, subn;
+	u32 value, ctrl;
+
+	/*
+	 * Set the root complex BAR configuration register:
+	 * - disable both BAR0 and BAR1.
+	 * - enable Prefetchable Memory Base and Limit registers in type 1
+	 *   config space (64 bits).
+	 * - enable IO Base and Limit registers in type 1 config
+	 *   space (32 bits).
+	 */
+	ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+	value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+		CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+		CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+		CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+		CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+		CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+	/* Set root port configuration space */
+	if (data->vendor_id != 0xffff)
+		cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+	if (data->device_id != 0xffff)
+		cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+	cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+	cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+	cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+	pbn = rc->bus_range->start;
+	sbn = pbn + 1; /* Single root port. */
+	subn = rc->bus_range->end;
+	cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
+	cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+	cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

Quoted 52 lines
drivers/pci/cadence/pcie-cadence-host.c new file +52 -0
+	return 0;
+}
+
+static int cdns_pcie_host_init_address_translation(struct cdns_pcie_rc *rc)
+{
+	struct cdns_pcie *pcie = &rc->pcie;
+	struct resource *cfg_res = rc->cfg_res;
+	struct resource *mem_res = pcie->mem_res;
+	struct resource *bus_range = rc->bus_range;
+	struct device *dev = rc->dev;
+	struct device_node *np = dev->of_node;
+	struct of_pci_range_parser parser;
+	struct of_pci_range range;
+	u32 addr0, addr1, desc1;
+	u64 cpu_addr;
+	int r, err;
+
+	/*
+	 * Reserve region 0 for PCI configure space accesses:
+	 * OB_REGION_PCI_ADDR0 and OB_REGION_DESC0 are updated dynamically by
+	 * cdns_pci_map_bus(), other region registers are set here once for all.
+	 */
+	addr1 = 0; /* Should be programmed to zero. */
+	desc1 = CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus_range->start);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(0), addr1);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(0), desc1);
+
+	cpu_addr = cfg_res->start - mem_res->start;
+	addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(12) |
+		(lower_32_bits(cpu_addr) & GENMASK(31, 8));
+	addr1 = upper_32_bits(cpu_addr);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(0), addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(0), addr1);
+
+	err = of_pci_range_parser_init(&parser, np);
+	if (err)
+		return err;
+
+	r = 1;
+	for_each_of_pci_range(&parser, &range) {
+		bool is_io;
+
+		if (r >= rc->data->max_regions)
+			break;
+
+		if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
+			is_io = false;
+		else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
+			is_io = true;
+		else
+			continue;
+

drivers/pci/cadence/pcie-cadence-host.c line 271

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+		cdns_pcie_set_outbound_region(pcie, r, is_io,
+					      range.cpu_addr,
+					      range.pci_addr,
+					      range.size);
+		r++;
+	}
+
+	/*
+	 * Set Root Port no BAR match Inbound Translation registers:
+	 * needed for MSI.

And DMA :) if I understand what this is doing correctly, ie setting the root complex decoding for incoming memory traffic.

Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +12 -0
+	 * Root Port BAR0 and BAR1 are disabled, hence no need to set their
+	 * inbound translation registers.
+	 */
+	addr0 = CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(rc->data->no_bar_nbits);
+	addr1 = 0;
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR0(RP_NO_BAR), addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR1(RP_NO_BAR), addr1);
+
+	return 0;
+}
+
+static int cdns_pcie_host_init(struct device *dev,

drivers/pci/cadence/pcie-cadence-host.c line 293

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+			       struct list_head *resources,
+			       struct cdns_pcie_rc *rc)
+{
+	struct resource *bus_range = NULL;
+	int err;
+
+	/* Parse our PCI ranges and request their resources */
+	err = cdns_pcie_parse_request_of_pci_ranges(dev, resources, &bus_range);
+	if (err)
+		goto err_out;

I think that the err_out path should be part of:

cdns_pcie_parse_request_of_pci_ranges()

implementation and here you would just return.

drivers/pci/cadence/pcie-cadence-host.c line 298

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+
+	if (bus_range->start > bus_range->end) {
+		err = -EINVAL;
+		goto err_out;
+	}

Add a space here; this check seems useless to me anyway.

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+	rc->bus_range = bus_range;
+	rc->pcie.bus = bus_range->start;
+
+	err = cdns_pcie_host_init_root_port(rc);
+	if (err)

drivers/pci/cadence/pcie-cadence-host.c line 313

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+		goto err_out;
+
+	err = cdns_pcie_host_init_address_translation(rc);
+	if (err)
+		goto err_out;
+
+	return 0;
+
+ err_out:
+	pci_free_resource_list(resources);

See above.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-host.c new file +14 -0
+	return err;
+}
+
+static int cdns_pcie_host_probe(struct platform_device *pdev)
+{
+	const struct of_device_id *of_id;
+	const char *type;
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct pci_bus *bus, *child;
+	struct pci_host_bridge *bridge;
+	struct list_head resources;
+	struct cdns_pcie_rc *rc;
+	struct cdns_pcie *pcie;

drivers/pci/cadence/pcie-cadence-host.c line 337

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+	struct resource *res;
+	int ret;
+
+	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
+	if (!bridge)
+		return -ENOMEM;
+
+	rc = pci_host_bridge_priv(bridge);
+	rc->dev = dev;
+	platform_set_drvdata(pdev, rc);

I do not think it is needed.

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+	pcie = &rc->pcie;
+	pcie->is_rc = true;
+
+	of_id = of_match_node(cdns_pcie_host_of_match, np);
+	rc->data = (const struct cdns_pcie_rc_data *)of_id->data;
+
+	type = of_get_property(np, "device_type", NULL);
+	if (!type || strcmp(type, "pci")) {
+		dev_err(dev, "invalid \"device_type\" %s\n", type);
+		return -EINVAL;
+	}

drivers/pci/cadence/pcie-cadence-host.c line 359

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+	pcie->reg_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(pcie->reg_base)) {
+		dev_err(dev, "missing \"reg\"\n");
+		return PTR_ERR(pcie->reg_base);
+	}
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
+	rc->cfg_base = devm_ioremap_resource(dev, res);

devm_pci_remap_cfg_resource() please.

Quoted 136 lines
drivers/pci/cadence/pcie-cadence-host.c new file +66 -0
+	if (IS_ERR(rc->cfg_base)) {
+		dev_err(dev, "missing \"cfg\"\n");
+		return PTR_ERR(rc->cfg_base);
+	}
+	rc->cfg_res = res;
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+	if (!res) {
+		dev_err(dev, "missing \"mem\"\n");
+		return -EINVAL;
+	}
+	pcie->mem_res = res;
+
+	pm_runtime_enable(dev);
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		dev_err(dev, "pm_runtime_get_sync() failed\n");
+		goto err_get_sync;
+	}
+
+	INIT_LIST_HEAD(&resources);
+	ret = cdns_pcie_host_init(dev, &resources, rc);
+	if (ret)
+		goto err_init;
+
+	list_splice_init(&resources, &bridge->windows);
+	bridge->dev.parent = dev;
+	bridge->busnr = pcie->bus;
+	bridge->ops = &cdns_pcie_host_ops;
+	bridge->map_irq = of_irq_parse_and_map_pci;
+	bridge->swizzle_irq = pci_common_swizzle;
+
+	ret = pci_scan_root_bus_bridge(bridge);
+	if (ret < 0) {
+		dev_err(dev, "Scanning root bridge failed");
+		goto err_init;
+	}
+
+	bus = bridge->bus;
+	pci_bus_size_bridges(bus);
+	pci_bus_assign_resources(bus);
+
+	list_for_each_entry(child, &bus->children, node)
+		pcie_bus_configure_settings(child);
+
+	pci_bus_add_devices(bus);
+
+	return 0;
+
+ err_init:
+	pm_runtime_put_sync(dev);
+
+ err_get_sync:
+	pm_runtime_disable(dev);
+
+	return ret;
+}
+
+static struct platform_driver cdns_pcie_host_driver = {
+	.driver = {
+		.name = "cdns-pcie-host",
+		.of_match_table = cdns_pcie_host_of_match,
+	},
+	.probe = cdns_pcie_host_probe,
+};
+builtin_platform_driver(cdns_pcie_host_driver);
drivers/pci/cadence/pcie-cadence.c new file +64 -0
new file mode 100644
index 000000000000..5c10879d5e96
@@ -0,0 +1,110 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/kernel.h>
+
+#include "pcie-cadence.h"
+
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool is_io,
+				   u64 cpu_addr, u64 pci_addr, size_t size)
+{
+	/*
+	 * roundup_pow_of_two() returns an unsigned long, which is not suited
+	 * for 64bit values.
+	 */
+	u64 sz = 1ULL << fls64(size - 1);
+	int nbits = ilog2(sz);
+	u32 addr0, addr1, desc0, desc1;
+
+	if (nbits < 8)
+		nbits = 8;
+
+	/* Set the PCI address */
+	addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
+		(lower_32_bits(pci_addr) & GENMASK(31, 8));
+	addr1 = upper_32_bits(pci_addr);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
+
+	/* Set the PCIe header descriptor */
+	if (is_io)
+		desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
+	else
+		desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
+	desc1 = 0;
+
+	if (pcie->is_rc) {
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+			 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+		desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+	}
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+
+	/* Set the CPU address */
+	cpu_addr -= pcie->mem_res->start;

drivers/pci/cadence/pcie-cadence.c line 74

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.c new file +10 -0
+	addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
+		(lower_32_bits(cpu_addr) & GENMASK(31, 8));
+	addr1 = upper_32_bits(cpu_addr);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u32 r,
+						  u64 cpu_addr)

Not used in this patch, you should split it out.

Quoted 104 lines
drivers/pci/cadence/pcie-cadence.c new file +36 -0
+{
+	u32 addr0, addr1, desc0, desc1;
+
+	desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
+	desc1 = 0;
+	if (pcie->is_rc) {
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+			 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+		desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+	}
+
+	/* Set the CPU address */
+	cpu_addr -= pcie->mem_res->start;
+	addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
+		(lower_32_bits(cpu_addr) & GENMASK(31, 8));
+	addr1 = upper_32_bits(cpu_addr);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
+{
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
+}
drivers/pci/cadence/pcie-cadence.h new file +62 -0
new file mode 100644
index 000000000000..195e23b7d4fe
@@ -0,0 +1,325 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PCIE_CADENCE_H
+#define _PCIE_CADENCE_H
+
+#include <linux/kernel.h>
+#include <linux/pci.h>
+
+/*
+ * Local Management Registers
+ */
+#define CDNS_PCIE_LM_BASE	0x00100000
+
+/* Vendor ID Register */
+#define CDNS_PCIE_LM_ID		(CDNS_PCIE_LM_BASE + 0x0044)
+#define  CDNS_PCIE_LM_ID_VENDOR_MASK	GENMASK(15, 0)
+#define  CDNS_PCIE_LM_ID_VENDOR_SHIFT	0
+#define  CDNS_PCIE_LM_ID_VENDOR(vid) \
+	(((vid) << CDNS_PCIE_LM_ID_VENDOR_SHIFT) & CDNS_PCIE_LM_ID_VENDOR_MASK)
+#define  CDNS_PCIE_LM_ID_SUBSYS_MASK	GENMASK(31, 16)
+#define  CDNS_PCIE_LM_ID_SUBSYS_SHIFT	16
+#define  CDNS_PCIE_LM_ID_SUBSYS(sub) \
+	(((sub) << CDNS_PCIE_LM_ID_SUBSYS_SHIFT) & CDNS_PCIE_LM_ID_SUBSYS_MASK)
+
+/* Root Port Requestor ID Register */
+#define CDNS_PCIE_LM_RP_RID	(CDNS_PCIE_LM_BASE + 0x0228)
+#define  CDNS_PCIE_LM_RP_RID_MASK	GENMASK(15, 0)
+#define  CDNS_PCIE_LM_RP_RID_SHIFT	0
+#define  CDNS_PCIE_LM_RP_RID_(rid) \
+	(((rid) << CDNS_PCIE_LM_RP_RID_SHIFT) & CDNS_PCIE_LM_RP_RID_MASK)
+
+/* Endpoint Bus and Device Number Register */
+#define CDNS_PCIE_LM_EP_ID	(CDNS_PCIE_LM_BASE + 0x022c)
+#define  CDNS_PCIE_LM_EP_ID_DEV_MASK	GENMASK(4, 0)
+#define  CDNS_PCIE_LM_EP_ID_DEV_SHIFT	0
+#define  CDNS_PCIE_LM_EP_ID_BUS_MASK	GENMASK(15, 8)
+#define  CDNS_PCIE_LM_EP_ID_BUS_SHIFT	8
+
+/* Endpoint Function f BAR b Configuration Registers */
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn) \
+	(CDNS_PCIE_LM_BASE + 0x0240 + (fn) * 0x0008)
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn) \
+	(CDNS_PCIE_LM_BASE + 0x0244 + (fn) * 0x0008)
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) \

drivers/pci/cadence/pcie-cadence.h line 72

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+	(GENMASK(4, 0) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, a) \
+	(((a) << ((b) * 8)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b) \
+	(GENMASK(7, 5) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, c) \
+	(((c) << ((b) * 8 + 5)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b))
+
+/* Endpoint Function Configuration Register */
+#define CDNS_PCIE_LM_EP_FUNC_CFG	(CDNS_PCIE_LM_BASE + 0x02c0)

All endpoint defines should be moved to the patch that needs them.

Quoted 233 lines
drivers/pci/cadence/pcie-cadence.h new file +233 -0
+
+/* Root Complex BAR Configuration Register */
+#define CDNS_PCIE_LM_RC_BAR_CFG	(CDNS_PCIE_LM_BASE + 0x0300)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK	GENMASK(5, 0)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE(a) \
+	(((a) << 0) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK		GENMASK(8, 6)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(c) \
+	(((c) << 6) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK	GENMASK(13, 9)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE(a) \
+	(((a) << 9) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK		GENMASK(16, 14)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(c) \
+	(((c) << 14) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE	BIT(17)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_32BITS	0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS	BIT(18)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE		BIT(19)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_16BITS		0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS		BIT(20)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_CHECK_ENABLE		BIT(31)
+
+/* BAR control values applicable to both Endpoint Function and Root Complex */
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED		0x0
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS		0x1
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS		0x4
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS	0x5
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS		0x6
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS	0x7
+
+
+/*
+ * Endpoint Function Registers (PCI configuration space for endpoint functions)
+ */
+#define CDNS_PCIE_EP_FUNC_BASE(fn)	(((fn) << 12) & GENMASK(19, 12))
+
+#define CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET	0x90
+
+/*
+ * Root Port Registers (PCI configuration space for the root port function)
+ */
+#define CDNS_PCIE_RP_BASE	0x00200000
+
+
+/*
+ * Address Translation Registers
+ */
+#define CDNS_PCIE_AT_BASE	0x00400000
+
+/* Region r Outbound AXI to PCIe Address Translation Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r) \
+	(CDNS_PCIE_AT_BASE + 0x0000 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK	GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) \
+	(((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK	GENMASK(19, 12)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) \
+	(((devfn) << 12) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK	GENMASK(27, 20)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(bus) \
+	(((bus) << 20) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK)
+
+/* Region r Outbound AXI to PCIe Address Translation Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r) \
+	(CDNS_PCIE_AT_BASE + 0x0004 + ((r) & 0x1f) * 0x0020)
+
+/* Region r Outbound PCIe Descriptor Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_DESC0(r) \
+	(CDNS_PCIE_AT_BASE + 0x0008 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MASK		GENMASK(3, 0)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM		0x2
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO		0x6
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0	0xa
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1	0xb
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG	0xc
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_VENDOR_MSG	0xd
+/* Bit 23 MUST be set in RC mode. */
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID	BIT(23)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK	GENMASK(31, 24)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(devfn) \
+	(((devfn) << 24) & CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK)
+
+/* Region r Outbound PCIe Descriptor Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_DESC1(r)	\
+	(CDNS_PCIE_AT_BASE + 0x000c + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK	GENMASK(7, 0)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus) \
+	((bus) & CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK)
+
+/* Region r AXI Region Base Address Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r) \
+	(CDNS_PCIE_AT_BASE + 0x0018 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK	GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) \
+	(((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK)
+
+/* Region r AXI Region Base Address Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r) \
+	(CDNS_PCIE_AT_BASE + 0x001c + ((r) & 0x1f) * 0x0020)
+
+/* Root Port BAR Inbound PCIe to AXI Address Translation Register */
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR0(bar) \
+	(CDNS_PCIE_AT_BASE + 0x0800 + (bar) * 0x0008)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK	GENMASK(5, 0)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(nbits) \
+	(((nbits) - 1) & CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK)
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR1(bar) \
+	(CDNS_PCIE_AT_BASE + 0x0804 + (bar) * 0x0008)
+
+enum cdns_pcie_rp_bar {
+	RP_BAR0,
+	RP_BAR1,
+	RP_NO_BAR
+};
+
+/* Endpoint Function BAR Inbound PCIe to AXI Address Translation Register */
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar) \
+	(CDNS_PCIE_AT_BASE + 0x0840 + (fn) * 0x0040 + (bar) * 0x0008)
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar) \
+	(CDNS_PCIE_AT_BASE + 0x0844 + (fn) * 0x0040 + (bar) * 0x0008)
+
+/* Normal/Vendor specific message access: offset inside some outbound region */
+#define CDNS_PCIE_NORMAL_MSG_ROUTING_MASK	GENMASK(7, 5)
+#define CDNS_PCIE_NORMAL_MSG_ROUTING(route) \
+	(((route) << 5) & CDNS_PCIE_NORMAL_MSG_ROUTING_MASK)
+#define CDNS_PCIE_NORMAL_MSG_CODE_MASK		GENMASK(15, 8)
+#define CDNS_PCIE_NORMAL_MSG_CODE(code) \
+	(((code) << 8) & CDNS_PCIE_NORMAL_MSG_CODE_MASK)
+#define CDNS_PCIE_MSG_NO_DATA			BIT(16)
+
+enum cdns_pcie_msg_code {
+	MSG_CODE_ASSERT_INTA	= 0x20,
+	MSG_CODE_ASSERT_INTB	= 0x21,
+	MSG_CODE_ASSERT_INTC	= 0x22,
+	MSG_CODE_ASSERT_INTD	= 0x23,
+	MSG_CODE_DEASSERT_INTA	= 0x24,
+	MSG_CODE_DEASSERT_INTB	= 0x25,
+	MSG_CODE_DEASSERT_INTC	= 0x26,
+	MSG_CODE_DEASSERT_INTD	= 0x27,
+};
+
+enum cdns_pcie_msg_routing {
+	/* Route to Root Complex */
+	MSG_ROUTING_TO_RC,
+
+	/* Use Address Routing */
+	MSG_ROUTING_BY_ADDR,
+
+	/* Use ID Routing */
+	MSG_ROUTING_BY_ID,
+
+	/* Route as Broadcast Message from Root Complex */
+	MSG_ROUTING_BCAST,
+
+	/* Local message; terminate at receiver (INTx messages) */
+	MSG_ROUTING_LOCAL,
+
+	/* Gather & route to Root Complex (PME_TO_Ack message) */
+	MSG_ROUTING_GATHER,
+};
+
+/**
+ * struct cdns_pcie - private data for Cadence PCIe controller drivers
+ * @reg_base: IO mapped register base
+ * @mem_res: start/end offsets in the physical system memory to map PCI accesses
+ * @is_rc: tell whether the PCIe controller mode is Root Complex or Endpoint.
+ * @bus: In Root Complex mode, the bus number
+ */
+struct cdns_pcie {
+	void __iomem		*reg_base;
+	struct resource		*mem_res;
+	bool			is_rc;
+	u8			bus;
+};
+
+/* Register access */
+static inline void cdns_pcie_writeb(struct cdns_pcie *pcie, u32 reg, u8 value)
+{
+	writeb(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writew(struct cdns_pcie *pcie, u32 reg, u16 value)
+{
+	writew(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writel(struct cdns_pcie *pcie, u32 reg, u32 value)
+{
+	writel(value, pcie->reg_base + reg);
+}
+
+static inline u32 cdns_pcie_readl(struct cdns_pcie *pcie, u32 reg)
+{
+	return readl(pcie->reg_base + reg);
+}
+
+/* Root Port register access */
+static inline void cdns_pcie_rp_writeb(struct cdns_pcie *pcie,
+				       u32 reg, u8 value)
+{
+	writeb(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+static inline void cdns_pcie_rp_writew(struct cdns_pcie *pcie,
+				       u32 reg, u16 value)
+{
+	writew(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+/* Endpoint Function register access */
+static inline void cdns_pcie_ep_fn_writeb(struct cdns_pcie *pcie, u8 fn,
+					  u32 reg, u8 value)
+{
+	writeb(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writew(struct cdns_pcie *pcie, u8 fn,
+					  u32 reg, u16 value)
+{
+	writew(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writel(struct cdns_pcie *pcie, u8 fn,
+					  u32 reg, u16 value)
+{
+	writel(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u8 cdns_pcie_ep_fn_readb(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+	return readb(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}

drivers/pci/cadence/pcie-cadence.h line 315

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+
+static inline u16 cdns_pcie_ep_fn_readw(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+	return readw(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u32 cdns_pcie_ep_fn_readl(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+	return readl(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}

Same comments for all endpoint related functions and defines above.

Thanks,
Lorenzo

Quoted 12 lines
drivers/pci/cadence/pcie-cadence.h new file +9 -0
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool is_io,
+				   u64 cpu_addr, u64 pci_addr, size_t size);
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u32 r,
+						  u64 cpu_addr);
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r);
+
+#endif /* _PCIE_CADENCE_H */
Signature · 1 line
2.11.0
#34 lore raw
In reply to Lorenzo Pieralisi

Hi,

Quoted 17 lines · Lorenzo Pieralisi
Quoted 16 lines · Cyrille Pitchen

Hi all,

this series of patches adds support to the Cadence PCIe controller. It was tested on a ARM64 platform emulated by a Palladium running both linux-next (next-20171123) and pci-next kernels.

The host mode was tested with some PCIe devices connected to the Palladium through a speed-bridge. Some of those devices were a USB host controller and a SATA controller. The PCIe host controller was also tested with a second controller configured in endpoint mode and connected back to back to the first controller.

The EndPoint Controller (EPC) driver of this series was tested with the pci-epf-test.c EndPoint Function (EPF) driver and the pcitest userspace program.

Quoted 6 lines
Quoted 6 lines

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after

The reason to patch drivers/Makefile should be because pcie-cadence-ep has to be compiled even when CONFIG_PCI is not enabled. CONFIG_PCI enables host specific features and ENDPOINT shouldn't depend on CONFIG_PCI.

Quoted 7 lines
Quoted 3 lines

drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.

Nice :( - isn't there a way to improve this (ie probe deferral or registering the EPF bus earlier) ?

Quoted 6 lines
Quoted 2 lines

I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Or probably the other way around - see commit 5e8cb4033807

@Kishon, thoughts ?

Lorenzo, ordering Makefile is one way to initialize EP core before other drivers. the other way is to have PCI EP core have a different initcall level.. subsys_initcall??

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Lorenzo Pieralisi

On Wed, Nov 29, 2017 at 06:25:15PM +0000, Lorenzo Pieralisi wrote:

[...]

Quoted 22 lines

static void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
int where)

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
+	struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge);
+	struct cdns_pcie *pcie = &rc->pcie;
+	unsigned int busn = bus->number;
+	u32 addr0, desc0;
+
+	if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+		return NULL;

It does not hurt but I wonder whether you really need this check.

drivers/pci/cadence/pcie-cadence-host.c line 76

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+	if (busn == rc->bus_range->start) {
+		if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

Quoted 11 lines

drivers/pci/cadence/pcie-cadence-host.c line 83

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+			return NULL;
+
+		return pcie->reg_base + (where & 0xfff);
+	}
+
+	/* Update Output registers for AXI region 0. */
+	addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

By re-reading it, it looks like this mechanism is there to just associate a different RID TLP on the PCI bus to a fixed window in CPU virtual address space.

Quoted 24 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+		CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+		CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+	/* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c line 98

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+	desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+		CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+	/*
+	 * The bus number was already set once for all in desc1 by
+	 * cdns_pcie_host_init_address_translation().
+	 */
+	if (busn == rc->bus_range->start + 1)
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+	else
+		desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+	return rc->cfg_base + (where & 0xfff);
+}

[...]

Quoted 14 lines

drivers/pci/cadence/pcie-cadence-host.c line 293

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+static int cdns_pcie_host_init(struct device *dev,
+			       struct list_head *resources,
+			       struct cdns_pcie_rc *rc)
+{
+	struct resource *bus_range = NULL;
+	int err;
+
+	/* Parse our PCI ranges and request their resources */
+	err = cdns_pcie_parse_request_of_pci_ranges(dev, resources, &bus_range);
+	if (err)
+		goto err_out;

I think that the err_out path should be part of:

Quoted 3 lines

cdns_pcie_parse_request_of_pci_ranges()

implementation and here you would just return.

I take it back, what you are doing is cleaner and allows you to have code freeing the resource list in one single place so you can leave this as-is.

Thanks,
Lorenzo

#35 lore raw
In reply to Kishon Vijay Abraham I

On Thu, Nov 30, 2017 at 12:43:20PM +0530, Kishon Vijay Abraham I wrote:

[...]

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after

The reason to patch drivers/Makefile should be because pcie-cadence-ep has to be compiled even when CONFIG_PCI is not enabled. CONFIG_PCI enables host specific features and ENDPOINT shouldn't depend on CONFIG_PCI.

Quoted 7 lines
Quoted 3 lines

drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.

Nice :( - isn't there a way to improve this (ie probe deferral or registering the EPF bus earlier) ?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Or probably the other way around - see commit 5e8cb4033807

@Kishon, thoughts ?

Lorenzo, ordering Makefile is one way to initialize EP core before

Makefile ordering is fragile, I do not like relying on it.

Quoted 2 lines

other drivers. the other way is to have PCI EP core have a different initcall level.. subsys_initcall??

Yes, registering the bus at eg postcore_initcall() as PCI does should do (if that's the problem this is solving) but still, the code must not crash if the ordering is not correct, we have to fix this regardless.

I would appreciate if Cyrille can debug the cause of the kernel crash so that we can fix it in the longer term.

Thanks,
Lorenzo

#36 lore raw

Hi all,

Quoted 35 lines · Lorenzo Pieralisi

On Thu, Nov 30, 2017 at 12:43:20PM +0530, Kishon Vijay Abraham I wrote:

[...]

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after

The reason to patch drivers/Makefile should be because pcie-cadence-ep has to be compiled even when CONFIG_PCI is not enabled. CONFIG_PCI enables host specific features and ENDPOINT shouldn't depend on CONFIG_PCI.

Quoted 7 lines
Quoted 3 lines

drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.

Nice :( - isn't there a way to improve this (ie probe deferral or registering the EPF bus earlier) ?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Or probably the other way around - see commit 5e8cb4033807

@Kishon, thoughts ?

Lorenzo, ordering Makefile is one way to initialize EP core before

Makefile ordering is fragile, I do not like relying on it.

Quoted 2 lines

other drivers. the other way is to have PCI EP core have a different initcall level.. subsys_initcall??

Quoted 7 lines

Yes, registering the bus at eg postcore_initcall() as PCI does should do (if that's the problem this is solving) but still, the code must not crash if the ordering is not correct, we have to fix this regardless.

I would appreciate if Cyrille can debug the cause of the kernel crash so that we can fix it in the longer term.

I had the crash kernel few weeks ago hence I have to test again to confirm but if I remember correctly it was a NULL pointer dereferencing crash:

cdns_pcie_ep_probe()
|

+--> devm_pci_epc_create()
     |
     +--> __devm_pci_ep_create()
          |
          +--> __pci_epc_create()
	       |
	       +--> pci_ep_cfs_add_epc_group()
	            |
		    | /*
		    |  * Here controllers_group is still NULL because
		    |  * pci_ep_cfs_init() has not been called yet.
		    |  */
		    +--> configfs_register_group(controllers_group, ...)
		         |
			 +--> link_group()
			      /* Dereferencing NULL pointer */

Best regards,

Cyrille

Quoted 3 lines

Thanks,
Lorenzo

Signature · 3 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#37 lore raw
Quoted 68 lines · Cyrille Pitchen

Hi all,

Quoted 35 lines · Lorenzo Pieralisi

On Thu, Nov 30, 2017 at 12:43:20PM +0530, Kishon Vijay Abraham I wrote:

[...]

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines

For linux-next, I applied this series on top of Kishon's patch ("PCI: endpoint: Use EPC's device in dma_alloc_coherent/dma_free_coherent") otherwise dma_alloc_coherent() fails when called by pci_epf_alloc_space().

Also, I patched drivers/Makefile rather than drivers/pci/Makefile to make the drivers/pci/cadence/pcie-cadence-ep.o linked after

The reason to patch drivers/Makefile should be because pcie-cadence-ep has to be compiled even when CONFIG_PCI is not enabled. CONFIG_PCI enables host specific features and ENDPOINT shouldn't depend on CONFIG_PCI.

Quoted 7 lines
Quoted 3 lines

drivers/pci/endpoint/*.o objects, otherwise the built-in pci-cadence-ep driver would be probed before the PCI endpoint framework would have been initialized, which results in a kernel crash.

Nice :( - isn't there a way to improve this (ie probe deferral or registering the EPF bus earlier) ?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I guess this is the reason why the "pci/dwc" line was also put in drivers/Makefile, right after the "pci/endpoint" line.

Or probably the other way around - see commit 5e8cb4033807

@Kishon, thoughts ?

Lorenzo, ordering Makefile is one way to initialize EP core before

Makefile ordering is fragile, I do not like relying on it.

Quoted 2 lines

other drivers. the other way is to have PCI EP core have a different initcall level.. subsys_initcall??

Quoted 7 lines

Yes, registering the bus at eg postcore_initcall() as PCI does should do (if that's the problem this is solving) but still, the code must not crash if the ordering is not correct, we have to fix this regardless.

I would appreciate if Cyrille can debug the cause of the kernel crash so that we can fix it in the longer term.

I had the crash kernel few weeks ago hence I have to test again to confirm but if I remember correctly it was a NULL pointer dereferencing crash:

cdns_pcie_ep_probe()
|

+--> devm_pci_epc_create()
     |
     +--> __devm_pci_ep_create()
          |
          +--> __pci_epc_create()
	       |
	       +--> pci_ep_cfs_add_epc_group()
	            |
		    | /*
		    |  * Here controllers_group is still NULL because
		    |  * pci_ep_cfs_init() has not been called yet.
		    |  */
		    +--> configfs_register_group(controllers_group, ...)
		         |
			 +--> link_group()
			      /* Dereferencing NULL pointer */
Quoted 4 lines

Best regards,

Cyrille

I know it's far from perfect but what do you think or something like that:

---8<---------------------------------------------------------------------------

drivers/Makefile +1 -5
index 27bdd98784d9..9757199b9a65 100644
@@ -16,11 +16,7 @@ obj-$(CONFIG_PINCTRL)		+= pinctrl/
 obj-$(CONFIG_GPIOLIB)		+= gpio/
 obj-y				+= pwm/
 
-obj-$(CONFIG_PCI)		+= pci/
-obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
-obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/
-# PCI dwc controller drivers
-obj-y				+= pci/dwc/
+obj-y				+= pci/
 
 obj-$(CONFIG_PARISC)		+= parisc/
 obj-$(CONFIG_RAPIDIO)		+= rapidio/
drivers/pci/Makefile +7 -0
index 3d5e047f0a32..4e57fe4499ce 100644
@@ -3,6 +3,7 @@
 # Makefile for the PCI bus specific drivers.
 #
 
+ifdef CONFIG_PCI
 obj-y		+= access.o bus.o probe.o host-bridge.o remove.o pci.o \
 			pci-driver.o search.o pci-sysfs.o rom.o setup-res.o \
 			irq.o vpd.o setup-bus.o vc.o mmap.o setup-irq.o
@@ -54,3 +55,9 @@ ccflags-$(CONFIG_PCI_DEBUG) := -DDEBUG
 # PCI host controller drivers
 obj-y += host/
 obj-y += switch/
+endif
+
+obj-$(CONFIG_PCI_ENDPOINT)	+= endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= cadence/
+# PCI dwc controller drivers
+obj-y				+= dwc/

---8<---------------------------------------------------------------------------

I admit it doesn't solve the ordering issue but at least it cleans some mess in drivers/Makefile.

I agree with Lorenzo, we should avoid as much as we could to rely on the link order.

However I faced another issue: code from pcie-cadence.c is used by both pcie-cadence-host.c and pcie-cadence-ep.c so I had to find a way to compile this shared file if at least one of the host and EPC driver was selected.

That's why I put source files in the cadence sub-directory and used the single cadence/Makefile to regroup all the compilation rules, instead of splitting them between pci/Makefile and pci/endpoint/Makefile and having to add pcie-cadence.o in both Makefiles.

Quoted 5 lines
Quoted 3 lines

Thanks,
Lorenzo

Signature · 7 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#38 lore raw
Quoted 53 lines · Cyrille Pitchen
Quoted 7 lines · Cyrille Pitchen
Quoted 6 lines · Lorenzo Pieralisi
Quoted 4 lines · Kishon Vijay Abraham I

The reason to patch drivers/Makefile should be because pcie-cadence-ep has to be compiled even when CONFIG_PCI is not enabled. CONFIG_PCI enables host specific features and ENDPOINT shouldn't depend on CONFIG_PCI.

I know it's far from perfect but what do you think or something like that:

---8<---------------------------------------------------------------------------

drivers/Makefile +1 -5
index 27bdd98784d9..9757199b9a65 100644
@@ -16,11 +16,7 @@ obj-$(CONFIG_PINCTRL)		+= pinctrl/
 obj-$(CONFIG_GPIOLIB)		+= gpio/
 obj-y				+= pwm/
 
-obj-$(CONFIG_PCI)		+= pci/
-obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
-obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/
-# PCI dwc controller drivers
-obj-y				+= pci/dwc/
+obj-y				+= pci/
 
 obj-$(CONFIG_PARISC)		+= parisc/
 obj-$(CONFIG_RAPIDIO)		+= rapidio/
drivers/pci/Makefile +7 -1
index 3d5e047f0a32..4e57fe4499ce 100644
@@ -3,6 +3,7 @@
 # Makefile for the PCI bus specific drivers.
 #
 
+ifdef CONFIG_PCI
 obj-y		+= access.o bus.o probe.o host-bridge.o remove.o pci.o \
 			pci-driver.o search.o pci-sysfs.o rom.o setup-res.o \
 			irq.o vpd.o setup-bus.o vc.o mmap.o setup-irq.o
@@ -54,3 +55,9 @@ ccflags-$(CONFIG_PCI_DEBUG) := -DDEBUG
 # PCI host controller drivers
 obj-y += host/
 obj-y += switch/
+endif
+
+obj-$(CONFIG_PCI_ENDPOINT)	+= endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= cadence/
+# PCI dwc controller drivers
+obj-y				+= dwc/
---8<---------------------------------------------------------------------------
Quoted 2 lines

I admit it doesn't solve the ordering issue but at least it cleans some mess in drivers/Makefile.

I like this a lot. I think the only things that would actually require the ifdef are the generic symbol things (CONFIG_PROC_FS, CONFIG_SYSFS, CONFIG_OF), so we could do this for most of it:

  obj-$(CONFIG_PCI)		+= access.o bus.o probe.o ...
  ...
  obj-$(CONFIG_PCI_QUIRKS)	+= quirks.o
  obj-$(CONFIG_PCIEPORTBUS)	+= pcie/
  obj-$(CONFIG_PCI)		+= host/
  obj-$(CONFIG_PCI)		+= switch/

With possibly a few Kconfig dependency tweaks.

#11 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Bjorn Helgaas

Hi Bjorn,

Quoted 19 lines · Bjorn Helgaas
Quoted 17 lines · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
Quoted 6 lines

I prefer a single file per driver. I assume you're anticipating something like dwc, where the DesignWare core is incorporated into several devices in slightly different ways. But it doesn't look like that's here yet, and personally, I'd rather split out the required things when they actually become required, not ahead of time.

The source code in pcie-cadence.c is shared between pcie-cadence-host.c (Root Complex mode) and pcie-cadence-ep.c (Endpoint mode), the second driver of this series.

Taking other comments into accounts, I will move endpoint only related stuff in the pcie-cadence.{c,h} files from this patch to the endpoint patch.

Otherwise your right, I expect this Cadence PCIe core to be embedded inside several vendor SoCs but I thought I could handle this as much as possible using mainly DT properties and/or the 'struct cdns_pcie_*_data' associated to the DT 'compatible' strings. We will have to wait for those SoCs to be released to know whether these two solutions are enough or if we will need dedicated files anyway.

Quoted 6 lines
Quoted 6 lines
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
Quoted 9 lines

drivers/Makefile obj-y += pwm/ · line 21

Quoted 3 lines
drivers/Makefile +1 -0
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/

I can't remember why we added CONFIG_PCI_ENDPOINT here instead of in drivers/pci/Makefile. Is there any way to move both CONFIG_PCI_ENDPOINT and CONFIG_PCI_CADENCE into drivers/pci/Makefile so this is better encapsulated?

I will work on the solution I have proposed in another reply since it seems to be OK for you :)

Quoted 63 lines

drivers/Makefile obj-y += pwm/

Quoted 5 lines
drivers/Makefile +1 -0
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
...
 
+ * struct cdns_pcie_rc_data - hardware specific data

"cdns" is a weird abbreviation for "Cadence", since "Cadence" doesn't contain an "s".

Quoted 35 lines

...

drivers/Makefile +34 -0
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+						 struct list_head *resources,
+						 struct resource **bus_range)
+{
+	int err, res_valid = 0;
+	struct device_node *np = dev->of_node;
+	resource_size_t iobase;
+	struct resource_entry *win, *tmp;
+
+	err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+	if (err)
+		return err;
+
+	err = devm_request_pci_bus_resources(dev, resources);
+	if (err)
+		return err;
+
+	resource_list_for_each_entry_safe(win, tmp, resources) {
+		struct resource *res = win->res;
+
+		switch (resource_type(res)) {
+		case IORESOURCE_IO:
+			err = pci_remap_iospace(res, iobase);
+			if (err) {
+				dev_warn(dev, "error %d: failed to map resource %pR\n",
+					 err, res);
+				resource_list_destroy_entry(win);
+			}
+			break;
+		case IORESOURCE_MEM:
+			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+			break;
+		case IORESOURCE_BUS:
+			*bus_range = res;

drivers/Makefile

Quoted 10 lines
drivers/Makefile +10 -0
+			break;
+		}
+	}
+
+	if (res_valid)
+		return 0;
+
+	dev_err(dev, "non-prefetchable memory resource required\n");
+	return -EINVAL;
+}

The code above is starting to look awfully familiar. I wonder if it's time to think about some PCI-internal interface that can encapsulate this. In this case, there's really nothing Cadence-specific here. There are other callers where there *is* vendor-specific code, but possibly that could be handled by returning pointers to bus number, I/O port, and MMIO resources so the caller could do the vendor-specific stuff?

Quoted 2 lines

Bjorn

I am listing:
- gen_pci_parse_request_of_pci_ranges() [1]
- cdns_pcie_parse_request_of_pci_ranges() - same as [1]
- advk_pcie_parse_request_of_pci_ranges()
- altera_pcie_parse_request_of_pci_ranges()
- versatile_pci_parse_request_of_pci_ranges()
- rcar_pcie_parse_request_of_pci_ranges()

Then what about doing something like that:

---8<--------------------------------------------------------------------------

drivers/pci/host/pci-host-common.c +91 -22
index 44a47d4f0b8f..2413c5d83cbd 100644
@@ -24,10 +24,19 @@
 #include <linux/pci-ecam.h>
 #include <linux/platform_device.h>
 
-static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
-		       struct list_head *resources, struct resource **bus_range)
+struct pci_host_resource_parser {
+	int	(*get_resource)(void *userdata, struct resource_entry *win,
+				resource_size_t iobase);
+	int	(*finalize)(void *userdata);
+	void	(*abort)(void *userdata);
+};
+
+int pci_host_parse_request_of_pci_ranges(struct device *dev,
+				 struct list_head *resources,
+				 const struct pci_host_resource_parser *parser,
+				 void *userdata)
 {
-	int err, res_valid = 0;
+	int err;
 	struct device_node *np = dev->of_node;
 	resource_size_t iobase;
 	struct resource_entry *win, *tmp;
@@ -40,34 +49,94 @@ static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
 	if (err)
 		return err;
 
-	resource_list_for_each_entry_safe(win, tmp, resources) {
-		struct resource *res = win->res;
-
-		switch (resource_type(res)) {
-		case IORESOURCE_IO:
-			err = pci_remap_iospace(res, iobase);
-			if (err) {
-				dev_warn(dev, "error %d: failed to map resource %pR\n",
-					 err, res);
-				resource_list_destroy_entry(win);
-			}
-			break;
-		case IORESOURCE_MEM:
-			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
-			break;
-		case IORESOURCE_BUS:
-			*bus_range = res;
-			break;
+	if (parser && parser->get_resource) {
+		resource_list_for_each_entry_safe(win, tmp, resources) {
+			err = parser->get_resource(userdata, win, iobase);
+			if (err)
+				goto do_abort;
+		}
+	}
+
+	if (parser && parser->finalize)
+		return parser->finalize(userdata);
+
+	return 0;
+
+ do_abort:
+	if (parser && parser->abort)
+		parser->abort(userdata);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(pci_host_parse_request_of_pci_ranges);
+
+
+struct gen_pci_parser_context {
+	struct device	*dev;
+	struct resource	**bus_range;
+	int		res_valid;
+};
+
+static int gen_pci_resource_parser_get_resource(void *userdata,
+						struct resource_entry *win,
+						resource_size_t iobase)
+{
+	struct gen_pci_parser_context *ctx = userdata;
+	struct device *dev = ctx->dev;
+	struct resource *res = win->res;
+	int err;
+
+	switch (resource_type(res)) {
+	case IORESOURCE_IO:
+		err = pci_remap_iospace(res, iobase);
+		if (err) {
+			dev_warn(dev, "error %d: failed to map resource %pR\n",
+				 err, res);
+			resource_list_destroy_entry(win);
 		}
+		break;
+	case IORESOURCE_MEM:
+		ctx->res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+		break;
+	case IORESOURCE_BUS:
+		*ctx->bus_range = res;
+		break;
 	}
 
-	if (res_valid)
+	return 0;
+}
+
+static int gen_pci_resource_parser_finalize(void *userdata)
+{
+	struct gen_pci_parser_context *ctx = userdata;
+	struct device *dev = ctx->dev;
+
+	if (ctx->res_valid)
 		return 0;
 
 	dev_err(dev, "non-prefetchable memory resource required\n");
 	return -EINVAL;
 }
 
+static const struct pci_host_resource_parser gen_pci_resource_parser = {
+	.get_resource	= gen_pci_resource_parser_get_resource,
+	.finalize	= gen_pci_resource_parser_finalize,
+};
+
+static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
+		       struct list_head *resources, struct resource **bus_range)
+{
+	struct gen_pci_parser_context ctx;
+
+	ctx.dev = dev;
+	ctx.bus_range = bus_range;
+	ctx.res_valid = 0;
+
+	return pci_host_parse_request_of_pci_ranges(dev, resources,
+						    &gen_pci_resource_parser,
+						    &ctx);
+}
+
 static void gen_pci_unmap_cfg(void *ptr)
 {
 	pci_ecam_free((struct pci_config_window *)ptr);

---8<--------------------------------------------------------------------------

Best regards,

Cyrille

Signature · 7 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#24 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Cyrille Pitchen
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
 	  mode. This PCIe controller may be embedded into many different vendors
 	  SoCs.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on PCI_ENDPOINT
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *

drivers/pci/cadence/pcie-cadence-ep.c line 26

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/pci-epc.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/sizes.h>
+#include <linux/delay.h>

Nit: alphabetical order.

drivers/pci/cadence/pcie-cadence-ep.c line 36

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +9 -0
+#include "pcie-cadence.h"
+
+#define CDNS_PCIE_EP_MIN_APERTURE		128	/* 128 bytes */
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE		0x1
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY	0x3
+
+/**
+ * struct cdns_pcie_ep_data - hardware specific data
+ * @max_regions: maximum nmuber of regions supported by hardware

s/nmuber/number

Quoted 20 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +20 -0
+ */
+struct cdns_pcie_ep_data {
+	size_t				max_regions;
+};
+
+/**
+ * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
+ * @pcie: Cadence PCIe controller
+ * @data: pointer to a 'struct cdns_pcie_data'
+ */
+struct cdns_pcie_ep {
+	struct cdns_pcie		pcie;
+	const struct cdns_pcie_ep_data	*data;
+	struct pci_epc			*epc;
+	unsigned long			ob_region_map;
+	phys_addr_t			*ob_addr;
+	phys_addr_t			irq_phys_addr;
+	void __iomem			*irq_cpu_addr;
+	u64				irq_pci_addr;
+	u8				irq_pending;

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+};
+
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

Quoted 31 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +31 -0
+		u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
+			 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
+		cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
+	}
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
+			       hdr->subclass_code | hdr->baseclass_code << 8);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
+			       hdr->cache_line_size);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_set_bar(struct pci_epc *epc, enum pci_barno bar,
+				dma_addr_t bar_phys, size_t size, int flags)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
+	u8 fn = 0;
+	u64 sz;
+
+	/* BAR size is 2^(aperture + 7) */
+	sz = max_t(size_t, size, CDNS_PCIE_EP_MIN_APERTURE);
+	sz = 1ULL << fls64(sz - 1);
+	aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
+

drivers/pci/cadence/pcie-cadence-ep.c line 107

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
+		ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
+	} else {
+		bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
+		bool is_64bits = sz > SZ_2G;
+
+		if (is_64bits && (bar & 1))
+			return -EINVAL;
+
+		switch (is_64bits << 1 | is_prefetch) {

I would not mind implementing this as a nested if-else, I am not a big fan of using bool this way.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+		case 0:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
+			break;
+
+		case 1:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
+			break;
+
+		case 2:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
+			break;
+
+		case 3:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;

drivers/pci/cadence/pcie-cadence-ep.c line 131

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+			break;
+		}
+	}
+
+	addr0 = lower_32_bits(bar_phys);
+	addr1 = upper_32_bits(bar_phys);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
+			 addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
+			 addr1);

Is fn always 0 ?

Quoted 42 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +42 -0
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
+		CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	return 0;
+}
+
+static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, enum pci_barno bar)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 reg, cfg, b, ctrl;
+	u8 fn = 0;
+
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(ctrl);
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);

drivers/pci/cadence/pcie-cadence-ep.c line 184

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static int cdns_pcie_ep_map_addr(struct pci_epc *epc, phys_addr_t addr,
+				 u64 pci_addr, size_t size)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	r = find_first_zero_bit(&ep->ob_region_map, sizeof(ep->ob_region_map));

Second argument must be in bits not bytes.

https://marc.info/?l=linux-pci&m=151179781225513&w=2

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+	if (r >= ep->data->max_regions - 1) {
+		dev_err(&epc->dev, "no free outbound region\n");
+		return -EINVAL;
+	}
+
+	cdns_pcie_set_outbound_region(pcie, r, false, addr, pci_addr, size);
+
+	set_bit(r, &ep->ob_region_map);
+	ep->ob_addr[r] = addr;
+
+	return 0;
+}
+
+static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, phys_addr_t addr)

drivers/pci/cadence/pcie-cadence-ep.c line 208

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	for (r = 0; r < ep->data->max_regions - 1; r++)
+		if (ep->ob_addr[r] == addr)
+			break;
+
+	if (r >= ep->data->max_regions - 1)

== ?

Quoted 50 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +50 -0
+		return;
+
+	cdns_pcie_reset_outbound_region(pcie, r);
+
+	ep->ob_addr[r] = 0;
+	clear_bit(r, &ep->ob_region_map);
+}
+
+static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 mmc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/*
+	 * Set the Multiple Message Capable bitfield into the Message Control
+	 * register.
+	 */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
+	flags |= PCI_MSI_FLAGS_64BIT;
+	flags &= ~PCI_MSI_FLAGS_MASKBIT;
+	cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_get_msi(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/* Validate that the MSI feature is actually enabled. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+

drivers/pci/cadence/pcie-cadence-ep.c line 268

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	/*
+	 * Get the Multiple Message Enable bitfield from the Message Control
+	 * register.
+	 */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

You should comment on what this 5 means and why it is fine to cap mme.

Quoted 43 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +43 -0
+
+	return mme;
+}
+
+static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
+				     u8 intx, bool is_asserted)
+{
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r = ep->data->max_regions - 1;
+	u32 offset;
+	u16 status;
+	u8 msg_code;
+
+	intx &= 3;
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region_for_normal_msg(pcie, r,
+							     ep->irq_phys_addr);
+		ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
+	}
+
+	if (is_asserted) {
+		ep->irq_pending |= BIT(intx);
+		msg_code = MSG_CODE_ASSERT_INTA + intx;
+	} else {
+		ep->irq_pending &= ~BIT(intx);
+		msg_code = MSG_CODE_DEASSERT_INTA + intx;
+	}
+
+	status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
+	if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
+		status ^= PCI_STATUS_INTERRUPT;
+		cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
+	}
+
+	offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
+		 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
+		 CDNS_PCIE_MSG_NO_DATA;
+	writel(0, ep->irq_cpu_addr + offset);
+}
+

drivers/pci/cadence/pcie-cadence-ep.c line 321

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
+{
+	u16 cmd;
+
+	cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
+	if (cmd & PCI_COMMAND_INTX_DISABLE)
+		return -EINVAL;
+
+	cdns_pcie_ep_assert_intx(ep, fn, intx, true);
+	mdelay(1);

Add a comment please to explain the mdelay value.

Quoted 25 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +25 -0
+	cdns_pcie_ep_assert_intx(ep, fn, intx, false);
+	return 0;
+}
+
+static int cdns_pcie_ep_raise_irq(struct pci_epc *epc,
+				  enum pci_epc_irq_type type, u8 interrupt_num)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme, data, data_mask;
+	u8 msi_count;
+	u64 pci_addr, pci_addr_mask = 0xff;
+	u8 fn = 0;
+
+	/* Handle legacy IRQ. */
+	if (type == PCI_EPC_IRQ_LEGACY)
+		return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
+
+	/* Otherwise MSI. */
+	if (type != PCI_EPC_IRQ_MSI)
+		return -EINVAL;
+
+	/* Check whether the MSI feature has been enabled by the PCI host. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);

drivers/pci/cadence/pcie-cadence-ep.c line 356

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/* Get the number of enabled MSIs */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

Same comment as above.

Quoted 51 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +51 -0
+	msi_count = 1 << mme;
+	if (!interrupt_num || interrupt_num > msi_count)
+		return -EINVAL;
+
+	/* Compute the data value to be written. */
+	data_mask = msi_count - 1;
+	data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
+	data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
+
+	/* Get the PCI address where to write the data into. */
+	pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
+	pci_addr <<= 32;
+	pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
+	pci_addr &= GENMASK_ULL(63, 2);
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != pci_addr)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region(pcie, ep->data->max_regions - 1,
+					      false,
+					      ep->irq_phys_addr,
+					      pci_addr & ~pci_addr_mask,
+					      pci_addr_mask + 1);
+		ep->irq_pci_addr = pci_addr;
+	}
+	writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
+
+	return 0;
+}
+
+static int cdns_pcie_ep_start(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	struct pci_epf *epf;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Enable this endpoint function. */
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg |= BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+
+	/*
+	 * Already linked-up: don't call directly pci_epc_linkup() because we've
+	 * already locked the epc->lock.
+	 */
+	list_for_each_entry(epf, &epc->pci_epf, list)
+		pci_epf_linkup(epf);
+
+	return 0;

drivers/pci/cadence/pcie-cadence-ep.c line 418

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static void cdns_pcie_ep_stop(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Disable this endpoint function (function 0 can't be disabled). */

I do not understand this comment and how it applies to the code, in other words fn is always 0 here (so it can't be disabled) I do not understand what this code is there for.

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +11 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg &= ~BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+}
+
+static const struct pci_epc_ops cdns_pcie_epc_ops = {
+	.write_header	= cdns_pcie_ep_write_header,
+	.set_bar	= cdns_pcie_ep_set_bar,
+	.clear_bar	= cdns_pcie_ep_clear_bar,
+	.map_addr	= cdns_pcie_ep_map_addr,
+	.unmap_addr	= cdns_pcie_ep_unmap_addr,

drivers/pci/cadence/pcie-cadence-ep.c line 439

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	.set_msi	= cdns_pcie_ep_set_msi,
+	.get_msi	= cdns_pcie_ep_get_msi,
+	.raise_irq	= cdns_pcie_ep_raise_irq,
+	.start		= cdns_pcie_ep_start,
+	.stop		= cdns_pcie_ep_stop,
+};
+
+static const struct cdns_pcie_ep_data cdns_pcie_ep_data = {
+	.max_regions	= 16,
+};

As I mentioned in patch 3, should this be set-up with DT ?

Thanks,
Lorenzo

Quoted 117 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +114 -0
+
+static const struct of_device_id cdns_pcie_ep_of_match[] = {
+	{ .compatible = "cdns,cdns-pcie-ep",
+	  .data = &cdns_pcie_ep_data },
+
+	{ },
+};
+
+static int cdns_pcie_ep_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	const struct of_device_id *of_id;
+	struct cdns_pcie_ep *ep;
+	struct cdns_pcie *pcie;
+	struct pci_epc *epc;
+	struct resource *res;
+	size_t max_regions;
+	int ret;
+
+	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
+	if (!ep)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, ep);
+
+	pcie = &ep->pcie;
+	pcie->is_rc = false;
+
+	of_id = of_match_node(cdns_pcie_ep_of_match, np);
+	ep->data = (const struct cdns_pcie_ep_data *)of_id->data;
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+	pcie->reg_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(pcie->reg_base)) {
+		dev_err(dev, "missing \"reg\"\n");
+		return PTR_ERR(pcie->reg_base);
+	}
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+	if (!res) {
+		dev_err(dev, "missing \"mem\"\n");
+		return -EINVAL;
+	}
+	pcie->mem_res = res;
+
+	max_regions = ep->data->max_regions;
+	ep->ob_addr = devm_kzalloc(dev, max_regions * sizeof(*ep->ob_addr),
+				   GFP_KERNEL);
+	if (!ep->ob_addr)
+		return -ENOMEM;
+
+	pm_runtime_enable(dev);
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		dev_err(dev, "pm_runtime_get_sync() failed\n");
+		goto err_get_sync;
+	}
+
+	/* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
+
+	epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
+	if (IS_ERR(epc)) {
+		dev_err(dev, "failed to create epc device\n");
+		ret = PTR_ERR(epc);
+		goto err_init;
+	}
+
+	ep->epc = epc;
+	epc_set_drvdata(epc, ep);
+
+	ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
+	if (ret < 0)
+		epc->max_functions = 1;
+
+	ret = pci_epc_mem_init(epc, pcie->mem_res->start,
+			       resource_size(pcie->mem_res));
+	if (ret < 0) {
+		dev_err(dev, "failed to initialize the memory space\n");
+		goto err_init;
+	}
+
+	ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
+						  SZ_128K);
+	if (!ep->irq_cpu_addr) {
+		dev_err(dev, "failed to reserve memory space for MSI\n");
+		ret = -ENOMEM;
+		goto free_epc_mem;
+	}
+	ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
+
+	return 0;
+
+ free_epc_mem:
+	pci_epc_mem_exit(epc);
+
+ err_init:
+	pm_runtime_put_sync(dev);
+
+ err_get_sync:
+	pm_runtime_disable(dev);
+
+	return ret;
+}
+
+static struct platform_driver cdns_pcie_ep_driver = {
+	.driver = {
+		.name = "cdns-pcie-ep",
+		.of_match_table = cdns_pcie_ep_of_match,
+	},
+	.probe = cdns_pcie_ep_probe,
+};
+builtin_platform_driver(cdns_pcie_ep_driver);
Signature · 1 line
2.11.0
#12 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen
Quoted 267 lines · Cyrille Pitchen

Hi Bjorn,

Quoted 19 lines · Bjorn Helgaas
Quoted 17 lines · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in host mode.

---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
Quoted 6 lines

I prefer a single file per driver. I assume you're anticipating something like dwc, where the DesignWare core is incorporated into several devices in slightly different ways. But it doesn't look like that's here yet, and personally, I'd rather split out the required things when they actually become required, not ahead of time.

The source code in pcie-cadence.c is shared between pcie-cadence-host.c (Root Complex mode) and pcie-cadence-ep.c (Endpoint mode), the second driver of this series.

Taking other comments into accounts, I will move endpoint only related stuff in the pcie-cadence.{c,h} files from this patch to the endpoint patch.

Otherwise your right, I expect this Cadence PCIe core to be embedded inside several vendor SoCs but I thought I could handle this as much as possible using mainly DT properties and/or the 'struct cdns_pcie_*_data' associated to the DT 'compatible' strings. We will have to wait for those SoCs to be released to know whether these two solutions are enough or if we will need dedicated files anyway.

Quoted 6 lines
Quoted 6 lines
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644
@@ -18,6 +18,7 @@ obj-y				+= pwm/
 
Quoted 9 lines

drivers/Makefile obj-y += pwm/ · line 21

Quoted 3 lines
drivers/Makefile +1 -0
 obj-$(CONFIG_PCI)		+= pci/
 obj-$(CONFIG_PCI_ENDPOINT)	+= pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)	+= pci/cadence/

I can't remember why we added CONFIG_PCI_ENDPOINT here instead of in drivers/pci/Makefile. Is there any way to move both CONFIG_PCI_ENDPOINT and CONFIG_PCI_CADENCE into drivers/pci/Makefile so this is better encapsulated?

I will work on the solution I have proposed in another reply since it seems to be OK for you :)

Quoted 63 lines

drivers/Makefile obj-y += pwm/

Quoted 5 lines
drivers/Makefile +1 -0
 # PCI dwc controller drivers
 obj-y				+= pci/dwc/
...
 
+ * struct cdns_pcie_rc_data - hardware specific data

"cdns" is a weird abbreviation for "Cadence", since "Cadence" doesn't contain an "s".

Quoted 35 lines

...

drivers/Makefile +34 -0
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+						 struct list_head *resources,
+						 struct resource **bus_range)
+{
+	int err, res_valid = 0;
+	struct device_node *np = dev->of_node;
+	resource_size_t iobase;
+	struct resource_entry *win, *tmp;
+
+	err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+	if (err)
+		return err;
+
+	err = devm_request_pci_bus_resources(dev, resources);
+	if (err)
+		return err;
+
+	resource_list_for_each_entry_safe(win, tmp, resources) {
+		struct resource *res = win->res;
+
+		switch (resource_type(res)) {
+		case IORESOURCE_IO:
+			err = pci_remap_iospace(res, iobase);
+			if (err) {
+				dev_warn(dev, "error %d: failed to map resource %pR\n",
+					 err, res);
+				resource_list_destroy_entry(win);
+			}
+			break;
+		case IORESOURCE_MEM:
+			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+			break;
+		case IORESOURCE_BUS:
+			*bus_range = res;

drivers/Makefile

Quoted 10 lines
drivers/Makefile +10 -0
+			break;
+		}
+	}
+
+	if (res_valid)
+		return 0;
+
+	dev_err(dev, "non-prefetchable memory resource required\n");
+	return -EINVAL;
+}

The code above is starting to look awfully familiar. I wonder if it's time to think about some PCI-internal interface that can encapsulate this. In this case, there's really nothing Cadence-specific here. There are other callers where there *is* vendor-specific code, but possibly that could be handled by returning pointers to bus number, I/O port, and MMIO resources so the caller could do the vendor-specific stuff?

Quoted 2 lines

Bjorn

I am listing:
- gen_pci_parse_request_of_pci_ranges() [1]
- cdns_pcie_parse_request_of_pci_ranges() - same as [1]
- advk_pcie_parse_request_of_pci_ranges()
- altera_pcie_parse_request_of_pci_ranges()
- versatile_pci_parse_request_of_pci_ranges()
- rcar_pcie_parse_request_of_pci_ranges()

Then what about doing something like that:

---8<--------------------------------------------------------------------------

drivers/pci/host/pci-host-common.c +80 -22
index 44a47d4f0b8f..2413c5d83cbd 100644
@@ -24,10 +24,19 @@
 #include <linux/pci-ecam.h>
 #include <linux/platform_device.h>
 
-static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
-		       struct list_head *resources, struct resource **bus_range)
+struct pci_host_resource_parser {
+	int	(*get_resource)(void *userdata, struct resource_entry *win,
+				resource_size_t iobase);
+	int	(*finalize)(void *userdata);
+	void	(*abort)(void *userdata);
+};
+
+int pci_host_parse_request_of_pci_ranges(struct device *dev,
+				 struct list_head *resources,
+				 const struct pci_host_resource_parser *parser,
+				 void *userdata)
 {
-	int err, res_valid = 0;
+	int err;
 	struct device_node *np = dev->of_node;
 	resource_size_t iobase;
 	struct resource_entry *win, *tmp;
@@ -40,34 +49,94 @@ static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
 	if (err)
 		return err;
 
-	resource_list_for_each_entry_safe(win, tmp, resources) {
-		struct resource *res = win->res;
-
-		switch (resource_type(res)) {
-		case IORESOURCE_IO:
-			err = pci_remap_iospace(res, iobase);
-			if (err) {
-				dev_warn(dev, "error %d: failed to map resource %pR\n",
-					 err, res);
-				resource_list_destroy_entry(win);
-			}
-			break;
-		case IORESOURCE_MEM:
-			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
-			break;
-		case IORESOURCE_BUS:
-			*bus_range = res;
-			break;
+	if (parser && parser->get_resource) {
+		resource_list_for_each_entry_safe(win, tmp, resources) {
+			err = parser->get_resource(userdata, win, iobase);
+			if (err)
+				goto do_abort;
+		}
+	}
+
+	if (parser && parser->finalize)
+		return parser->finalize(userdata);
+
+	return 0;
+
+ do_abort:
+	if (parser && parser->abort)
+		parser->abort(userdata);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(pci_host_parse_request_of_pci_ranges);
+
+
+struct gen_pci_parser_context {
+	struct device	*dev;
+	struct resource	**bus_range;
+	int		res_valid;
+};
+
+static int gen_pci_resource_parser_get_resource(void *userdata,
+						struct resource_entry *win,
+						resource_size_t iobase)
+{
+	struct gen_pci_parser_context *ctx = userdata;
+	struct device *dev = ctx->dev;
+	struct resource *res = win->res;
+	int err;
+
+	switch (resource_type(res)) {
+	case IORESOURCE_IO:
+		err = pci_remap_iospace(res, iobase);
+		if (err) {
+			dev_warn(dev, "error %d: failed to map resource %pR\n",
+				 err, res);
+			resource_list_destroy_entry(win);
 		}
+		break;
+	case IORESOURCE_MEM:
+		ctx->res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+		break;
+	case IORESOURCE_BUS:
+		*ctx->bus_range = res;
+		break;
 	}
 
-	if (res_valid)
+	return 0;
+}
+
+static int gen_pci_resource_parser_finalize(void *userdata)
+{
+	struct gen_pci_parser_context *ctx = userdata;
+	struct device *dev = ctx->dev;
+
+	if (ctx->res_valid)
 		return 0;
 
 	dev_err(dev, "non-prefetchable memory resource required\n");
 	return -EINVAL;
 }
 
+static const struct pci_host_resource_parser gen_pci_resource_parser = {
+	.get_resource	= gen_pci_resource_parser_get_resource,
+	.finalize	= gen_pci_resource_parser_finalize,
+};
+
+static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
+		       struct list_head *resources, struct resource **bus_range)
+{

drivers/pci/host/pci-host-common.c static int gen_pci_parse_request_of_pci_ranges(struct device *dev, · line 138

Quoted 10 lines
drivers/pci/host/pci-host-common.c +10 -0
+	struct gen_pci_parser_context ctx;
+
+	ctx.dev = dev;
+	ctx.bus_range = bus_range;
+	ctx.res_valid = 0;
+
+	return pci_host_parse_request_of_pci_ranges(dev, resources,
+						    &gen_pci_resource_parser,
+						    &ctx);
+}

I think that what you are doing is something along the lines:

acpi_dev_get_resources()

and the struct res_proc_context but maybe we can do something simpler to start with.

We can go through the resource list twice in every driver, once through the generic function that we are creating - that embeds code common across bridges - to be clear something like:

static int of_pci_parse_request_pci_resources(struct device *dev,
struct list_head *res)

{
	int err, res_valid;
	resource_size_t iobase;
	struct device_node *np = dev->of_node;
	struct resource_entry *win, *tmp;

	err = of_pci_get_host_bridge_resources(np, 0, 0xff, res, &iobase);
	if (err)
		return err;

	err = devm_request_pci_bus_resources(dev, res);
	if (err)
		goto out_release_res;

	resource_list_for_each_entry_safe(win, tmp, res) {
		struct resource *res = win->res;

		switch (resource_type(res)) {
		case IORESOURCE_IO:
			err = pci_remap_iospace(res, iobase);
			if (err) {
				dev_warn(dev, "error %d: failed to map resource %pR\n",
					 err, res);
				resource_list_destroy_entry(win);
			}
			break;
		case IORESOURCE_MEM:
			res_valid |= !(res->flags & IORESOURCE_PREFETCH);
			break;
		}
	}

	if (res_valid)
		return 0;

	dev_err(dev, "non-prefetchable memory resource required\n");
	err = -EINVAL;

out_release_res:

	pci_free_resource_list(res);
	return err;
}

and then in a PCI host bridge specific loop that just carries out the host bridge specific actions for every resource returned in the res list.

Lorenzo

Quoted 5 lines
drivers/pci/host/pci-host-common.c +1 -1
+
 static void gen_pci_unmap_cfg(void *ptr)
 {
 	pci_ecam_free((struct pci_config_window *)ptr);
---8<--------------------------------------------------------------------------
#14 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Lorenzo Pieralisi

Hi Lorenzo,

Le 29/11/2017 à 18:34, Lorenzo Pieralisi a écrit :

Quoted 14 lines · Cyrille Pitchen
Quoted 1 line

This patch adds support to the Cadence PCIe controller in host mode.

Bjorn already commented on this, it would be good to add some of the cover letter details in this log.

Quoted 7 lines
---
 drivers/Makefile                        |   1 +
 drivers/pci/Kconfig                     |   1 +
 drivers/pci/cadence/Kconfig             |  24 ++
 drivers/pci/cadence/Makefile            |   2 +
 drivers/pci/cadence/pcie-cadence-host.c | 425 ++++++++++++++++++++++++++++++++
Quoted 1 line

You should also update the MAINTAINERS file.

I will ask Cadence who will be the maintainer.

Quoted 54 lines
Quoted 15 lines
 drivers/pci/cadence/pcie-cadence.c      | 110 +++++++++
 drivers/pci/cadence/pcie-cadence.h      | 325 ++++++++++++++++++++++++
 7 files changed, 888 insertions(+)
 create mode 100644 drivers/pci/cadence/Kconfig
 create mode 100644 drivers/pci/cadence/Makefile
 create mode 100644 drivers/pci/cadence/pcie-cadence-host.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.c
 create mode 100644 drivers/pci/cadence/pcie-cadence.h
drivers/Makefile +0 -0
index 1d034b680431..27bdd98784d9 100644

drivers/Makefile obj-y += pwm/ · line 21

Quoted 4 lines
drivers/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-y                               += pwm/
 
 obj-$(CONFIG_PCI)            += pci/
 obj-$(CONFIG_PCI_ENDPOINT)   += pci/endpoint/
+obj-$(CONFIG_PCI_CADENCE)    += pci/cadence/

Already commented on the cover letter.

Quoted 32 lines
drivers/Makefile +0 -0
 # PCI dwc controller drivers
 obj-y                                += pci/dwc/
 
drivers/pci/Kconfig +1 -0
index 90944667ccea..2471b2e36b8b 100644
@@ -144,6 +144,7 @@ config PCI_HYPERV
           PCI devices from a PCI backend to support PCI driver domains.
 
 source "drivers/pci/hotplug/Kconfig"
+source "drivers/pci/cadence/Kconfig"
 source "drivers/pci/dwc/Kconfig"
 source "drivers/pci/host/Kconfig"
 source "drivers/pci/endpoint/Kconfig"
drivers/pci/cadence/Kconfig new file +11 -0
new file mode 100644
index 000000000000..120306cae2aa
@@ -0,0 +1,24 @@
+menuconfig PCI_CADENCE
+     bool "Cadence PCI controllers support"
+     depends on PCI && HAS_IOMEM
+     help
+       Say Y here if you want to support some Cadence PCI controller.
+
+       When in doubt, say N.
+
+if PCI_CADENCE
+
+config PCIE_CADENCE
Quoted 9 lines

drivers/pci/cadence/Kconfig line 17

Quoted 6 lines
drivers/pci/cadence/Kconfig new file +6 -0
+     bool
+
+config PCIE_CADENCE_HOST
+     bool "Cadence PCIe host controller"
+     depends on OF
+     depends on PCI_MSI_IRQ_DOMAIN

I do not see the reason for this dependency in the code.

I will remove it if it's not needed.

Quoted 52 lines
Quoted 52 lines
drivers/pci/cadence/Kconfig new file +7 -0
+     select PCIE_CADENCE
+     help
+       Say Y here if you want to support the Cadence PCIe controller in host
+       mode. This PCIe controller may be embedded into many different vendors
+       SoCs.
+
+endif # PCI_CADENCE
drivers/pci/cadence/Makefile new file +2 -0
new file mode 100644
index 000000000000..d57d192d2595
@@ -0,0 +1,2 @@
+obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
+obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
drivers/pci/cadence/pcie-cadence-host.c new file +31 -0
new file mode 100644
index 000000000000..252471e72a93
@@ -0,0 +1,425 @@
+/*
+ * Cadence PCIe host controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of_address.h>
+#include <linux/of_pci.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+
+#include "pcie-cadence.h"
+
+/**
+ * struct cdns_pcie_rc_data - hardware specific data
+ * @max_regions: maximum number of regions supported by the hardware
+ * @vendor_id: PCI vendor ID
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 37

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+ * @device_id: PCI device ID
+ * @no_bar_nbits: Number of bits to keep for inbound (PCIe -> CPU) address
+ *                translation (nbits sets into the "no BAR match" register).
+ */
+struct cdns_pcie_rc_data {
+     size_t                  max_regions;

Reason for it to be size_t ?

No special reason, I can use another integer type.

Quoted 7 lines

drivers/pci/cadence/pcie-cadence-host.c line 41

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+     u16                     vendor_id;
+     u16                     device_id;
+     u8                      no_bar_nbits;
+};

I think that this data should come from DT (?) more below.

I can update the DT bindings documentation and the driver source code as needed.

Quoted 27 lines
Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +12 -0
+
+/**
+ * struct cdns_pcie_rc - private data for this PCIe Root Complex driver
+ * @pcie: Cadence PCIe controller
+ * @dev: pointer to PCIe device
+ * @cfg_res: start/end offsets in the physical system memory to map PCI
+ *           configuration space accesses
+ * @bus_range: first/last buses behind the PCIe host controller
+ * @cfg_base: IO mapped window to access the PCI configuration space of a
+ *            single function at a time
+ * @data: pointer to a 'struct cdns_pcie_rc_data'
+ */

drivers/pci/cadence/pcie-cadence-host.c line 63

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+struct cdns_pcie_rc {
+     struct cdns_pcie        pcie;
+     struct device           *dev;
+     struct resource         *cfg_res;
+     struct resource         *bus_range;
+     void __iomem            *cfg_base;
+     const struct cdns_pcie_rc_data  *data;
+};
+
+static void __iomem *

Please do not split lines like this, storage class and return type should be in the same line as the name, move parameter(s) to a new line.

Quoted 3 lines

static void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
int where)

ok :)

Quoted 4 lines
Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+     struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
+     struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+     struct cdns_pcie *pcie = &rc->pcie;
+     unsigned int busn = bus->number;
+     u32 addr0, desc0;
+
+     if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+             return NULL;

It does not hurt but I wonder whether you really need this check.

I can remove it.

Quoted 6 lines

drivers/pci/cadence/pcie-cadence-host.c line 76

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     if (busn == rc->bus_range->start) {
+             if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

If I have understood correctly, Cadence team told me that only the root port is available on the first bus through device 0, function 0. No other device/function should connected on this bus, all other devices are behind at least one PCI bridge.

I can add a comment here to explain that.

Quoted 12 lines

drivers/pci/cadence/pcie-cadence-host.c line 83

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+                     return NULL;
+
+             return pcie->reg_base + (where & 0xfff);
+     }
+
+     /* Update Output registers for AXI region 0. */
+     addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

The idea is to able to use only a 4KB memory area at a fixed address in the space allocated for the PCIe controller in the AXI bus. I guess the plan is to leave more space on the AXI bus to map all other PCIe devices.

This is just my guess. Anyway one purpose of this driver was actually to perform all PCI configuration space accesses through this single 4KB memory area in the AXI bus, changing the mapping dynamically to reach the relevant PCI device.

Quoted 16 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+     /* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c line 98

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+             CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+     /*
+      * The bus number was already set once for all in desc1 by
+      * cdns_pcie_host_init_address_translation().
+      */
+     if (busn == rc->bus_range->start + 1)
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+     else
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;
Quoted 3 lines

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

PCI configuration space registers of the root port can only be read through the APB bus at offset 0:
->reg_base + (where & 0xfff)

They are internal registers of the PCIe controller so no TLP on the PCIe bus.

However to access the PCI configuration space registers of any other device, the PCIe controller builds then sends a TLP on the PCIe bus using the offset in the 4KB AXI area as the offset of the register in the PCI configuration space:
->cfg_base + (where & 0xfff)

Quoted 12 lines
Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+     return rc->cfg_base + (where & 0xfff);
+}
+
+static struct pci_ops cdns_pcie_host_ops = {
+     .map_bus        = cdns_pci_map_bus,
+     .read           = pci_generic_config_read,
+     .write          = pci_generic_config_write,
+};
+
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 115

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+static const struct cdns_pcie_rc_data cdns_pcie_rc_data = {
+     .max_regions    = 32,
+     .vendor_id      = PCI_VENDOR_ID_CDNS,
+     .device_id      = 0x0200,
+     .no_bar_nbits   = 32,
+};

Should (some of) these parameters be retrieved through a DT binding ?

Indeed, maybe we get max_regions and no_bar_nbits from the DT.

About the vendor and device IDs, I don't know which would be the best choice between some dedicated DT properties or associating a custom structure as above to the 'compatible' string.

Honestly, I don't have any strong preference, please just tell me what you would prefer :)

Quoted 104 lines
Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+static const struct of_device_id cdns_pcie_host_of_match[] = {
+     { .compatible = "cdns,cdns-pcie-host",
+       .data = &cdns_pcie_rc_data },
+
+     { },
+};
+
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+                                              struct list_head *resources,
+                                              struct resource **bus_range)
+{
+     int err, res_valid = 0;
+     struct device_node *np = dev->of_node;
+     resource_size_t iobase;
+     struct resource_entry *win, *tmp;
+
+     err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+     if (err)
+             return err;
+
+     err = devm_request_pci_bus_resources(dev, resources);
+     if (err)
+             return err;
+
+     resource_list_for_each_entry_safe(win, tmp, resources) {
+             struct resource *res = win->res;
+
+             switch (resource_type(res)) {
+             case IORESOURCE_IO:
+                     err = pci_remap_iospace(res, iobase);
+                     if (err) {
+                             dev_warn(dev, "error %d: failed to map resource %pR\n",
+                                      err, res);
+                             resource_list_destroy_entry(win);
+                     }
+                     break;
+             case IORESOURCE_MEM:
+                     res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+                     break;
+             case IORESOURCE_BUS:

drivers/pci/cadence/pcie-cadence-host.c line 166

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+                     *bus_range = res;
+                     break;
+             }
+     }
+
+     if (res_valid)
+             return 0;
+
+     dev_err(dev, "non-prefetchable memory resource required\n");
+     return -EINVAL;

Nit, I prefer you swap these two as it is done in pci-aardvark.c:

        if (!res_valid) {
                dev_err(dev, "non-prefetchable memory resource required\n");
                return -EINVAL;
        }

        return 0;

but as per previous replies this function can be factorized in core PCI code - I would not bother unless you are willing to write the patch series that does the refactoring yourself :)

Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+}
+
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data = rc->data;
+     struct cdns_pcie *pcie = &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn = rc->bus_range->start;
+     sbn = pbn + 1; /* Single root port. */
+     subn = rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

I will test without those lines to test whether I can remove them.

At first, the PCIe controller was tested by Cadence team: there was code in their bootloader to initialize the hardware (building the AXI <-> PCIe mappings, ...): the bootloader used to set the primary, secondary and subordinate bus numbers in the root port PCI config space.

Also there was a hardware trick to redirect accesses of the lowest addresses in the AXI bus to the APB bus so the PCI configuration space of the root port could have been accessed from the AXI bus too.

The AXI <-> PCIe mapping being done by the bootloader and the root port config space being accessible from the AXI bus, it was possible to use the pci-host-generic driver.

However, the hardware trick won't be included in the final design since Cadence now wants to perform all PCI configuration space accesses through a small 4KB window at a fixed address on the AXI bus.
Also, we now want all initialisations to be done by the linux driver instead of the bootloader.

I simply moved all those initialisations from the bootloader to the linux driver but actually there is a chance that I can remove the 3 writes to the PCI_*_BUS registers.

Quoted 57 lines
Quoted 57 lines
drivers/pci/cadence/pcie-cadence-host.c new file +57 -0
+     return 0;
+}
+
+static int cdns_pcie_host_init_address_translation(struct cdns_pcie_rc *rc)
+{
+     struct cdns_pcie *pcie = &rc->pcie;
+     struct resource *cfg_res = rc->cfg_res;
+     struct resource *mem_res = pcie->mem_res;
+     struct resource *bus_range = rc->bus_range;
+     struct device *dev = rc->dev;
+     struct device_node *np = dev->of_node;
+     struct of_pci_range_parser parser;
+     struct of_pci_range range;
+     u32 addr0, addr1, desc1;
+     u64 cpu_addr;
+     int r, err;
+
+     /*
+      * Reserve region 0 for PCI configure space accesses:
+      * OB_REGION_PCI_ADDR0 and OB_REGION_DESC0 are updated dynamically by
+      * cdns_pci_map_bus(), other region registers are set here once for all.
+      */
+     addr1 = 0; /* Should be programmed to zero. */
+     desc1 = CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus_range->start);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(0), addr1);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(0), desc1);
+
+     cpu_addr = cfg_res->start - mem_res->start;
+     addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(12) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 = upper_32_bits(cpu_addr);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(0), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(0), addr1);
+
+     err = of_pci_range_parser_init(&parser, np);
+     if (err)
+             return err;
+
+     r = 1;
+     for_each_of_pci_range(&parser, &range) {
+             bool is_io;
+
+             if (r >= rc->data->max_regions)
+                     break;
+
+             if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
+                     is_io = false;
+             else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
+                     is_io = true;
+             else
+                     continue;
+
+             cdns_pcie_set_outbound_region(pcie, r, is_io,
+                                           range.cpu_addr,
+                                           range.pci_addr,
+                                           range.size);
+             r++;
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 271

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+     }
+
+     /*
+      * Set Root Port no BAR match Inbound Translation registers:
+      * needed for MSI.

And DMA :) if I understand what this is doing correctly, ie setting the root complex decoding for incoming memory traffic.

Yes, indeed :)

Quoted 29 lines
Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +12 -0
+      * Root Port BAR0 and BAR1 are disabled, hence no need to set their
+      * inbound translation registers.
+      */
+     addr0 = CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(rc->data->no_bar_nbits);
+     addr1 = 0;
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR0(RP_NO_BAR), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR1(RP_NO_BAR), addr1);
+
+     return 0;
+}
+
+static int cdns_pcie_host_init(struct device *dev,

drivers/pci/cadence/pcie-cadence-host.c line 293

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+                            struct list_head *resources,
+                            struct cdns_pcie_rc *rc)
+{
+     struct resource *bus_range = NULL;
+     int err;
+
+     /* Parse our PCI ranges and request their resources */
+     err = cdns_pcie_parse_request_of_pci_ranges(dev, resources, &bus_range);
+     if (err)
+             goto err_out;

I think that the err_out path should be part of:

cdns_pcie_parse_request_of_pci_ranges()

implementation and here you would just return.

Quoted 8 lines

drivers/pci/cadence/pcie-cadence-host.c line 298

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+
+     if (bus_range->start > bus_range->end) {
+             err = -EINVAL;
+             goto err_out;
+     }

Add a space here; this check seems useless to me anyway.

I can remove this too.

Quoted 60 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+     rc->bus_range = bus_range;
+     rc->pcie.bus = bus_range->start;
+
+     err = cdns_pcie_host_init_root_port(rc);
+     if (err)

drivers/pci/cadence/pcie-cadence-host.c line 313

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+             goto err_out;
+
+     err = cdns_pcie_host_init_address_translation(rc);
+     if (err)
+             goto err_out;
+
+     return 0;
+
+ err_out:
+     pci_free_resource_list(resources);

See above.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-host.c new file +14 -0
+     return err;
+}
+
+static int cdns_pcie_host_probe(struct platform_device *pdev)
+{
+     const struct of_device_id *of_id;
+     const char *type;
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev->of_node;
+     struct pci_bus *bus, *child;
+     struct pci_host_bridge *bridge;
+     struct list_head resources;
+     struct cdns_pcie_rc *rc;
+     struct cdns_pcie *pcie;

drivers/pci/cadence/pcie-cadence-host.c line 337

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     struct resource *res;
+     int ret;
+
+     bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
+     if (!bridge)
+             return -ENOMEM;
+
+     rc = pci_host_bridge_priv(bridge);
+     rc->dev = dev;
+     platform_set_drvdata(pdev, rc);

I do not think it is needed.

Quoted 15 lines
drivers/pci/cadence/pcie-cadence-host.c new file +15 -0
+     pcie = &rc->pcie;
+     pcie->is_rc = true;
+
+     of_id = of_match_node(cdns_pcie_host_of_match, np);
+     rc->data = (const struct cdns_pcie_rc_data *)of_id->data;
+
+     type = of_get_property(np, "device_type", NULL);
+     if (!type || strcmp(type, "pci")) {
+             dev_err(dev, "invalid \"device_type\" %s\n", type);
+             return -EINVAL;
+     }
+
+     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+     pcie->reg_base = devm_ioremap_resource(dev, res);
+     if (IS_ERR(pcie->reg_base)) {
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 359

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+             dev_err(dev, "missing \"reg\"\n");
+             return PTR_ERR(pcie->reg_base);
+     }
+
+     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
+     rc->cfg_base = devm_ioremap_resource(dev, res);

devm_pci_remap_cfg_resource() please.

I will change that.

Quoted 257 lines
Quoted 136 lines
drivers/pci/cadence/pcie-cadence-host.c new file +66 -0
+     if (IS_ERR(rc->cfg_base)) {
+             dev_err(dev, "missing \"cfg\"\n");
+             return PTR_ERR(rc->cfg_base);
+     }
+     rc->cfg_res = res;
+
+     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+     if (!res) {
+             dev_err(dev, "missing \"mem\"\n");
+             return -EINVAL;
+     }
+     pcie->mem_res = res;
+
+     pm_runtime_enable(dev);
+     ret = pm_runtime_get_sync(dev);
+     if (ret < 0) {
+             dev_err(dev, "pm_runtime_get_sync() failed\n");
+             goto err_get_sync;
+     }
+
+     INIT_LIST_HEAD(&resources);
+     ret = cdns_pcie_host_init(dev, &resources, rc);
+     if (ret)
+             goto err_init;
+
+     list_splice_init(&resources, &bridge->windows);
+     bridge->dev.parent = dev;
+     bridge->busnr = pcie->bus;
+     bridge->ops = &cdns_pcie_host_ops;
+     bridge->map_irq = of_irq_parse_and_map_pci;
+     bridge->swizzle_irq = pci_common_swizzle;
+
+     ret = pci_scan_root_bus_bridge(bridge);
+     if (ret < 0) {
+             dev_err(dev, "Scanning root bridge failed");
+             goto err_init;
+     }
+
+     bus = bridge->bus;
+     pci_bus_size_bridges(bus);
+     pci_bus_assign_resources(bus);
+
+     list_for_each_entry(child, &bus->children, node)
+             pcie_bus_configure_settings(child);
+
+     pci_bus_add_devices(bus);
+
+     return 0;
+
+ err_init:
+     pm_runtime_put_sync(dev);
+
+ err_get_sync:
+     pm_runtime_disable(dev);
+
+     return ret;
+}
+
+static struct platform_driver cdns_pcie_host_driver = {
+     .driver = {
+             .name = "cdns-pcie-host",
+             .of_match_table = cdns_pcie_host_of_match,
+     },
+     .probe = cdns_pcie_host_probe,
+};
+builtin_platform_driver(cdns_pcie_host_driver);
drivers/pci/cadence/pcie-cadence.c new file +64 -0
new file mode 100644
index 000000000000..5c10879d5e96
@@ -0,0 +1,110 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/kernel.h>
+
+#include "pcie-cadence.h"
+
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool is_io,
+                                u64 cpu_addr, u64 pci_addr, size_t size)
+{
+     /*
+      * roundup_pow_of_two() returns an unsigned long, which is not suited
+      * for 64bit values.
+      */
+     u64 sz = 1ULL << fls64(size - 1);
+     int nbits = ilog2(sz);
+     u32 addr0, addr1, desc0, desc1;
+
+     if (nbits < 8)
+             nbits = 8;
+
+     /* Set the PCI address */
+     addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
+             (lower_32_bits(pci_addr) & GENMASK(31, 8));
+     addr1 = upper_32_bits(pci_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
+
+     /* Set the PCIe header descriptor */
+     if (is_io)
+             desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
+     else
+             desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
+     desc1 = 0;
+
+     if (pcie->is_rc) {
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+                      CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+             desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+     }
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+
+     /* Set the CPU address */
+     cpu_addr -= pcie->mem_res->start;

drivers/pci/cadence/pcie-cadence.c line 74

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.c new file +10 -0
+     addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 = upper_32_bits(cpu_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u32 r,
+                                               u64 cpu_addr)

Not used in this patch, you should split it out.

Quoted 108 lines
drivers/pci/cadence/pcie-cadence.c new file +36 -0
+{
+     u32 addr0, addr1, desc0, desc1;
+
+     desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
+     desc1 = 0;
+     if (pcie->is_rc) {
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+                      CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+             desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
+     }
+
+     /* Set the CPU address */
+     cpu_addr -= pcie->mem_res->start;
+     addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
+             (lower_32_bits(cpu_addr) & GENMASK(31, 8));
+     addr1 = upper_32_bits(cpu_addr);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
+}
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
+{
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
+
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
+}
drivers/pci/cadence/pcie-cadence.h new file +66 -0
new file mode 100644
index 000000000000..195e23b7d4fe
@@ -0,0 +1,325 @@
+/*
+ * Cadence PCIe controller driver.
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PCIE_CADENCE_H
+#define _PCIE_CADENCE_H
+
+#include <linux/kernel.h>
+#include <linux/pci.h>
+
+/*
+ * Local Management Registers
+ */
+#define CDNS_PCIE_LM_BASE    0x00100000
+
+/* Vendor ID Register */
+#define CDNS_PCIE_LM_ID              (CDNS_PCIE_LM_BASE + 0x0044)
+#define  CDNS_PCIE_LM_ID_VENDOR_MASK GENMASK(15, 0)
+#define  CDNS_PCIE_LM_ID_VENDOR_SHIFT        0
+#define  CDNS_PCIE_LM_ID_VENDOR(vid) \
+     (((vid) << CDNS_PCIE_LM_ID_VENDOR_SHIFT) & CDNS_PCIE_LM_ID_VENDOR_MASK)
+#define  CDNS_PCIE_LM_ID_SUBSYS_MASK GENMASK(31, 16)
+#define  CDNS_PCIE_LM_ID_SUBSYS_SHIFT        16
+#define  CDNS_PCIE_LM_ID_SUBSYS(sub) \
+     (((sub) << CDNS_PCIE_LM_ID_SUBSYS_SHIFT) & CDNS_PCIE_LM_ID_SUBSYS_MASK)
+
+/* Root Port Requestor ID Register */
+#define CDNS_PCIE_LM_RP_RID  (CDNS_PCIE_LM_BASE + 0x0228)
+#define  CDNS_PCIE_LM_RP_RID_MASK    GENMASK(15, 0)
+#define  CDNS_PCIE_LM_RP_RID_SHIFT   0
+#define  CDNS_PCIE_LM_RP_RID_(rid) \
+     (((rid) << CDNS_PCIE_LM_RP_RID_SHIFT) & CDNS_PCIE_LM_RP_RID_MASK)
+
+/* Endpoint Bus and Device Number Register */
+#define CDNS_PCIE_LM_EP_ID   (CDNS_PCIE_LM_BASE + 0x022c)
+#define  CDNS_PCIE_LM_EP_ID_DEV_MASK GENMASK(4, 0)
+#define  CDNS_PCIE_LM_EP_ID_DEV_SHIFT        0
+#define  CDNS_PCIE_LM_EP_ID_BUS_MASK GENMASK(15, 8)
+#define  CDNS_PCIE_LM_EP_ID_BUS_SHIFT        8
+
+/* Endpoint Function f BAR b Configuration Registers */
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn) \
+     (CDNS_PCIE_LM_BASE + 0x0240 + (fn) * 0x0008)
+#define CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn) \
+     (CDNS_PCIE_LM_BASE + 0x0244 + (fn) * 0x0008)
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) \
+     (GENMASK(4, 0) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, a) \
+     (((a) << ((b) * 8)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b) \
Quoted 9 lines

drivers/pci/cadence/pcie-cadence.h line 72

Quoted 6 lines
drivers/pci/cadence/pcie-cadence.h new file +6 -0
+     (GENMASK(7, 5) << ((b) * 8))
+#define  CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, c) \
+     (((c) << ((b) * 8 + 5)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b))
+
+/* Endpoint Function Configuration Register */
+#define CDNS_PCIE_LM_EP_FUNC_CFG     (CDNS_PCIE_LM_BASE + 0x02c0)

All endpoint defines should be moved to the patch that needs them.

Will move those definitions into the endpoint patch.

Thanks for the review!

Best regards,

Cyrille

Quoted 263 lines
Quoted 233 lines
drivers/pci/cadence/pcie-cadence.h new file +233 -0
+
+/* Root Complex BAR Configuration Register */
+#define CDNS_PCIE_LM_RC_BAR_CFG      (CDNS_PCIE_LM_BASE + 0x0300)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK  GENMASK(5, 0)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE(a) \
+     (((a) << 0) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK              GENMASK(8, 6)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(c) \
+     (((c) << 6) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK  GENMASK(13, 9)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE(a) \
+     (((a) << 9) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK              GENMASK(16, 14)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(c) \
+     (((c) << 14) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE BIT(17)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_32BITS 0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS BIT(18)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE           BIT(19)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_16BITS           0
+#define  CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS           BIT(20)
+#define  CDNS_PCIE_LM_RC_BAR_CFG_CHECK_ENABLE                BIT(31)
+
+/* BAR control values applicable to both Endpoint Function and Root Complex */
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED          0x0
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS         0x1
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS                0x4
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS       0x5
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS                0x6
+#define  CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS       0x7
+
+
+/*
+ * Endpoint Function Registers (PCI configuration space for endpoint functions)
+ */
+#define CDNS_PCIE_EP_FUNC_BASE(fn)   (((fn) << 12) & GENMASK(19, 12))
+
+#define CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET     0x90
+
+/*
+ * Root Port Registers (PCI configuration space for the root port function)
+ */
+#define CDNS_PCIE_RP_BASE    0x00200000
+
+
+/*
+ * Address Translation Registers
+ */
+#define CDNS_PCIE_AT_BASE    0x00400000
+
+/* Region r Outbound AXI to PCIe Address Translation Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0000 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK GENMASK(19, 12)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) \
+     (((devfn) << 12) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK   GENMASK(27, 20)
+#define  CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(bus) \
+     (((bus) << 20) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK)
+
+/* Region r Outbound AXI to PCIe Address Translation Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r) \
+     (CDNS_PCIE_AT_BASE + 0x0004 + ((r) & 0x1f) * 0x0020)
+
+/* Region r Outbound PCIe Descriptor Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_DESC0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0008 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MASK              GENMASK(3, 0)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM               0x2
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO                0x6
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0        0xa
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1        0xb
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG        0xc
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_VENDOR_MSG        0xd
+/* Bit 23 MUST be set in RC mode. */
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID  BIT(23)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK     GENMASK(31, 24)
+#define  CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(devfn) \
+     (((devfn) << 24) & CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK)
+
+/* Region r Outbound PCIe Descriptor Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_DESC1(r)      \
+     (CDNS_PCIE_AT_BASE + 0x000c + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK       GENMASK(7, 0)
+#define  CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus) \
+     ((bus) & CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK)
+
+/* Region r AXI Region Base Address Register 0 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r) \
+     (CDNS_PCIE_AT_BASE + 0x0018 + ((r) & 0x1f) * 0x0020)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK GENMASK(5, 0)
+#define  CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK)
+
+/* Region r AXI Region Base Address Register 1 */
+#define CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r) \
+     (CDNS_PCIE_AT_BASE + 0x001c + ((r) & 0x1f) * 0x0020)
+
+/* Root Port BAR Inbound PCIe to AXI Address Translation Register */
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR0(bar) \
+     (CDNS_PCIE_AT_BASE + 0x0800 + (bar) * 0x0008)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK     GENMASK(5, 0)
+#define  CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(nbits) \
+     (((nbits) - 1) & CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK)
+#define CDNS_PCIE_AT_IB_RP_BAR_ADDR1(bar) \
+     (CDNS_PCIE_AT_BASE + 0x0804 + (bar) * 0x0008)
+
+enum cdns_pcie_rp_bar {
+     RP_BAR0,
+     RP_BAR1,
+     RP_NO_BAR
+};
+
+/* Endpoint Function BAR Inbound PCIe to AXI Address Translation Register */
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar) \
+     (CDNS_PCIE_AT_BASE + 0x0840 + (fn) * 0x0040 + (bar) * 0x0008)
+#define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar) \
+     (CDNS_PCIE_AT_BASE + 0x0844 + (fn) * 0x0040 + (bar) * 0x0008)
+
+/* Normal/Vendor specific message access: offset inside some outbound region */
+#define CDNS_PCIE_NORMAL_MSG_ROUTING_MASK    GENMASK(7, 5)
+#define CDNS_PCIE_NORMAL_MSG_ROUTING(route) \
+     (((route) << 5) & CDNS_PCIE_NORMAL_MSG_ROUTING_MASK)
+#define CDNS_PCIE_NORMAL_MSG_CODE_MASK               GENMASK(15, 8)
+#define CDNS_PCIE_NORMAL_MSG_CODE(code) \
+     (((code) << 8) & CDNS_PCIE_NORMAL_MSG_CODE_MASK)
+#define CDNS_PCIE_MSG_NO_DATA                        BIT(16)
+
+enum cdns_pcie_msg_code {
+     MSG_CODE_ASSERT_INTA    = 0x20,
+     MSG_CODE_ASSERT_INTB    = 0x21,
+     MSG_CODE_ASSERT_INTC    = 0x22,
+     MSG_CODE_ASSERT_INTD    = 0x23,
+     MSG_CODE_DEASSERT_INTA  = 0x24,
+     MSG_CODE_DEASSERT_INTB  = 0x25,
+     MSG_CODE_DEASSERT_INTC  = 0x26,
+     MSG_CODE_DEASSERT_INTD  = 0x27,
+};
+
+enum cdns_pcie_msg_routing {
+     /* Route to Root Complex */
+     MSG_ROUTING_TO_RC,
+
+     /* Use Address Routing */
+     MSG_ROUTING_BY_ADDR,
+
+     /* Use ID Routing */
+     MSG_ROUTING_BY_ID,
+
+     /* Route as Broadcast Message from Root Complex */
+     MSG_ROUTING_BCAST,
+
+     /* Local message; terminate at receiver (INTx messages) */
+     MSG_ROUTING_LOCAL,
+
+     /* Gather & route to Root Complex (PME_TO_Ack message) */
+     MSG_ROUTING_GATHER,
+};
+
+/**
+ * struct cdns_pcie - private data for Cadence PCIe controller drivers
+ * @reg_base: IO mapped register base
+ * @mem_res: start/end offsets in the physical system memory to map PCI accesses
+ * @is_rc: tell whether the PCIe controller mode is Root Complex or Endpoint.
+ * @bus: In Root Complex mode, the bus number
+ */
+struct cdns_pcie {
+     void __iomem            *reg_base;
+     struct resource         *mem_res;
+     bool                    is_rc;
+     u8                      bus;
+};
+
+/* Register access */
+static inline void cdns_pcie_writeb(struct cdns_pcie *pcie, u32 reg, u8 value)
+{
+     writeb(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writew(struct cdns_pcie *pcie, u32 reg, u16 value)
+{
+     writew(value, pcie->reg_base + reg);
+}
+
+static inline void cdns_pcie_writel(struct cdns_pcie *pcie, u32 reg, u32 value)
+{
+     writel(value, pcie->reg_base + reg);
+}
+
+static inline u32 cdns_pcie_readl(struct cdns_pcie *pcie, u32 reg)
+{
+     return readl(pcie->reg_base + reg);
+}
+
+/* Root Port register access */
+static inline void cdns_pcie_rp_writeb(struct cdns_pcie *pcie,
+                                    u32 reg, u8 value)
+{
+     writeb(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+static inline void cdns_pcie_rp_writew(struct cdns_pcie *pcie,
+                                    u32 reg, u16 value)
+{
+     writew(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg);
+}
+
+/* Endpoint Function register access */
+static inline void cdns_pcie_ep_fn_writeb(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u8 value)
+{
+     writeb(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writew(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u16 value)
+{
+     writew(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline void cdns_pcie_ep_fn_writel(struct cdns_pcie *pcie, u8 fn,
+                                       u32 reg, u16 value)
+{
+     writel(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u8 cdns_pcie_ep_fn_readb(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+     return readb(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}

drivers/pci/cadence/pcie-cadence.h line 315

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +10 -0
+
+static inline u16 cdns_pcie_ep_fn_readw(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+     return readw(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}
+
+static inline u32 cdns_pcie_ep_fn_readl(struct cdns_pcie *pcie, u8 fn, u32 reg)
+{
+     return readl(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
+}

Same comments for all endpoint related functions and defines above.

Thanks,
Lorenzo

Quoted 10 lines
drivers/pci/cadence/pcie-cadence.h new file +9 -1
+void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u32 r, bool is_io,
+                                u64 cpu_addr, u64 pci_addr, size_t size);
+
+void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u32 r,
+                                               u64 cpu_addr);
+
+void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r);
+
+#endif /* _PCIE_CADENCE_H */
--
Quoted 2 lines

2.11.0

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Signature · 3 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#25 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Lorenzo Pieralisi

Hi Lorenzo,

Quoted 114 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
 	  mode. This PCIe controller may be embedded into many different vendors
 	  SoCs.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on PCI_ENDPOINT
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *

drivers/pci/cadence/pcie-cadence-ep.c line 26

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/pci-epc.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/sizes.h>
+#include <linux/delay.h>

Nit: alphabetical order.

drivers/pci/cadence/pcie-cadence-ep.c line 36

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +9 -0
+#include "pcie-cadence.h"
+
+#define CDNS_PCIE_EP_MIN_APERTURE		128	/* 128 bytes */
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE		0x1
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY	0x3
+
+/**
+ * struct cdns_pcie_ep_data - hardware specific data
+ * @max_regions: maximum nmuber of regions supported by hardware

s/nmuber/number

Quoted 24 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +24 -0
+ */
+struct cdns_pcie_ep_data {
+	size_t				max_regions;
+};
+
+/**
+ * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
+ * @pcie: Cadence PCIe controller
+ * @data: pointer to a 'struct cdns_pcie_data'
+ */
+struct cdns_pcie_ep {
+	struct cdns_pcie		pcie;
+	const struct cdns_pcie_ep_data	*data;
+	struct pci_epc			*epc;
+	unsigned long			ob_region_map;
+	phys_addr_t			*ob_addr;
+	phys_addr_t			irq_phys_addr;
+	void __iomem			*irq_cpu_addr;
+	u64				irq_pci_addr;
+	u8				irq_pending;
+};
+
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

I've used some "u8 fn = 0;" when implementing every handler of 'struct pci_epc_ops' because the current API of the EPC library supports only one function for now but I guess this will change soon.

So I've implement each handler as if a new 'fn' parameter were added to the handler prototype. I guess it would make sense to add such a parameter to the pci_epc_*() functions in pci-epc-core.c.

Quoted 36 lines
Quoted 36 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +36 -0
+		u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
+			 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
+		cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
+	}
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
+			       hdr->subclass_code | hdr->baseclass_code << 8);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
+			       hdr->cache_line_size);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_set_bar(struct pci_epc *epc, enum pci_barno bar,
+				dma_addr_t bar_phys, size_t size, int flags)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
+	u8 fn = 0;
+	u64 sz;
+
+	/* BAR size is 2^(aperture + 7) */
+	sz = max_t(size_t, size, CDNS_PCIE_EP_MIN_APERTURE);
+	sz = 1ULL << fls64(sz - 1);
+	aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
+
+	if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
+		ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
+	} else {
+		bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
+		bool is_64bits = sz > SZ_2G;
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 107

Quoted 5 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +5 -0
+
+		if (is_64bits && (bar & 1))
+			return -EINVAL;
+
+		switch (is_64bits << 1 | is_prefetch) {

I would not mind implementing this as a nested if-else, I am not a big fan of using bool this way.

It did it this way to cope with the 80 columns rule but I will try to do it with nested if-elst statements. The macro names I've chosen are quite long!

Quoted 18 lines
Quoted 18 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +18 -0
+		case 0:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
+			break;
+
+		case 1:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
+			break;
+
+		case 2:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
+			break;
+
+		case 3:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
+			break;
+		}
+	}
+
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 131

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+	addr0 = lower_32_bits(bar_phys);
+	addr1 = upper_32_bits(bar_phys);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
+			 addr0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
+			 addr1);

Is fn always 0 ?

If the API of the EPC library is updated, fn could be something else but 0 later but for now, fn is actually always 0.

Quoted 55 lines
Quoted 42 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +42 -0
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
+		CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	return 0;
+}
+
+static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, enum pci_barno bar)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 reg, cfg, b, ctrl;
+	u8 fn = 0;
+
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(ctrl);
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);

drivers/pci/cadence/pcie-cadence-ep.c line 184

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static int cdns_pcie_ep_map_addr(struct pci_epc *epc, phys_addr_t addr,
+				 u64 pci_addr, size_t size)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	r = find_first_zero_bit(&ep->ob_region_map, sizeof(ep->ob_region_map));

Second argument must be in bits not bytes.

Quoted 2 lines

https://marc.info/?l=linux-pci&m=151179781225513&w=2

OK, thanks!

Quoted 18 lines
Quoted 18 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +18 -0
+	if (r >= ep->data->max_regions - 1) {
+		dev_err(&epc->dev, "no free outbound region\n");
+		return -EINVAL;
+	}
+
+	cdns_pcie_set_outbound_region(pcie, r, false, addr, pci_addr, size);
+
+	set_bit(r, &ep->ob_region_map);
+	ep->ob_addr[r] = addr;
+
+	return 0;
+}
+
+static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, phys_addr_t addr)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 208

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+
+	for (r = 0; r < ep->data->max_regions - 1; r++)
+		if (ep->ob_addr[r] == addr)
+			break;
+
+	if (r >= ep->data->max_regions - 1)

== ?

I will change that.

Quoted 110 lines
Quoted 50 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +50 -0
+		return;
+
+	cdns_pcie_reset_outbound_region(pcie, r);
+
+	ep->ob_addr[r] = 0;
+	clear_bit(r, &ep->ob_region_map);
+}
+
+static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 mmc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/*
+	 * Set the Multiple Message Capable bitfield into the Message Control
+	 * register.
+	 */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
+	flags |= PCI_MSI_FLAGS_64BIT;
+	flags &= ~PCI_MSI_FLAGS_MASKBIT;
+	cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_get_msi(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/* Validate that the MSI feature is actually enabled. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+

drivers/pci/cadence/pcie-cadence-ep.c line 268

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	/*
+	 * Get the Multiple Message Enable bitfield from the Message Control
+	 * register.
+	 */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

You should comment on what this 5 means and why it is fine to cap mme.

Quoted 47 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +47 -0
+
+	return mme;
+}
+
+static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
+				     u8 intx, bool is_asserted)
+{
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r = ep->data->max_regions - 1;
+	u32 offset;
+	u16 status;
+	u8 msg_code;
+
+	intx &= 3;
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region_for_normal_msg(pcie, r,
+							     ep->irq_phys_addr);
+		ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
+	}
+
+	if (is_asserted) {
+		ep->irq_pending |= BIT(intx);
+		msg_code = MSG_CODE_ASSERT_INTA + intx;
+	} else {
+		ep->irq_pending &= ~BIT(intx);
+		msg_code = MSG_CODE_DEASSERT_INTA + intx;
+	}
+
+	status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
+	if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
+		status ^= PCI_STATUS_INTERRUPT;
+		cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
+	}
+
+	offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
+		 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
+		 CDNS_PCIE_MSG_NO_DATA;
+	writel(0, ep->irq_cpu_addr + offset);
+}
+
+static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
+{
+	u16 cmd;
+
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 321

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+	cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
+	if (cmd & PCI_COMMAND_INTX_DISABLE)
+		return -EINVAL;
+
+	cdns_pcie_ep_assert_intx(ep, fn, intx, true);
+	mdelay(1);

Add a comment please to explain the mdelay value.

Honestly, I don't know for this value: I've just adapted the source code from what was already done by dra7xx_pcie_raise_legacy_irq() from drivers/pci/dwc/pci-dra7xx.c.

Quoted 95 lines
Quoted 25 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +25 -0
+	cdns_pcie_ep_assert_intx(ep, fn, intx, false);
+	return 0;
+}
+
+static int cdns_pcie_ep_raise_irq(struct pci_epc *epc,
+				  enum pci_epc_irq_type type, u8 interrupt_num)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme, data, data_mask;
+	u8 msi_count;
+	u64 pci_addr, pci_addr_mask = 0xff;
+	u8 fn = 0;
+
+	/* Handle legacy IRQ. */
+	if (type == PCI_EPC_IRQ_LEGACY)
+		return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
+
+	/* Otherwise MSI. */
+	if (type != PCI_EPC_IRQ_MSI)
+		return -EINVAL;
+
+	/* Check whether the MSI feature has been enabled by the PCI host. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);

drivers/pci/cadence/pcie-cadence-ep.c line 356

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/* Get the number of enabled MSIs */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

Same comment as above.

Quoted 57 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +57 -0
+	msi_count = 1 << mme;
+	if (!interrupt_num || interrupt_num > msi_count)
+		return -EINVAL;
+
+	/* Compute the data value to be written. */
+	data_mask = msi_count - 1;
+	data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
+	data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
+
+	/* Get the PCI address where to write the data into. */
+	pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
+	pci_addr <<= 32;
+	pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
+	pci_addr &= GENMASK_ULL(63, 2);
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != pci_addr)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region(pcie, ep->data->max_regions - 1,
+					      false,
+					      ep->irq_phys_addr,
+					      pci_addr & ~pci_addr_mask,
+					      pci_addr_mask + 1);
+		ep->irq_pci_addr = pci_addr;
+	}
+	writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
+
+	return 0;
+}
+
+static int cdns_pcie_ep_start(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	struct pci_epf *epf;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Enable this endpoint function. */
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg |= BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+
+	/*
+	 * Already linked-up: don't call directly pci_epc_linkup() because we've
+	 * already locked the epc->lock.
+	 */
+	list_for_each_entry(epf, &epc->pci_epf, list)
+		pci_epf_linkup(epf);
+
+	return 0;
+}
+
+static void cdns_pcie_ep_stop(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 418

Quoted 4 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +4 -0
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Disable this endpoint function (function 0 can't be disabled). */

I do not understand this comment and how it applies to the code, in other words fn is always 0 here (so it can't be disabled) I do not understand what this code is there for.

fn == 0 for the same reason as for other 'struct pci_epc_ops' handlers. I added this comment above because BIT(0) is hard-wired to 1: in other word function 0 is the only one that can't be disabled.

So the current code actually does nothing. I've implemented anyway still hoping that the API would change so function other than 0 could be used.

Quoted 15 lines
Quoted 15 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +15 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg &= ~BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+}
+
+static const struct pci_epc_ops cdns_pcie_epc_ops = {
+	.write_header	= cdns_pcie_ep_write_header,
+	.set_bar	= cdns_pcie_ep_set_bar,
+	.clear_bar	= cdns_pcie_ep_clear_bar,
+	.map_addr	= cdns_pcie_ep_map_addr,
+	.unmap_addr	= cdns_pcie_ep_unmap_addr,
+	.set_msi	= cdns_pcie_ep_set_msi,
+	.get_msi	= cdns_pcie_ep_get_msi,
+	.raise_irq	= cdns_pcie_ep_raise_irq,
+	.start		= cdns_pcie_ep_start,
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-ep.c line 439

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+	.stop		= cdns_pcie_ep_stop,
+};
+
+static const struct cdns_pcie_ep_data cdns_pcie_ep_data = {
+	.max_regions	= 16,
+};

As I mentioned in patch 3, should this be set-up with DT ?

OK, I'll move it as a DT property.

Best regards,

Cyrille

Quoted 121 lines

Thanks,
Lorenzo

Quoted 117 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +114 -0
+
+static const struct of_device_id cdns_pcie_ep_of_match[] = {
+	{ .compatible = "cdns,cdns-pcie-ep",
+	  .data = &cdns_pcie_ep_data },
+
+	{ },
+};
+
+static int cdns_pcie_ep_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	const struct of_device_id *of_id;
+	struct cdns_pcie_ep *ep;
+	struct cdns_pcie *pcie;
+	struct pci_epc *epc;
+	struct resource *res;
+	size_t max_regions;
+	int ret;
+
+	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
+	if (!ep)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, ep);
+
+	pcie = &ep->pcie;
+	pcie->is_rc = false;
+
+	of_id = of_match_node(cdns_pcie_ep_of_match, np);
+	ep->data = (const struct cdns_pcie_ep_data *)of_id->data;
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+	pcie->reg_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(pcie->reg_base)) {
+		dev_err(dev, "missing \"reg\"\n");
+		return PTR_ERR(pcie->reg_base);
+	}
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+	if (!res) {
+		dev_err(dev, "missing \"mem\"\n");
+		return -EINVAL;
+	}
+	pcie->mem_res = res;
+
+	max_regions = ep->data->max_regions;
+	ep->ob_addr = devm_kzalloc(dev, max_regions * sizeof(*ep->ob_addr),
+				   GFP_KERNEL);
+	if (!ep->ob_addr)
+		return -ENOMEM;
+
+	pm_runtime_enable(dev);
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		dev_err(dev, "pm_runtime_get_sync() failed\n");
+		goto err_get_sync;
+	}
+
+	/* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
+
+	epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
+	if (IS_ERR(epc)) {
+		dev_err(dev, "failed to create epc device\n");
+		ret = PTR_ERR(epc);
+		goto err_init;
+	}
+
+	ep->epc = epc;
+	epc_set_drvdata(epc, ep);
+
+	ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
+	if (ret < 0)
+		epc->max_functions = 1;
+
+	ret = pci_epc_mem_init(epc, pcie->mem_res->start,
+			       resource_size(pcie->mem_res));
+	if (ret < 0) {
+		dev_err(dev, "failed to initialize the memory space\n");
+		goto err_init;
+	}
+
+	ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
+						  SZ_128K);
+	if (!ep->irq_cpu_addr) {
+		dev_err(dev, "failed to reserve memory space for MSI\n");
+		ret = -ENOMEM;
+		goto free_epc_mem;
+	}
+	ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
+
+	return 0;
+
+ free_epc_mem:
+	pci_epc_mem_exit(epc);
+
+ err_init:
+	pm_runtime_put_sync(dev);
+
+ err_get_sync:
+	pm_runtime_disable(dev);
+
+	return ret;
+}
+
+static struct platform_driver cdns_pcie_ep_driver = {
+	.driver = {
+		.name = "cdns-pcie-ep",
+		.of_match_table = cdns_pcie_ep_of_match,
+	},
+	.probe = cdns_pcie_ep_probe,
+};
+builtin_platform_driver(cdns_pcie_ep_driver);
Signature · 1 line
2.11.0
Signature · 3 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#15 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Cyrille Pitchen

[+Ard]

Hi Cyrille,

On Sun, Dec 03, 2017 at 09:44:46PM +0100, Cyrille Pitchen wrote:

[...]

Quoted 23 lines
Quoted 4 lines
Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+     struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
+     struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+     struct cdns_pcie *pcie = &rc->pcie;
+     unsigned int busn = bus->number;
+     u32 addr0, desc0;
+
+     if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+             return NULL;

It does not hurt but I wonder whether you really need this check.

I can remove it.

Quoted 6 lines

drivers/pci/cadence/pcie-cadence-host.c line 76

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     if (busn == rc->bus_range->start) {
+             if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

Quoted 6 lines

If I have understood correctly, Cadence team told me that only the root port is available on the first bus through device 0, function 0. No other device/function should connected on this bus, all other devices are behind at least one PCI bridge.

I can add a comment here to explain that.

That's understood, the question is what happens if you do scan devfn != 0.

Quoted 13 lines
Quoted 12 lines

drivers/pci/cadence/pcie-cadence-host.c line 83

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+                     return NULL;
+
+             return pcie->reg_base + (where & 0xfff);
+     }
+
+     /* Update Output registers for AXI region 0. */
+     addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

Quoted 8 lines

The idea is to able to use only a 4KB memory area at a fixed address in the space allocated for the PCIe controller in the AXI bus. I guess the plan is to leave more space on the AXI bus to map all other PCIe devices.

This is just my guess. Anyway one purpose of this driver was actually to perform all PCI configuration space accesses through this single 4KB memory area in the AXI bus, changing the mapping dynamically to reach the relevant PCI device.

Thank you for explaining - that matches my understanding.

Quoted 54 lines
Quoted 16 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+     /* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c line 98

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+             CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+     /*
+      * The bus number was already set once for all in desc1 by
+      * cdns_pcie_host_init_address_translation().
+      */
+     if (busn == rc->bus_range->start + 1)
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+     else
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;
Quoted 3 lines

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

PCI configuration space registers of the root port can only be read through the APB bus at offset 0:
->reg_base + (where & 0xfff)

They are internal registers of the PCIe controller so no TLP on the PCIe bus.

However to access the PCI configuration space registers of any other device, the PCIe controller builds then sends a TLP on the PCIe bus using the offset in the 4KB AXI area as the offset of the register in the PCI configuration space:
->cfg_base + (where & 0xfff)

Quoted 12 lines
Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+     return rc->cfg_base + (where & 0xfff);
+}
+
+static struct pci_ops cdns_pcie_host_ops = {
+     .map_bus        = cdns_pci_map_bus,
+     .read           = pci_generic_config_read,
+     .write          = pci_generic_config_write,
+};
+
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 115

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+static const struct cdns_pcie_rc_data cdns_pcie_rc_data = {
+     .max_regions    = 32,
+     .vendor_id      = PCI_VENDOR_ID_CDNS,
+     .device_id      = 0x0200,
+     .no_bar_nbits   = 32,
+};

Should (some of) these parameters be retrieved through a DT binding ?

Quoted 8 lines

Indeed, maybe we get max_regions and no_bar_nbits from the DT.

About the vendor and device IDs, I don't know which would be the best choice between some dedicated DT properties or associating a custom structure as above to the 'compatible' string.

Honestly, I don't have any strong preference, please just tell me what you would prefer :)

I think it is best to ask DT maintainers (in CC) POV on this, they certainly have a more comprehensive view than mine on the subject - I have just noticed that _some_ data can be retrieved through DT therefore I raised the point - either through different compatible strings or some IP specific properties.

Quoted 121 lines
Quoted 104 lines
Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+static const struct of_device_id cdns_pcie_host_of_match[] = {
+     { .compatible = "cdns,cdns-pcie-host",
+       .data = &cdns_pcie_rc_data },
+
+     { },
+};
+
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+                                              struct list_head *resources,
+                                              struct resource **bus_range)
+{
+     int err, res_valid = 0;
+     struct device_node *np = dev->of_node;
+     resource_size_t iobase;
+     struct resource_entry *win, *tmp;
+
+     err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+     if (err)
+             return err;
+
+     err = devm_request_pci_bus_resources(dev, resources);
+     if (err)
+             return err;
+
+     resource_list_for_each_entry_safe(win, tmp, resources) {
+             struct resource *res = win->res;
+
+             switch (resource_type(res)) {
+             case IORESOURCE_IO:
+                     err = pci_remap_iospace(res, iobase);
+                     if (err) {
+                             dev_warn(dev, "error %d: failed to map resource %pR\n",
+                                      err, res);
+                             resource_list_destroy_entry(win);
+                     }
+                     break;
+             case IORESOURCE_MEM:
+                     res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+                     break;
+             case IORESOURCE_BUS:

drivers/pci/cadence/pcie-cadence-host.c line 166

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+                     *bus_range = res;
+                     break;
+             }
+     }
+
+     if (res_valid)
+             return 0;
+
+     dev_err(dev, "non-prefetchable memory resource required\n");
+     return -EINVAL;

Nit, I prefer you swap these two as it is done in pci-aardvark.c:

        if (!res_valid) {
                dev_err(dev, "non-prefetchable memory resource required\n");
                return -EINVAL;
        }

        return 0;

but as per previous replies this function can be factorized in core PCI code - I would not bother unless you are willing to write the patch series that does the refactoring yourself :)

Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+}
+
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data = rc->data;
+     struct cdns_pcie *pcie = &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn = rc->bus_range->start;
+     sbn = pbn + 1; /* Single root port. */
+     subn = rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

I will test without those lines to test whether I can remove them.

At first, the PCIe controller was tested by Cadence team: there was code in their bootloader to initialize the hardware (building the AXI <-> PCIe mappings, ...): the bootloader used to set the primary, secondary and subordinate bus numbers in the root port PCI config space.

Quoted 7 lines

Also there was a hardware trick to redirect accesses of the lowest addresses in the AXI bus to the APB bus so the PCI configuration space of the root port could have been accessed from the AXI bus too.

The AXI <-> PCIe mapping being done by the bootloader and the root port config space being accessible from the AXI bus, it was possible to use the pci-host-generic driver.

That's what I was getting at. Ard (CC'ed) implemented a firmware set-up (even though it was for a different IP but maybe it applies here) that allows the kernel to use the pci-host-generic driver to initialize the PCI controller:

https://marc.info/?l=linux-pci&m=150360022626351&w=2

I want to understand if there is an IP initialization sequence whereby this IP can be made to work in an ECAM compliant way and therefore reuse (most of) the pci-host-generic driver code.

Quoted 3 lines

However, the hardware trick won't be included in the final design since Cadence now wants to perform all PCI configuration space accesses through a small 4KB window at a fixed address on the AXI bus.

I would like to understand what the HW "trick" (if you can disclose it) was, because if there is a chance to reuse the pci-host-generic driver for this IP I want to take it (yes it may entail some firmware set-up in the bootloader) - was it a HW trick or a specific IP SW configuration ?

Quoted 2 lines

Also, we now want all initialisations to be done by the linux driver instead of the bootloader.

That's a choice, I do not necessarily agree with it and I think we should aim for more standardization on the PCI host bridge set-up at firmware->kernel handover on DT platforms.

Quoted 3 lines

I simply moved all those initialisations from the bootloader to the linux driver but actually there is a chance that I can remove the 3 writes to the PCI_*_BUS registers.

I asked because I do not have this IP documentation so I rely on you to provide the correct initialization sequence and an explanation for it, I think I understand now the initialization sequence a bit more but it would be good to get to the bottom of it.

Thank you,
Lorenzo

#16 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

Quoted 31 lines · Lorenzo Pieralisi

[+Ard]

Hi Cyrille,

On Sun, Dec 03, 2017 at 09:44:46PM +0100, Cyrille Pitchen wrote:

[...]

Quoted 23 lines
Quoted 4 lines
Quoted 4 lines
drivers/pci/cadence/pcie-cadence-host.c new file +4 -0
+cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
+{
+     struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
+     struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 73

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+     struct cdns_pcie *pcie = &rc->pcie;
+     unsigned int busn = bus->number;
+     u32 addr0, desc0;
+
+     if (busn < rc->bus_range->start || busn > rc->bus_range->end)
+             return NULL;

It does not hurt but I wonder whether you really need this check.

I can remove it.

Quoted 6 lines

drivers/pci/cadence/pcie-cadence-host.c line 76

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     if (busn == rc->bus_range->start) {
+             if (devfn)

I suspect I know why you need this check but I ask you to explain it anyway if you do not mind please.

Quoted 9 lines
Quoted 6 lines

If I have understood correctly, Cadence team told me that only the root port is available on the first bus through device 0, function 0. No other device/function should connected on this bus, all other devices are behind at least one PCI bridge.

I can add a comment here to explain that.

That's understood, the question is what happens if you do scan devfn != 0.

OK, this is similar to the Synopsys IP. Type 0 config TLPs are not filtered by the hardware, and so if you don't filter them in software, the device downstream of the rootport will appear 32 times.

Quoted 227 lines
Quoted 13 lines
Quoted 12 lines

drivers/pci/cadence/pcie-cadence-host.c line 83

Quoted 7 lines
drivers/pci/cadence/pcie-cadence-host.c new file +7 -0
+                     return NULL;
+
+             return pcie->reg_base + (where & 0xfff);
+     }
+
+     /* Update Output registers for AXI region 0. */
+     addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) |

Ok, so for every config access you reprogram addr0 to reflect the correct bus|devfn ID in the PCI bus TLP corresponding to an ECAM address in CPU physical address space, is my understanding correct ?

Quoted 8 lines

The idea is to able to use only a 4KB memory area at a fixed address in the space allocated for the PCIe controller in the AXI bus. I guess the plan is to leave more space on the AXI bus to map all other PCIe devices.

This is just my guess. Anyway one purpose of this driver was actually to perform all PCI configuration space accesses through this single 4KB memory area in the AXI bus, changing the mapping dynamically to reach the relevant PCI device.

Thank you for explaining - that matches my understanding.

Quoted 54 lines
Quoted 16 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-host.c new file +5 -0
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) |
+             CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn);
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0);
+
+     /* Configuration Type 0 or Type 1 access. */

drivers/pci/cadence/pcie-cadence-host.c line 98

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+     desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
+             CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
+     /*
+      * The bus number was already set once for all in desc1 by
+      * cdns_pcie_host_init_address_translation().
+      */
+     if (busn == rc->bus_range->start + 1)
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0;
+     else
+             desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1;
Quoted 3 lines

I would like to ask you why you have to do it here and the root port does not figure it out by itself, I do not have the datasheet so I am just asking for my own information.

PCI configuration space registers of the root port can only be read through the APB bus at offset 0:
->reg_base + (where & 0xfff)

They are internal registers of the PCIe controller so no TLP on the PCIe bus.

However to access the PCI configuration space registers of any other device, the PCIe controller builds then sends a TLP on the PCIe bus using the offset in the 4KB AXI area as the offset of the register in the PCI configuration space:
->cfg_base + (where & 0xfff)

Quoted 12 lines
Quoted 12 lines
drivers/pci/cadence/pcie-cadence-host.c new file +11 -0
+     cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0);
+
+     return rc->cfg_base + (where & 0xfff);
+}
+
+static struct pci_ops cdns_pcie_host_ops = {
+     .map_bus        = cdns_pci_map_bus,
+     .read           = pci_generic_config_read,
+     .write          = pci_generic_config_write,
+};
+
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 115

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-host.c new file +6 -0
+static const struct cdns_pcie_rc_data cdns_pcie_rc_data = {
+     .max_regions    = 32,
+     .vendor_id      = PCI_VENDOR_ID_CDNS,
+     .device_id      = 0x0200,
+     .no_bar_nbits   = 32,
+};

Should (some of) these parameters be retrieved through a DT binding ?

Quoted 8 lines

Indeed, maybe we get max_regions and no_bar_nbits from the DT.

About the vendor and device IDs, I don't know which would be the best choice between some dedicated DT properties or associating a custom structure as above to the 'compatible' string.

Honestly, I don't have any strong preference, please just tell me what you would prefer :)

I think it is best to ask DT maintainers (in CC) POV on this, they certainly have a more comprehensive view than mine on the subject - I have just noticed that _some_ data can be retrieved through DT therefore I raised the point - either through different compatible strings or some IP specific properties.

Quoted 121 lines
Quoted 104 lines
Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+static const struct of_device_id cdns_pcie_host_of_match[] = {
+     { .compatible = "cdns,cdns-pcie-host",
+       .data = &cdns_pcie_rc_data },
+
+     { },
+};
+
+static int cdns_pcie_parse_request_of_pci_ranges(struct device *dev,
+                                              struct list_head *resources,
+                                              struct resource **bus_range)
+{
+     int err, res_valid = 0;
+     struct device_node *np = dev->of_node;
+     resource_size_t iobase;
+     struct resource_entry *win, *tmp;
+
+     err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
+     if (err)
+             return err;
+
+     err = devm_request_pci_bus_resources(dev, resources);
+     if (err)
+             return err;
+
+     resource_list_for_each_entry_safe(win, tmp, resources) {
+             struct resource *res = win->res;
+
+             switch (resource_type(res)) {
+             case IORESOURCE_IO:
+                     err = pci_remap_iospace(res, iobase);
+                     if (err) {
+                             dev_warn(dev, "error %d: failed to map resource %pR\n",
+                                      err, res);
+                             resource_list_destroy_entry(win);
+                     }
+                     break;
+             case IORESOURCE_MEM:
+                     res_valid |= !(res->flags & IORESOURCE_PREFETCH);
+                     break;
+             case IORESOURCE_BUS:

drivers/pci/cadence/pcie-cadence-host.c line 166

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-host.c new file +10 -0
+                     *bus_range = res;
+                     break;
+             }
+     }
+
+     if (res_valid)
+             return 0;
+
+     dev_err(dev, "non-prefetchable memory resource required\n");
+     return -EINVAL;

Nit, I prefer you swap these two as it is done in pci-aardvark.c:

        if (!res_valid) {
                dev_err(dev, "non-prefetchable memory resource required\n");
                return -EINVAL;
        }

        return 0;

but as per previous replies this function can be factorized in core PCI code - I would not bother unless you are willing to write the patch series that does the refactoring yourself :)

Quoted 40 lines
drivers/pci/cadence/pcie-cadence-host.c new file +40 -0
+}
+
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data = rc->data;
+     struct cdns_pcie *pcie = &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn = rc->bus_range->start;
+     sbn = pbn + 1; /* Single root port. */
+     subn = rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

I will test without those lines to test whether I can remove them.

At first, the PCIe controller was tested by Cadence team: there was code in their bootloader to initialize the hardware (building the AXI <-> PCIe mappings, ...): the bootloader used to set the primary, secondary and subordinate bus numbers in the root port PCI config space.

Quoted 7 lines

Also there was a hardware trick to redirect accesses of the lowest addresses in the AXI bus to the APB bus so the PCI configuration space of the root port could have been accessed from the AXI bus too.

The AXI <-> PCIe mapping being done by the bootloader and the root port config space being accessible from the AXI bus, it was possible to use the pci-host-generic driver.

That's what I was getting at. Ard (CC'ed) implemented a firmware set-up (even though it was for a different IP but maybe it applies here) that allows the kernel to use the pci-host-generic driver to initialize the PCI controller:

Quoted 6 lines

https://marc.info/?l=linux-pci&m=150360022626351&w=2

I want to understand if there is an IP initialization sequence whereby this IP can be made to work in an ECAM compliant way and therefore reuse (most of) the pci-host-generic driver code.

I think the Synopsys case is probably very similar. There are some registers that look like the config space of a root port, but in reality, every memory access that hits a live host bridge window is forwarded onto the link, regardless of the values of the bridge BARs. That is why in the quoted case, we can get away with ignoring the root port altogether, rather than jumping through hoops to make the IP block's PCI config space registers appear at B/D/F 0/0/0, while still having to filter type 0 config TLPs going onto the link (which is arguably the job of the root port to begin with)

So if this IP does implement a proper root port (i.e., one where the bridge BARs are actually taken into account, and where type 0 config TLPs are in fact filtered), I strongly recommend mapping its config space registers in an ECAM compliant matter, which implies no accessors in the OS.

However, given the observation above, this IP does not appear to filter type 0 config TLPs to devfn > 0 downstream of the root port either.

Quoted 9 lines
Quoted 3 lines

However, the hardware trick won't be included in the final design since Cadence now wants to perform all PCI configuration space accesses through a small 4KB window at a fixed address on the AXI bus.

I would like to understand what the HW "trick" (if you can disclose it) was, because if there is a chance to reuse the pci-host-generic driver for this IP I want to take it (yes it may entail some firmware set-up in the bootloader) - was it a HW trick or a specific IP SW configuration ?

Quoted 7 lines
Quoted 2 lines

Also, we now want all initialisations to be done by the linux driver instead of the bootloader.

That's a choice, I do not necessarily agree with it and I think we should aim for more standardization on the PCI host bridge set-up at firmware->kernel handover on DT platforms.

Well, for one, it means this IP will never be supported by ACPI, which seems like a huge downside to me.

Quoted 9 lines
Quoted 3 lines

I simply moved all those initialisations from the bootloader to the linux driver but actually there is a chance that I can remove the 3 writes to the PCI_*_BUS registers.

I asked because I do not have this IP documentation so I rely on you to provide the correct initialization sequence and an explanation for it, I think I understand now the initialization sequence a bit more but it would be good to get to the bottom of it.

#26 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Lorenzo Pieralisi

Hi,

Quoted 114 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
 	  mode. This PCIe controller may be embedded into many different vendors
 	  SoCs.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on PCI_ENDPOINT
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <cyrille.pitchen-wi1+55ScJUtKEb57/[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *

drivers/pci/cadence/pcie-cadence-ep.c line 26

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/pci-epc.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/sizes.h>
+#include <linux/delay.h>

Nit: alphabetical order.

drivers/pci/cadence/pcie-cadence-ep.c line 36

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +9 -0
+#include "pcie-cadence.h"
+
+#define CDNS_PCIE_EP_MIN_APERTURE		128	/* 128 bytes */
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE		0x1
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY	0x3
+
+/**
+ * struct cdns_pcie_ep_data - hardware specific data
+ * @max_regions: maximum nmuber of regions supported by hardware

s/nmuber/number

Quoted 24 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +24 -0
+ */
+struct cdns_pcie_ep_data {
+	size_t				max_regions;
+};
+
+/**
+ * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
+ * @pcie: Cadence PCIe controller
+ * @data: pointer to a 'struct cdns_pcie_data'
+ */
+struct cdns_pcie_ep {
+	struct cdns_pcie		pcie;
+	const struct cdns_pcie_ep_data	*data;
+	struct pci_epc			*epc;
+	unsigned long			ob_region_map;
+	phys_addr_t			*ob_addr;
+	phys_addr_t			irq_phys_addr;
+	void __iomem			*irq_cpu_addr;
+	u64				irq_pci_addr;
+	u8				irq_pending;
+};
+
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

hmm.. the endpoint core has to send the function number which right now it's not doing though it has the function number info in pci_epf.

Quoted 17 lines
Quoted 17 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+		u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
+			 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
+		cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
+	}
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
+			       hdr->subclass_code | hdr->baseclass_code << 8);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
+			       hdr->cache_line_size);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
+
+	return 0;
+}
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 90

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+
+static int cdns_pcie_ep_set_bar(struct pci_epc *epc, enum pci_barno bar,
+				dma_addr_t bar_phys, size_t size, int flags)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
+	u8 fn = 0;

Here too endpoint core should send the function number..

Quoted 35 lines
Quoted 7 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +7 -0
+	u64 sz;
+
+	/* BAR size is 2^(aperture + 7) */
+	sz = max_t(size_t, size, CDNS_PCIE_EP_MIN_APERTURE);
+	sz = 1ULL << fls64(sz - 1);
+	aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
+

drivers/pci/cadence/pcie-cadence-ep.c line 107

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
+		ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
+	} else {
+		bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
+		bool is_64bits = sz > SZ_2G;
+
+		if (is_64bits && (bar & 1))
+			return -EINVAL;
+
+		switch (is_64bits << 1 | is_prefetch) {

I would not mind implementing this as a nested if-else, I am not a big fan of using bool this way.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+		case 0:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
+			break;
+
+		case 1:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
+			break;
+
+		case 2:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
+			break;
+
+		case 3:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 129

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+			break;
+		}
+	}
+
+	addr0 = lower_32_bits(bar_phys);
+	addr1 = upper_32_bits(bar_phys);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
+			 addr0);

It would be nice if you can have defines for CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0 included in this patch rather than "PCI: cadence: Add host driver for Cadence PCIe controller". All EP specific functions in header file should be included here.

Quoted 21 lines

drivers/pci/cadence/pcie-cadence-ep.c line 131

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
+			 addr1);

Is fn always 0 ?

Quoted 16 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
+		CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	return 0;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 156

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+}
+
+static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, enum pci_barno bar)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 reg, cfg, b, ctrl;
+	u8 fn = 0;

Here too endpoint core should send the function number..

Quoted 80 lines
Quoted 18 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +18 -0
+
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(ctrl);
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);

drivers/pci/cadence/pcie-cadence-ep.c line 184

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static int cdns_pcie_ep_map_addr(struct pci_epc *epc, phys_addr_t addr,
+				 u64 pci_addr, size_t size)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	r = find_first_zero_bit(&ep->ob_region_map, sizeof(ep->ob_region_map));

Second argument must be in bits not bytes.

https://marc.info/?l=linux-pci&m=151179781225513&w=2

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+	if (r >= ep->data->max_regions - 1) {
+		dev_err(&epc->dev, "no free outbound region\n");
+		return -EINVAL;
+	}
+
+	cdns_pcie_set_outbound_region(pcie, r, false, addr, pci_addr, size);
+
+	set_bit(r, &ep->ob_region_map);
+	ep->ob_addr[r] = addr;
+
+	return 0;
+}
+
+static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, phys_addr_t addr)

drivers/pci/cadence/pcie-cadence-ep.c line 208

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	for (r = 0; r < ep->data->max_regions - 1; r++)
+		if (ep->ob_addr[r] == addr)
+			break;
+
+	if (r >= ep->data->max_regions - 1)

== ?

Quoted 20 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +20 -0
+		return;
+
+	cdns_pcie_reset_outbound_region(pcie, r);
+
+	ep->ob_addr[r] = 0;
+	clear_bit(r, &ep->ob_region_map);
+}
+
+static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 mmc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 236

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	/*
+	 * Set the Multiple Message Capable bitfield into the Message Control
+	 * register.
+	 */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
+	flags |= PCI_MSI_FLAGS_64BIT;
+	flags &= ~PCI_MSI_FLAGS_MASKBIT;

Any reason why "Per-vector masking capable" is reset?

Quoted 24 lines
Quoted 24 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +24 -0
+	cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_get_msi(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/* Validate that the MSI feature is actually enabled. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/*
+	 * Get the Multiple Message Enable bitfield from the Message Control
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 268

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	 * register.
+	 */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

I'm not sure if both these above checks are required..

Quoted 31 lines

You should comment on what this 5 means and why it is fine to cap mme.

Quoted 28 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +28 -0
+
+	return mme;
+}
+
+static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
+				     u8 intx, bool is_asserted)
+{
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r = ep->data->max_regions - 1;
+	u32 offset;
+	u16 status;
+	u8 msg_code;
+
+	intx &= 3;
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region_for_normal_msg(pcie, r,
+							     ep->irq_phys_addr);
+		ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
+	}
+
+	if (is_asserted) {
+		ep->irq_pending |= BIT(intx);
+		msg_code = MSG_CODE_ASSERT_INTA + intx;
+	} else {
+		ep->irq_pending &= ~BIT(intx);
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 304

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+		msg_code = MSG_CODE_DEASSERT_INTA + intx;
+	}
+
+	status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
+	if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
+		status ^= PCI_STATUS_INTERRUPT;
+		cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
+	}

here you are setting the PCI_STATUS_INTERRUPT even before sending the ASSERT message.

Quoted 34 lines
Quoted 7 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +7 -0
+
+	offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
+		 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
+		 CDNS_PCIE_MSG_NO_DATA;
+	writel(0, ep->irq_cpu_addr + offset);
+}
+

drivers/pci/cadence/pcie-cadence-ep.c line 321

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
+{
+	u16 cmd;
+
+	cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
+	if (cmd & PCI_COMMAND_INTX_DISABLE)
+		return -EINVAL;
+
+	cdns_pcie_ep_assert_intx(ep, fn, intx, true);
+	mdelay(1);

Add a comment please to explain the mdelay value.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+	cdns_pcie_ep_assert_intx(ep, fn, intx, false);
+	return 0;
+}
+
+static int cdns_pcie_ep_raise_irq(struct pci_epc *epc,
+				  enum pci_epc_irq_type type, u8 interrupt_num)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme, data, data_mask;
+	u8 msi_count;
+	u64 pci_addr, pci_addr_mask = 0xff;
+	u8 fn = 0;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 343

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+
+	/* Handle legacy IRQ. */
+	if (type == PCI_EPC_IRQ_LEGACY)
+		return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
+
+	/* Otherwise MSI. */
+	if (type != PCI_EPC_IRQ_MSI)
+		return -EINVAL;

MSI-X?

Quoted 55 lines

drivers/pci/cadence/pcie-cadence-ep.c line 356

Quoted 13 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +13 -0
+
+	/* Check whether the MSI feature has been enabled by the PCI host. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/* Get the number of enabled MSIs */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

Same comment as above.

Quoted 39 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +39 -0
+	msi_count = 1 << mme;
+	if (!interrupt_num || interrupt_num > msi_count)
+		return -EINVAL;
+
+	/* Compute the data value to be written. */
+	data_mask = msi_count - 1;
+	data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
+	data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
+
+	/* Get the PCI address where to write the data into. */
+	pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
+	pci_addr <<= 32;
+	pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
+	pci_addr &= GENMASK_ULL(63, 2);
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != pci_addr)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region(pcie, ep->data->max_regions - 1,
+					      false,
+					      ep->irq_phys_addr,
+					      pci_addr & ~pci_addr_mask,
+					      pci_addr_mask + 1);
+		ep->irq_pci_addr = pci_addr;
+	}
+	writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
+
+	return 0;
+}
+
+static int cdns_pcie_ep_start(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	struct pci_epf *epf;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Enable this endpoint function. */
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 404

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg |= BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+
+	/*
+	 * Already linked-up: don't call directly pci_epc_linkup() because we've
+	 * already locked the epc->lock.
+	 */

Not sure what you mean by linked-up here?

Quoted 100 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +4 -0
+	list_for_each_entry(epf, &epc->pci_epf, list)
+		pci_epf_linkup(epf);
 
+
+	return 0;

drivers/pci/cadence/pcie-cadence-ep.c line 418

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static void cdns_pcie_ep_stop(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Disable this endpoint function (function 0 can't be disabled). */

I do not understand this comment and how it applies to the code, in other words fn is always 0 here (so it can't be disabled) I do not understand what this code is there for.

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +11 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg &= ~BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+}
+
+static const struct pci_epc_ops cdns_pcie_epc_ops = {
+	.write_header	= cdns_pcie_ep_write_header,
+	.set_bar	= cdns_pcie_ep_set_bar,
+	.clear_bar	= cdns_pcie_ep_clear_bar,
+	.map_addr	= cdns_pcie_ep_map_addr,
+	.unmap_addr	= cdns_pcie_ep_unmap_addr,

drivers/pci/cadence/pcie-cadence-ep.c line 439

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	.set_msi	= cdns_pcie_ep_set_msi,
+	.get_msi	= cdns_pcie_ep_get_msi,
+	.raise_irq	= cdns_pcie_ep_raise_irq,
+	.start		= cdns_pcie_ep_start,
+	.stop		= cdns_pcie_ep_stop,
+};
+
+static const struct cdns_pcie_ep_data cdns_pcie_ep_data = {
+	.max_regions	= 16,
+};

As I mentioned in patch 3, should this be set-up with DT ?

Thanks,
Lorenzo

Quoted 53 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +53 -0
+
+static const struct of_device_id cdns_pcie_ep_of_match[] = {
+	{ .compatible = "cdns,cdns-pcie-ep",
+	  .data = &cdns_pcie_ep_data },
+
+	{ },
+};
+
+static int cdns_pcie_ep_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	const struct of_device_id *of_id;
+	struct cdns_pcie_ep *ep;
+	struct cdns_pcie *pcie;
+	struct pci_epc *epc;
+	struct resource *res;
+	size_t max_regions;
+	int ret;
+
+	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
+	if (!ep)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, ep);
+
+	pcie = &ep->pcie;
+	pcie->is_rc = false;
+
+	of_id = of_match_node(cdns_pcie_ep_of_match, np);
+	ep->data = (const struct cdns_pcie_ep_data *)of_id->data;
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+	pcie->reg_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(pcie->reg_base)) {
+		dev_err(dev, "missing \"reg\"\n");
+		return PTR_ERR(pcie->reg_base);
+	}
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+	if (!res) {
+		dev_err(dev, "missing \"mem\"\n");
+		return -EINVAL;
+	}
+	pcie->mem_res = res;
+
+	max_regions = ep->data->max_regions;
+	ep->ob_addr = devm_kzalloc(dev, max_regions * sizeof(*ep->ob_addr),
+				   GFP_KERNEL);
+	if (!ep->ob_addr)
+		return -ENOMEM;
+
+	pm_runtime_enable(dev);
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 500

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		dev_err(dev, "pm_runtime_get_sync() failed\n");
+		goto err_get_sync;
+	}
+
+	/* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));

why disable all functions?

Quoted 16 lines
Quoted 16 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+
+	epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
+	if (IS_ERR(epc)) {
+		dev_err(dev, "failed to create epc device\n");
+		ret = PTR_ERR(epc);
+		goto err_init;
+	}
+
+	ep->epc = epc;
+	epc_set_drvdata(epc, ep);
+
+	ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
+	if (ret < 0)
+		epc->max_functions = 1;
+
+	ret = pci_epc_mem_init(epc, pcie->mem_res->start,
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 524

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+			       resource_size(pcie->mem_res));
+	if (ret < 0) {
+		dev_err(dev, "failed to initialize the memory space\n");
+		goto err_init;
+	}
+
+	ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
+						  SZ_128K);

Any reason why you chose SZ_128K?

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Ard Biesheuvel

On Mon, Dec 04, 2017 at 06:49:12PM +0000, Ard Biesheuvel wrote:

[...]

Quoted 91 lines
Quoted 68 lines
Quoted 55 lines
Quoted 38 lines
Quoted 38 lines
drivers/pci/cadence/pcie-cadence-host.c new file +38 -0
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data = rc->data;
+     struct cdns_pcie *pcie = &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn = rc->bus_range->start;
+     sbn = pbn + 1; /* Single root port. */
+     subn = rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

I will test without those lines to test whether I can remove them.

At first, the PCIe controller was tested by Cadence team: there was code in their bootloader to initialize the hardware (building the AXI <-> PCIe mappings, ...): the bootloader used to set the primary, secondary and subordinate bus numbers in the root port PCI config space.

Quoted 7 lines

Also there was a hardware trick to redirect accesses of the lowest addresses in the AXI bus to the APB bus so the PCI configuration space of the root port could have been accessed from the AXI bus too.

The AXI <-> PCIe mapping being done by the bootloader and the root port config space being accessible from the AXI bus, it was possible to use the pci-host-generic driver.

That's what I was getting at. Ard (CC'ed) implemented a firmware set-up (even though it was for a different IP but maybe it applies here) that allows the kernel to use the pci-host-generic driver to initialize the PCI controller:

Quoted 6 lines

https://marc.info/?l=linux-pci&m=150360022626351&w=2

I want to understand if there is an IP initialization sequence whereby this IP can be made to work in an ECAM compliant way and therefore reuse (most of) the pci-host-generic driver code.

I think the Synopsys case is probably very similar. There are some registers that look like the config space of a root port, but in reality, every memory access that hits a live host bridge window is forwarded onto the link, regardless of the values of the bridge BARs. That is why in the quoted case, we can get away with ignoring the root port altogether, rather than jumping through hoops to make the IP block's PCI config space registers appear at B/D/F 0/0/0, while still having to filter type 0 config TLPs going onto the link (which is arguably the job of the root port to begin with)

So if this IP does implement a proper root port (i.e., one where the bridge BARs are actually taken into account, and where type 0 config TLPs are in fact filtered), I strongly recommend mapping its config space registers in an ECAM compliant matter, which implies no accessors in the OS.

Quoted 3 lines

However, given the observation above, this IP does not appear to filter type 0 config TLPs to devfn > 0 downstream of the root port either.

Unfortunately that matches my understanding too, let's wait for Cyrille's reply on my query.

Quoted 12 lines
Quoted 12 lines
Quoted 3 lines

However, the hardware trick won't be included in the final design since Cadence now wants to perform all PCI configuration space accesses through a small 4KB window at a fixed address on the AXI bus.

I would like to understand what the HW "trick" (if you can disclose it) was, because if there is a chance to reuse the pci-host-generic driver for this IP I want to take it (yes it may entail some firmware set-up in the bootloader) - was it a HW trick or a specific IP SW configuration ?

Quoted 2 lines

Also, we now want all initialisations to be done by the linux driver instead of the bootloader.

Quoted 7 lines
Quoted 4 lines

That's a choice, I do not necessarily agree with it and I think we should aim for more standardization on the PCI host bridge set-up at firmware->kernel handover on DT platforms.

Well, for one, it means this IP will never be supported by ACPI, which seems like a huge downside to me.

Yes it is - that's exactly where my comments were heading.

Thanks,
Lorenzo
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] PCI: Add vendor ID for Cadence

In reply to Cyrille Pitchen
Quoted 3 lines · Cyrille Pitchen

This patch adds a new PCI vendor ID for Cadence.

I assume Lorenzo will merge this along with the rest of the Cadence support.

Quoted 20 lines
---
 include/linux/pci_ids.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/pci_ids.h +2 -0
index ab20dc5db423..eb13e84e1fef 100644
@@ -2381,6 +2381,8 @@
 
 #define PCI_VENDOR_ID_LENOVO		0x17aa
 
+#define PCI_VENDOR_ID_CDNS		0x17cd
+
 #define PCI_VENDOR_ID_ARECA		0x17d3
 #define PCI_DEVICE_ID_ARECA_1110	0x1110
 #define PCI_DEVICE_ID_ARECA_1120	0x1120
Signature · 1 line
2.11.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#27 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Kishon Vijay Abraham I

Cyrille,

Quoted 71 lines · Kishon Vijay Abraham I

Hi,

Quoted 68 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
       mode. This PCIe controller may be embedded into many different vendors
       SoCs.
 
+config PCIE_CADENCE_EP
+    bool "Cadence PCIe endpoint controller"
+    depends on PCI_ENDPOINT
+    select PCIE_CADENCE
+    help
+      Say Y here if you want to support the Cadence PCIe  controller in
+      endpoint mode. This PCIe controller may be embedded into many
+      different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 17 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +17 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */

Would you consider using the new SPDX ids instead of this long legalese blurb? You can check tglx's doc patches for details and Linus comments on why he wants it a certain way with C++ // comments.

Here this could come out much streamlined with this top level comment block:

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2017 Cadence
// Cadence PCIe host controller driver.
// Author: Cyrille Pitchen <[email protected]>

This is only a suggestion, but everyone kinda likes it when the licensing boilerplate is minimal and out of the visual way in order to focus is on what matters most: the code!
Thank you for your kind consideration.

Signature · 2 lines
Cordially
Philippe Ombredanne
#28 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

Hi Philippe,

Le 07/12/2017 à 11:05, Philippe Ombredanne a écrit :

Quoted 87 lines

Cyrille,

Quoted 71 lines · Kishon Vijay Abraham I

Hi,

Quoted 68 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
       mode. This PCIe controller may be embedded into many different vendors
       SoCs.
 
+config PCIE_CADENCE_EP
+    bool "Cadence PCIe endpoint controller"
+    depends on PCI_ENDPOINT
+    select PCIE_CADENCE
+    help
+      Say Y here if you want to support the Cadence PCIe  controller in
+      endpoint mode. This PCIe controller may be embedded into many
+      different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 17 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +17 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <cyrille.pitchen-wi1+55ScJUtKEb57/[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */

Would you consider using the new SPDX ids instead of this long legalese blurb? You can check tglx's doc patches for details and Linus comments on why he wants it a certain way with C++ // comments.

Here this could come out much streamlined with this top level comment block:

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2017 Cadence
// Cadence PCIe host controller driver.
// Author: Cyrille Pitchen <cyrille.pitchen-wi1+55ScJUtKEb57/[email protected]>

Quoted 5 lines

This is only a suggestion, but everyone kinda likes it when the licensing boilerplate is minimal and out of the visual way in order to focus is on what matters most: the code!
Thank you for your kind consideration.

I don't mind changing for your proposal if this is the preferred way. I'm updating the series.

Best regards,

Cyrille

Signature · 7 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#18 lore raw

Re: [PATCH 3/5] PCI: cadence: Add host driver for Cadence PCIe controller

In reply to Lorenzo Pieralisi

Hi all,

Quoted 95 lines · Lorenzo Pieralisi

On Mon, Dec 04, 2017 at 06:49:12PM +0000, Ard Biesheuvel wrote:

[...]

Quoted 91 lines
Quoted 68 lines
Quoted 55 lines
Quoted 38 lines
Quoted 38 lines
drivers/pci/cadence/pcie-cadence-host.c new file +38 -0
+static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc)
+{
+     const struct cdns_pcie_rc_data *data = rc->data;
+     struct cdns_pcie *pcie = &rc->pcie;
+     u8 pbn, sbn, subn;
+     u32 value, ctrl;
+
+     /*
+      * Set the root complex BAR configuration register:
+      * - disable both BAR0 and BAR1.
+      * - enable Prefetchable Memory Base and Limit registers in type 1
+      *   config space (64 bits).
+      * - enable IO Base and Limit registers in type 1 config
+      *   space (32 bits).
+      */
+     ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+     value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE |
+             CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS;
+     cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value);
+
+     /* Set root port configuration space */
+     if (data->vendor_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, data->vendor_id);
+     if (data->device_id != 0xffff)
+             cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, data->device_id);
+
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0);
+     cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0);
+     cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
+
+     pbn = rc->bus_range->start;
+     sbn = pbn + 1; /* Single root port. */
+     subn = rc->bus_range->end;
+     cdns_pcie_rp_writeb(pcie, PCI_PRIMARY_BUS, pbn);
Quoted 9 lines

drivers/pci/cadence/pcie-cadence-host.c line 208

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-host.c new file +2 -0
+     cdns_pcie_rp_writeb(pcie, PCI_SECONDARY_BUS, sbn);
+     cdns_pcie_rp_writeb(pcie, PCI_SUBORDINATE_BUS, subn);

Again - I do not have the datasheet for this device therefore I would kindly ask you how this works; it seems to me that what you are doing here is done through normal configuration cycles in an ECAM compliant system to program the RP PRIMARY/SECONDARY/SUBORDINATE bus - I would like to understand why this code is needed.

I will test without those lines to test whether I can remove them.

At first, the PCIe controller was tested by Cadence team: there was code in their bootloader to initialize the hardware (building the AXI <-> PCIe mappings, ...): the bootloader used to set the primary, secondary and subordinate bus numbers in the root port PCI config space.

Quoted 7 lines

Also there was a hardware trick to redirect accesses of the lowest addresses in the AXI bus to the APB bus so the PCI configuration space of the root port could have been accessed from the AXI bus too.

The AXI <-> PCIe mapping being done by the bootloader and the root port config space being accessible from the AXI bus, it was possible to use the pci-host-generic driver.

That's what I was getting at. Ard (CC'ed) implemented a firmware set-up (even though it was for a different IP but maybe it applies here) that allows the kernel to use the pci-host-generic driver to initialize the PCI controller:

Quoted 6 lines

https://marc.info/?l=linux-pci&m=150360022626351&w=2

I want to understand if there is an IP initialization sequence whereby this IP can be made to work in an ECAM compliant way and therefore reuse (most of) the pci-host-generic driver code.

I think the Synopsys case is probably very similar. There are some registers that look like the config space of a root port, but in reality, every memory access that hits a live host bridge window is forwarded onto the link, regardless of the values of the bridge BARs. That is why in the quoted case, we can get away with ignoring the root port altogether, rather than jumping through hoops to make the IP block's PCI config space registers appear at B/D/F 0/0/0, while still having to filter type 0 config TLPs going onto the link (which is arguably the job of the root port to begin with)

So if this IP does implement a proper root port (i.e., one where the bridge BARs are actually taken into account, and where type 0 config TLPs are in fact filtered), I strongly recommend mapping its config space registers in an ECAM compliant matter, which implies no accessors in the OS.

Quoted 16 lines
Quoted 3 lines

However, given the observation above, this IP does not appear to filter type 0 config TLPs to devfn > 0 downstream of the root port either.

Unfortunately that matches my understanding too, let's wait for Cyrille's reply on my query.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

However, the hardware trick won't be included in the final design since Cadence now wants to perform all PCI configuration space accesses through a small 4KB window at a fixed address on the AXI bus.

I would like to understand what the HW "trick" (if you can disclose it) was, because if there is a chance to reuse the pci-host-generic driver for this IP I want to take it (yes it may entail some firmware set-up in the bootloader) - was it a HW trick or a specific IP SW configuration ?

I will have to ask for details to Cadence designers if needed but when I asked them about it, they explained me that AXI bus accesses in a small window (I guess 4KB width) were redirected to the APB bus where lay the registers for the root port PCI configuration space.

I was some hardware trick which won't be included in the final design, so we can't enable or disable it by software.

Actually, this is requirement from the Cadence's customer that the host driver can access the PCI config space of any device in sub ordinates buses through a small memory area on the AXI bus. For some reason, they don't want an ECAM compliant controller.

I don't know the reason but my guess is that they don't want to waste to much space allocated to the PCIe controller on the AXI bus, likely on a 32-bit platform. As I said, this is such an assumption.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Also, we now want all initialisations to be done by the linux driver instead of the bootloader.

That's a choice, I do not necessarily agree with it and I think we should aim for more standardization on the PCI host bridge set-up at firmware->kernel handover on DT platforms.

It was another requirement of Cadence's customer that the PCIe host controller initialization is done by the Linux driver rather than by some boot loader.

Best regards,

Cyrille

Quoted 9 lines
Quoted 3 lines

Well, for one, it means this IP will never be supported by ACPI, which seems like a huge downside to me.

Yes it is - that's exactly where my comments were heading.

Thanks,
Lorenzo

Signature · 3 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#29 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

In reply to Kishon Vijay Abraham I

Hi Kishon,

Le 05/12/2017 à 10:19, Kishon Vijay Abraham I a écrit :

Quoted 126 lines

Hi,

Quoted 114 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 40 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c
drivers/pci/cadence/Kconfig +9 -0
index 120306cae2aa..b2e6af71f39e 100644
@@ -21,4 +21,13 @@ config PCIE_CADENCE_HOST
 	  mode. This PCIe controller may be embedded into many different vendors
 	  SoCs.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on PCI_ENDPOINT
+	select PCIE_CADENCE
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endif # PCI_CADENCE
drivers/pci/cadence/Makefile +1 -0
index d57d192d2595..61e9c8d6839d 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_PCIE_CADENCE) += pcie-cadence.o
 obj-$(CONFIG_PCIE_CADENCE_HOST) += pcie-cadence-host.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci/cadence/pcie-cadence-ep.c new file +0 -0
new file mode 100644
index 000000000000..a1d761101a9c

drivers/pci/cadence/pcie-cadence-ep.c line 2

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
@@ -0,0 +1,553 @@
+/*
+ * Cadence PCIe host controller driver.

You should update this comment.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+ *
+ * Copyright (c) 2017 Cadence
+ *
+ * Author: Cyrille Pitchen <cyrille.pitchen-wi1+55ScJUtKEb57/[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *

drivers/pci/cadence/pcie-cadence-ep.c line 26

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/pci-epc.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/sizes.h>
+#include <linux/delay.h>

Nit: alphabetical order.

drivers/pci/cadence/pcie-cadence-ep.c line 36

Quoted 9 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +9 -0
+#include "pcie-cadence.h"
+
+#define CDNS_PCIE_EP_MIN_APERTURE		128	/* 128 bytes */
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE		0x1
+#define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY	0x3
+
+/**
+ * struct cdns_pcie_ep_data - hardware specific data
+ * @max_regions: maximum nmuber of regions supported by hardware

s/nmuber/number

Quoted 24 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +24 -0
+ */
+struct cdns_pcie_ep_data {
+	size_t				max_regions;
+};
+
+/**
+ * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
+ * @pcie: Cadence PCIe controller
+ * @data: pointer to a 'struct cdns_pcie_data'
+ */
+struct cdns_pcie_ep {
+	struct cdns_pcie		pcie;
+	const struct cdns_pcie_ep_data	*data;
+	struct pci_epc			*epc;
+	unsigned long			ob_region_map;
+	phys_addr_t			*ob_addr;
+	phys_addr_t			irq_phys_addr;
+	void __iomem			*irq_cpu_addr;
+	u64				irq_pci_addr;
+	u8				irq_pending;
+};
+
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 6 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +6 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

Quoted 2 lines

hmm.. the endpoint core has to send the function number which right now it's not doing though it has the function number info in pci_epf.

Would it be OK if I add a new patch in the next series adding a 'struct pcie_epf *epf' as a 2nd argument to all handlers in the 'struct pcie_epc_ops'? This way I could have access to epf->func_no as needed.

Best regards,

Cyrille

Quoted 520 lines
Quoted 17 lines
Quoted 17 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+		u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
+			 CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
+		cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
+	}
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
+			       hdr->subclass_code | hdr->baseclass_code << 8);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
+			       hdr->cache_line_size);
+	cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
+	cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
+
+	return 0;
+}
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 90

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+
+static int cdns_pcie_ep_set_bar(struct pci_epc *epc, enum pci_barno bar,
+				dma_addr_t bar_phys, size_t size, int flags)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
+	u8 fn = 0;

Here too endpoint core should send the function number..

Quoted 35 lines
Quoted 7 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +7 -0
+	u64 sz;
+
+	/* BAR size is 2^(aperture + 7) */
+	sz = max_t(size_t, size, CDNS_PCIE_EP_MIN_APERTURE);
+	sz = 1ULL << fls64(sz - 1);
+	aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
+

drivers/pci/cadence/pcie-cadence-ep.c line 107

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
+		ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
+	} else {
+		bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
+		bool is_64bits = sz > SZ_2G;
+
+		if (is_64bits && (bar & 1))
+			return -EINVAL;
+
+		switch (is_64bits << 1 | is_prefetch) {

I would not mind implementing this as a nested if-else, I am not a big fan of using bool this way.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+		case 0:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
+			break;
+
+		case 1:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
+			break;
+
+		case 2:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
+			break;
+
+		case 3:
+			ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 129

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+			break;
+		}
+	}
+
+	addr0 = lower_32_bits(bar_phys);
+	addr1 = upper_32_bits(bar_phys);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
+			 addr0);

It would be nice if you can have defines for CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0 included in this patch rather than "PCI: cadence: Add host driver for Cadence PCIe controller". All EP specific functions in header file should be included here.

Quoted 21 lines

drivers/pci/cadence/pcie-cadence-ep.c line 131

Quoted 2 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +2 -0
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
+			 addr1);

Is fn always 0 ?

Quoted 16 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
+		CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	return 0;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 156

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+}
+
+static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, enum pci_barno bar)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 reg, cfg, b, ctrl;
+	u8 fn = 0;

Here too endpoint core should send the function number..

Quoted 80 lines
Quoted 18 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +18 -0
+
+	if (bar < BAR_4) {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
+		b = bar;
+	} else {
+		reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
+		b = bar - BAR_4;
+	}
+
+	ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
+	cfg = cdns_pcie_readl(pcie, reg);
+	cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
+		 CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
+	cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(ctrl);
+	cdns_pcie_writel(pcie, reg, cfg);
+
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
+	cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);

drivers/pci/cadence/pcie-cadence-ep.c line 184

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static int cdns_pcie_ep_map_addr(struct pci_epc *epc, phys_addr_t addr,
+				 u64 pci_addr, size_t size)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	r = find_first_zero_bit(&ep->ob_region_map, sizeof(ep->ob_region_map));

Second argument must be in bits not bytes.

https://marc.info/?l=linux-pci&m=151179781225513&w=2

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+	if (r >= ep->data->max_regions - 1) {
+		dev_err(&epc->dev, "no free outbound region\n");
+		return -EINVAL;
+	}
+
+	cdns_pcie_set_outbound_region(pcie, r, false, addr, pci_addr, size);
+
+	set_bit(r, &ep->ob_region_map);
+	ep->ob_addr[r] = addr;
+
+	return 0;
+}
+
+static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, phys_addr_t addr)

drivers/pci/cadence/pcie-cadence-ep.c line 208

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r;
+
+	for (r = 0; r < ep->data->max_regions - 1; r++)
+		if (ep->ob_addr[r] == addr)
+			break;
+
+	if (r >= ep->data->max_regions - 1)

== ?

Quoted 20 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +20 -0
+		return;
+
+	cdns_pcie_reset_outbound_region(pcie, r);
+
+	ep->ob_addr[r] = 0;
+	clear_bit(r, &ep->ob_region_map);
+}
+
+static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 mmc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 236

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	/*
+	 * Set the Multiple Message Capable bitfield into the Message Control
+	 * register.
+	 */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
+	flags |= PCI_MSI_FLAGS_64BIT;
+	flags &= ~PCI_MSI_FLAGS_MASKBIT;

Any reason why "Per-vector masking capable" is reset?

Quoted 24 lines
Quoted 24 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +24 -0
+	cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
+
+	return 0;
+}
+
+static int cdns_pcie_ep_get_msi(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme;
+	u8 fn = 0;
+
+	/* Validate the ID of the MSI Capability structure. */
+	if (cdns_pcie_ep_fn_readb(pcie, fn, cap) != PCI_CAP_ID_MSI)
+		return -EINVAL;
+
+	/* Validate that the MSI feature is actually enabled. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/*
+	 * Get the Multiple Message Enable bitfield from the Message Control
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 268

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	 * register.
+	 */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

I'm not sure if both these above checks are required..

Quoted 31 lines

You should comment on what this 5 means and why it is fine to cap mme.

Quoted 28 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +28 -0
+
+	return mme;
+}
+
+static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
+				     u8 intx, bool is_asserted)
+{
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 r = ep->data->max_regions - 1;
+	u32 offset;
+	u16 status;
+	u8 msg_code;
+
+	intx &= 3;
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region_for_normal_msg(pcie, r,
+							     ep->irq_phys_addr);
+		ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
+	}
+
+	if (is_asserted) {
+		ep->irq_pending |= BIT(intx);
+		msg_code = MSG_CODE_ASSERT_INTA + intx;
+	} else {
+		ep->irq_pending &= ~BIT(intx);
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 304

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+		msg_code = MSG_CODE_DEASSERT_INTA + intx;
+	}
+
+	status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
+	if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
+		status ^= PCI_STATUS_INTERRUPT;
+		cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
+	}

here you are setting the PCI_STATUS_INTERRUPT even before sending the ASSERT message.

Quoted 34 lines
Quoted 7 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +7 -0
+
+	offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
+		 CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
+		 CDNS_PCIE_MSG_NO_DATA;
+	writel(0, ep->irq_cpu_addr + offset);
+}
+

drivers/pci/cadence/pcie-cadence-ep.c line 321

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
+{
+	u16 cmd;
+
+	cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
+	if (cmd & PCI_COMMAND_INTX_DISABLE)
+		return -EINVAL;
+
+	cdns_pcie_ep_assert_intx(ep, fn, intx, true);
+	mdelay(1);

Add a comment please to explain the mdelay value.

Quoted 14 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +14 -0
+	cdns_pcie_ep_assert_intx(ep, fn, intx, false);
+	return 0;
+}
+
+static int cdns_pcie_ep_raise_irq(struct pci_epc *epc,
+				  enum pci_epc_irq_type type, u8 interrupt_num)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
+	u16 flags, mmc, mme, data, data_mask;
+	u8 msi_count;
+	u64 pci_addr, pci_addr_mask = 0xff;
+	u8 fn = 0;
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 343

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+
+	/* Handle legacy IRQ. */
+	if (type == PCI_EPC_IRQ_LEGACY)
+		return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
+
+	/* Otherwise MSI. */
+	if (type != PCI_EPC_IRQ_MSI)
+		return -EINVAL;

MSI-X?

Quoted 55 lines

drivers/pci/cadence/pcie-cadence-ep.c line 356

Quoted 13 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +13 -0
+
+	/* Check whether the MSI feature has been enabled by the PCI host. */
+	flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
+	if (!(flags & PCI_MSI_FLAGS_ENABLE))
+		return -EINVAL;
+
+	/* Get the number of enabled MSIs */
+	mmc = (flags & PCI_MSI_FLAGS_QMASK) >> 1;
+	mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
+	if (mme > mmc)
+		mme = mmc;
+	if (mme > 5)
+		mme = 5;

Same comment as above.

Quoted 39 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +39 -0
+	msi_count = 1 << mme;
+	if (!interrupt_num || interrupt_num > msi_count)
+		return -EINVAL;
+
+	/* Compute the data value to be written. */
+	data_mask = msi_count - 1;
+	data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
+	data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
+
+	/* Get the PCI address where to write the data into. */
+	pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
+	pci_addr <<= 32;
+	pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
+	pci_addr &= GENMASK_ULL(63, 2);
+
+	/* Set the outbound region if needed. */
+	if (unlikely(ep->irq_pci_addr != pci_addr)) {
+		/* Last region was reserved for IRQ writes. */
+		cdns_pcie_set_outbound_region(pcie, ep->data->max_regions - 1,
+					      false,
+					      ep->irq_phys_addr,
+					      pci_addr & ~pci_addr_mask,
+					      pci_addr_mask + 1);
+		ep->irq_pci_addr = pci_addr;
+	}
+	writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
+
+	return 0;
+}
+
+static int cdns_pcie_ep_start(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	struct pci_epf *epf;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Enable this endpoint function. */
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 404

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg |= BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+
+	/*
+	 * Already linked-up: don't call directly pci_epc_linkup() because we've
+	 * already locked the epc->lock.
+	 */

Not sure what you mean by linked-up here?

Quoted 100 lines
Quoted 5 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +4 -0
+	list_for_each_entry(epf, &epc->pci_epf, list)
+		pci_epf_linkup(epf);
 
+
+	return 0;

drivers/pci/cadence/pcie-cadence-ep.c line 418

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+}
+
+static void cdns_pcie_ep_stop(struct pci_epc *epc)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u32 cfg;
+	u8 fn = 0;
+
+	/* Disable this endpoint function (function 0 can't be disabled). */

I do not understand this comment and how it applies to the code, in other words fn is always 0 here (so it can't be disabled) I do not understand what this code is there for.

Quoted 11 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +11 -0
+	cfg = cdns_pcie_readl(pcie, CDNS_PCIE_LM_EP_FUNC_CFG);
+	cfg &= ~BIT(fn);
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
+}
+
+static const struct pci_epc_ops cdns_pcie_epc_ops = {
+	.write_header	= cdns_pcie_ep_write_header,
+	.set_bar	= cdns_pcie_ep_set_bar,
+	.clear_bar	= cdns_pcie_ep_clear_bar,
+	.map_addr	= cdns_pcie_ep_map_addr,
+	.unmap_addr	= cdns_pcie_ep_unmap_addr,

drivers/pci/cadence/pcie-cadence-ep.c line 439

Quoted 10 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +10 -0
+	.set_msi	= cdns_pcie_ep_set_msi,
+	.get_msi	= cdns_pcie_ep_get_msi,
+	.raise_irq	= cdns_pcie_ep_raise_irq,
+	.start		= cdns_pcie_ep_start,
+	.stop		= cdns_pcie_ep_stop,
+};
+
+static const struct cdns_pcie_ep_data cdns_pcie_ep_data = {
+	.max_regions	= 16,
+};

As I mentioned in patch 3, should this be set-up with DT ?

Thanks,
Lorenzo

Quoted 53 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +53 -0
+
+static const struct of_device_id cdns_pcie_ep_of_match[] = {
+	{ .compatible = "cdns,cdns-pcie-ep",
+	  .data = &cdns_pcie_ep_data },
+
+	{ },
+};
+
+static int cdns_pcie_ep_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	const struct of_device_id *of_id;
+	struct cdns_pcie_ep *ep;
+	struct cdns_pcie *pcie;
+	struct pci_epc *epc;
+	struct resource *res;
+	size_t max_regions;
+	int ret;
+
+	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
+	if (!ep)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, ep);
+
+	pcie = &ep->pcie;
+	pcie->is_rc = false;
+
+	of_id = of_match_node(cdns_pcie_ep_of_match, np);
+	ep->data = (const struct cdns_pcie_ep_data *)of_id->data;
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
+	pcie->reg_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(pcie->reg_base)) {
+		dev_err(dev, "missing \"reg\"\n");
+		return PTR_ERR(pcie->reg_base);
+	}
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
+	if (!res) {
+		dev_err(dev, "missing \"mem\"\n");
+		return -EINVAL;
+	}
+	pcie->mem_res = res;
+
+	max_regions = ep->data->max_regions;
+	ep->ob_addr = devm_kzalloc(dev, max_regions * sizeof(*ep->ob_addr),
+				   GFP_KERNEL);
+	if (!ep->ob_addr)
+		return -ENOMEM;
+
+	pm_runtime_enable(dev);
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 500

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		dev_err(dev, "pm_runtime_get_sync() failed\n");
+		goto err_get_sync;
+	}
+
+	/* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
+	cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));

why disable all functions?

Quoted 16 lines
Quoted 16 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +16 -0
+
+	epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
+	if (IS_ERR(epc)) {
+		dev_err(dev, "failed to create epc device\n");
+		ret = PTR_ERR(epc);
+		goto err_init;
+	}
+
+	ep->epc = epc;
+	epc_set_drvdata(epc, ep);
+
+	ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
+	if (ret < 0)
+		epc->max_functions = 1;
+
+	ret = pci_epc_mem_init(epc, pcie->mem_res->start,
Quoted 8 lines

drivers/pci/cadence/pcie-cadence-ep.c line 524

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+			       resource_size(pcie->mem_res));
+	if (ret < 0) {
+		dev_err(dev, "failed to initialize the memory space\n");
+		goto err_init;
+	}
+
+	ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
+						  SZ_128K);

Any reason why you chose SZ_128K?

Thanks
Kishon

Signature · 7 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#30 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

Le 13/12/2017 à 17:50, Cyrille Pitchen a écrit :

Quoted 19 lines

Hi Kishon,

Le 05/12/2017 à 10:19, Kishon Vijay Abraham I a écrit :

Quoted 16 lines

Hi,

Quoted 13 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 7 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

Quoted 7 lines
Quoted 2 lines

hmm.. the endpoint core has to send the function number which right now it's not doing though it has the function number info in pci_epf.

Would it be OK if I add a new patch in the next series adding a 'struct pcie_epf *epf' as a 2nd argument to all handlers in the 'struct pcie_epc_ops'? This way I could have access to epf->func_no as needed.

Except for pci_epc_start() and pci_epc_stop(), both only called from pci_epc_start_store(), I don't have trouble getting the epf value to be passed as a 2nd argument to all other handlers in 'struct pcie_epc_ops'.

Now my next question is: is it better to keep the 'struct pci_epc *epc' as the 1st argument of all those handlers or do you prefer me to remove it as the value can always be retrieved from epf->epc, since now we provide epf as a new argument ?

I have no personal preference. Please let me know your choice :)

Quoted 4 lines

Best regards,

Cyrille

#31 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

Hi Cyrille,

Quoted 32 lines · Cyrille Pitchen
Quoted 19 lines · Cyrille Pitchen

Hi Kishon,

Quoted 16 lines · Kishon Vijay Abraham I

Hi,

Quoted 13 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 7 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

hmm.. the endpoint core has to send the function number which right now it's not doing though it has the function number info in pci_epf.

Would it be OK if I add a new patch in the next series adding a 'struct pcie_epf *epf' as a 2nd argument to all handlers in the 'struct pcie_epc_ops'? This way I could have access to epf->func_no as needed.

I prefer we just pass the func_no as the second argument. Do you see a problem with that?

Quoted 5 lines
Quoted 1 line

Except for pci_epc_start() and pci_epc_stop(), both only called from pci_epc_start_store(), I don't have trouble getting the epf value to be passed as a 2nd argument to all other handlers in 'struct pcie_epc_ops'.

pci_epc_start()/pci_epc_stop() is used to start/stop the end point controller as a whole and shouldn't need epf.

Quoted 5 lines

Now my next question is: is it better to keep the 'struct pci_epc *epc' as the 1st argument of all those handlers or do you prefer me to remove it as the value can always be retrieved from epf->epc, since now we provide epf as a new argument ?

Do we really need to pass epf when func_no is all we need?

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#32 lore raw

Re: [PATCH 5/5] PCI: cadence: add EndPoint Controller driver for Cadence PCIe controller

Hi Kishon,

Le 15/12/2017 à 06:49, Kishon Vijay Abraham I a écrit :

Quoted 53 lines

Hi Cyrille,

Quoted 32 lines · Cyrille Pitchen
Quoted 19 lines · Cyrille Pitchen

Hi Kishon,

Quoted 16 lines · Kishon Vijay Abraham I

Hi,

Quoted 13 lines · Lorenzo Pieralisi
Quoted 1 line · Cyrille Pitchen

This patch adds support to the Cadence PCIe controller in endpoint mode.

Please add a brief description to the log to describe the most salient features.

Quoted 7 lines
---
 drivers/pci/cadence/Kconfig           |   9 +
 drivers/pci/cadence/Makefile          |   1 +
 drivers/pci/cadence/pcie-cadence-ep.c | 553 ++++++++++++++++++++++++++++++++++
 3 files changed, 563 insertions(+)
 create mode 100644 drivers/pci/cadence/pcie-cadence-ep.c

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/pci/cadence/pcie-cadence-ep.c line 66

Quoted 8 lines
drivers/pci/cadence/pcie-cadence-ep.c new file +8 -0
+static int cdns_pcie_ep_write_header(struct pci_epc *epc,
+				     struct pci_epf_header *hdr)
+{
+	struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
+	struct cdns_pcie *pcie = &ep->pcie;
+	u8 fn = 0;
+
+	if (fn == 0) {

I think there is some code to retrieve fn missing here.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

hmm.. the endpoint core has to send the function number which right now it's not doing though it has the function number info in pci_epf.

Would it be OK if I add a new patch in the next series adding a 'struct pcie_epf *epf' as a 2nd argument to all handlers in the 'struct pcie_epc_ops'? This way I could have access to epf->func_no as needed.

I prefer we just pass the func_no as the second argument. Do you see a problem with that?

Quoted 5 lines
Quoted 1 line

Except for pci_epc_start() and pci_epc_stop(), both only called from pci_epc_start_store(), I don't have trouble getting the epf value to be passed as a 2nd argument to all other handlers in 'struct pcie_epc_ops'.

pci_epc_start()/pci_epc_stop() is used to start/stop the end point controller as a whole and shouldn't need epf.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Now my next question is: is it better to keep the 'struct pci_epc *epc' as the 1st argument of all those handlers or do you prefer me to remove it as the value can always be retrieved from epf->epc, since now we provide epf as a new argument ?

Do we really need to pass epf when func_no is all we need?

No, func_no alone is currently all I need so it's perfectly fine for me. I've just wanted to ask before to be sure I implement the expected solution before submitting :)

Thanks!

Cyrille

Quoted 3 lines

Thanks
Kishon

Signature · 7 lines
Cyrille Pitchen, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html