Skip to contentSkip to conversation

PCI: dwc: ep: Remove superfluous function dw_pcie_ep_find_ext_capability()

PATCH Applied Niklas Cassel 5 participants 14 messages Feb 21, 2025 – Mar 7, 2025

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

PATCH 1/2 PCI: dwc: ep: Remove superfluous function dw_pcie_ep_find_ext_capability()

Remove the superfluous function dw_pcie_ep_find_ext_capability(), as it is virtually identical to dw_pcie_find_ext_capability().

---
 .../pci/controller/dwc/pcie-designware-ep.c   | 24 +++----------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/pci/controller/dwc/pcie-designware-ep.c +3 -21
index f9d7f3f989ad..20f2436c7091 100644
@@ -102,24 +102,6 @@ static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
 	return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
 }
 
-static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
-{
-	u32 header;
-	int pos = PCI_CFG_SPACE_SIZE;
-
-	while (pos) {
-		header = dw_pcie_readl_dbi(pci, pos);
-		if (PCI_EXT_CAP_ID(header) == cap)
-			return pos;
-
-		pos = PCI_EXT_CAP_NEXT(header);
-		if (!pos)
-			break;
-	}
-
-	return 0;
-}
-
 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
 				   struct pci_epf_header *hdr)
 {
@@ -230,7 +212,7 @@ static unsigned int dw_pcie_ep_get_rebar_offset(struct dw_pcie *pci,
 	unsigned int offset, nbars;
 	int i;
 
-	offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
+	offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
 	if (!offset)
 		return offset;
 
@@ -847,7 +829,7 @@ static void dw_pcie_ep_init_non_sticky_registers(struct dw_pcie *pci)
 	enum pci_barno bar;
 	u32 reg, i, val;
 
-	offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
+	offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
 
 	dw_pcie_dbi_ro_wr_en(pci);
 
@@ -970,7 +952,7 @@ int dw_pcie_ep_init_registers(struct dw_pcie_ep *ep)
 	if (ep->ops->init)
 		ep->ops->init(ep);
 
-	ptm_cap_base = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
+	ptm_cap_base = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
 
 	/*
 	 * PTM responder capability can be disabled only after disabling
Signature · 1 line
2.48.1
#1 lore raw

PATCH 2/2 PCI: dw-rockchip: hide broken ATS capability

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

---
 drivers/pci/controller/dwc/pcie-dw-rockchip.c | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
drivers/pci/controller/dwc/pcie-dw-rockchip.c +46 -0
index 836ea10eafbb..2be005c1a161 100644
@@ -242,6 +242,51 @@ static const struct dw_pcie_host_ops rockchip_pcie_host_ops = {
 	.init = rockchip_pcie_host_init,
 };
 
+/*
+ * ATS does not work on rk3588 when running in EP mode.
+ * After a host has enabled ATS on the EP side, it will send an IOTLB
+ * invalidation request to the EP side. The rk3588 will never send a completion
+ * back and eventually the host will print an IOTLB_INV_TIMEOUT error, and the
+ * EP will not be operational. If we hide the ATS cap, things work as expected.
+ */
+static void rockchip_pcie_ep_hide_broken_ats_cap_rk3588(struct dw_pcie_ep *ep)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
+	struct device *dev = pci->dev;
+	unsigned int spcie_cap_offset, next_cap_offset;
+	u32 spcie_cap_header, next_cap_header;
+
+	/* only hide the ATS cap for rk3588 running in EP mode */
+	if (!of_device_is_compatible(dev->of_node, "rockchip,rk3588-pcie-ep"))
+		return;
+
+	spcie_cap_offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI);
+	if (!spcie_cap_offset)
+		return;
+
+	spcie_cap_header = dw_pcie_readl_dbi(pci, spcie_cap_offset);
+	next_cap_offset = PCI_EXT_CAP_NEXT(spcie_cap_header);
+
+	next_cap_header = dw_pcie_readl_dbi(pci, next_cap_offset);
+	if (PCI_EXT_CAP_ID(next_cap_header) != PCI_EXT_CAP_ID_ATS)
+		return;
+
+	/* clear next ptr */
+	spcie_cap_header &= ~GENMASK(31, 20);
+
+	/* set next ptr to next ptr of ATS_CAP */
+	spcie_cap_header |= next_cap_header & GENMASK(31, 20);
+
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writel_dbi(pci, spcie_cap_offset, spcie_cap_header);
+	dw_pcie_dbi_ro_wr_dis(pci);
+}
+
+static void rockchip_pcie_ep_pre_init(struct dw_pcie_ep *ep)
+{
+	rockchip_pcie_ep_hide_broken_ats_cap_rk3588(ep);
+}
+
 static void rockchip_pcie_ep_init(struct dw_pcie_ep *ep)
 {
 	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
@@ -314,6 +359,7 @@ rockchip_pcie_get_features(struct dw_pcie_ep *ep)
 
 static const struct dw_pcie_ep_ops rockchip_pcie_ep_ops = {
 	.init = rockchip_pcie_ep_init,
+	.pre_init = rockchip_pcie_ep_pre_init,
 	.raise_irq = rockchip_pcie_raise_irq,
 	.get_features = rockchip_pcie_get_features,
 };
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

Quoted 7 lines · Niklas Cassel

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Maybe add a blank line and indent the message since it's quoted material? E.g.,

When running the rk3588 in endpoint mode, with an Intel IOMMU, the host side prints:

DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD ...

    iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Too bad DMAR isn't smart enough to include a device ID in its message ;)

Can you include something here about what the issue is? Based on the subject line and the patch, I assume something is wrong with the ATS Capability? I guess this is some kind of rk3588 defect, right?

Quoted 9 lines

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

s/capabilties/capabilities/

Bjorn

#5 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Niklas Cassel

Hello,

Quoted 1 line

+ * ATS does not work on rk3588 when running in EP mode.

Would it be OK if we started to style "rk3588" as "RK3588", unless the lower-case is preferred? I had a look at Rockchip's own datasheet, and the product code names seem to be styled with upper-case. That said, I am not a Rockchip expert. Just curious for the sake of consistency.

Quoted 22 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +22 -0
+static void rockchip_pcie_ep_hide_broken_ats_cap_rk3588(struct dw_pcie_ep *ep)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
+	struct device *dev = pci->dev;
+	unsigned int spcie_cap_offset, next_cap_offset;
+	u32 spcie_cap_header, next_cap_header;
+
+	/* only hide the ATS cap for rk3588 running in EP mode */
+	if (!of_device_is_compatible(dev->of_node, "rockchip,rk3588-pcie-ep"))
+		return;
+
+	spcie_cap_offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI);
+	if (!spcie_cap_offset)
+		return;
+
+	spcie_cap_header = dw_pcie_readl_dbi(pci, spcie_cap_offset);
+	next_cap_offset = PCI_EXT_CAP_NEXT(spcie_cap_header);
+
+	next_cap_header = dw_pcie_readl_dbi(pci, next_cap_offset);
+	if (PCI_EXT_CAP_ID(next_cap_header) != PCI_EXT_CAP_ID_ATS)
+		return;
+

drivers/pci/controller/dwc/pcie-dw-rockchip.c static const struct dw_pcie_host_ops rockchip_pcie_host_ops = { · line 283

Quoted 10 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +10 -0
+	/* clear next ptr */
+	spcie_cap_header &= ~GENMASK(31, 20);
+
+	/* set next ptr to next ptr of ATS_CAP */
+	spcie_cap_header |= next_cap_header & GENMASK(31, 20);
+
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writel_dbi(pci, spcie_cap_offset, spcie_cap_header);
+	dw_pcie_dbi_ro_wr_dis(pci);
+}

To keep things consistent, it would be nice to capitalise sentences in code comments, and end them with a full-stop where appropriate (e.g., longer sentences, etc.). That said, this is something I can after applying, to save you the hassle of sending another versions.

Thank you!

Krzysztof

#3 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Bjorn Helgaas

Hello,

Quoted 30 lines
Quoted 7 lines

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Maybe add a blank line and indent the message since it's quoted material? E.g.,

When running the rk3588 in endpoint mode, with an Intel IOMMU, the host side prints:

DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD ...

    iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Too bad DMAR isn't smart enough to include a device ID in its message ;)

Can you include something here about what the issue is? Based on the subject line and the patch, I assume something is wrong with the ATS Capability? I guess this is some kind of rk3588 defect, right?

Quoted 4 lines

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

Quoted 7 lines
Quoted 5 lines

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

s/capabilties/capabilities/

If there are no code changes here, then I can update the commit log directly on the branch itself once applied.

Thank you!

Krzysztof

Reviewed-by on v1 1/2
#10 lore raw
In reply to Niklas Cassel
Quoted 4 lines · Niklas Cassel

Remove the superfluous function dw_pcie_ep_find_ext_capability(), as it is virtually identical to dw_pcie_find_ext_capability().

- Mani

Quoted 63 lines
---
 .../pci/controller/dwc/pcie-designware-ep.c   | 24 +++----------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/pci/controller/dwc/pcie-designware-ep.c +3 -21
index f9d7f3f989ad..20f2436c7091 100644
@@ -102,24 +102,6 @@ static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
 	return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
 }
 
-static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
-{
-	u32 header;
-	int pos = PCI_CFG_SPACE_SIZE;
-
-	while (pos) {
-		header = dw_pcie_readl_dbi(pci, pos);
-		if (PCI_EXT_CAP_ID(header) == cap)
-			return pos;
-
-		pos = PCI_EXT_CAP_NEXT(header);
-		if (!pos)
-			break;
-	}
-
-	return 0;
-}
-
 static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
 				   struct pci_epf_header *hdr)
 {
@@ -230,7 +212,7 @@ static unsigned int dw_pcie_ep_get_rebar_offset(struct dw_pcie *pci,
 	unsigned int offset, nbars;
 	int i;
 
-	offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
+	offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
 	if (!offset)
 		return offset;
 
@@ -847,7 +829,7 @@ static void dw_pcie_ep_init_non_sticky_registers(struct dw_pcie *pci)
 	enum pci_barno bar;
 	u32 reg, i, val;
 
-	offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
+	offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
 
 	dw_pcie_dbi_ro_wr_en(pci);
 
@@ -970,7 +952,7 @@ int dw_pcie_ep_init_registers(struct dw_pcie_ep *ep)
 	if (ep->ops->init)
 		ep->ops->init(ep);
 
-	ptm_cap_base = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
+	ptm_cap_base = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_PTM);
 
 	/*
 	 * PTM responder capability can be disabled only after disabling
Signature · 1 line
2.48.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#6 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Niklas Cassel
Quoted 38 lines · Niklas Cassel

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

---
 drivers/pci/controller/dwc/pcie-dw-rockchip.c | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
drivers/pci/controller/dwc/pcie-dw-rockchip.c +7 -0
index 836ea10eafbb..2be005c1a161 100644
@@ -242,6 +242,51 @@ static const struct dw_pcie_host_ops rockchip_pcie_host_ops = {
 	.init = rockchip_pcie_host_init,
 };
 
+/*
+ * ATS does not work on rk3588 when running in EP mode.
+ * After a host has enabled ATS on the EP side, it will send an IOTLB
+ * invalidation request to the EP side. The rk3588 will never send a completion
+ * back and eventually the host will print an IOTLB_INV_TIMEOUT error, and the
+ * EP will not be operational. If we hide the ATS cap, things work as expected.
+ */

drivers/pci/controller/dwc/pcie-dw-rockchip.c static const struct dw_pcie_host_ops rockchip_pcie_host_ops = { · line 261

Quoted 10 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +10 -0
+static void rockchip_pcie_ep_hide_broken_ats_cap_rk3588(struct dw_pcie_ep *ep)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
+	struct device *dev = pci->dev;
+	unsigned int spcie_cap_offset, next_cap_offset;
+	u32 spcie_cap_header, next_cap_header;
+
+	/* only hide the ATS cap for rk3588 running in EP mode */
+	if (!of_device_is_compatible(dev->of_node, "rockchip,rk3588-pcie-ep"))
+		return;

Compatible checks always tend to extend. So please use a boolean flag to identify the quirk in 'struct rockchip_pcie_of_data' and set it in 'rockchip_pcie_ep_of_data_rk3588'. This way, other SoCs could also reuse the flag if required.

Quoted 11 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +11 -0
+
+	spcie_cap_offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI);
+	if (!spcie_cap_offset)
+		return;
+
+	spcie_cap_header = dw_pcie_readl_dbi(pci, spcie_cap_offset);
+	next_cap_offset = PCI_EXT_CAP_NEXT(spcie_cap_header);
+
+	next_cap_header = dw_pcie_readl_dbi(pci, next_cap_offset);
+	if (PCI_EXT_CAP_ID(next_cap_header) != PCI_EXT_CAP_ID_ATS)
+		return;

drivers/pci/controller/dwc/pcie-dw-rockchip.c static const struct dw_pcie_host_ops rockchip_pcie_host_ops = { · line 282

Quoted 10 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +10 -0
+
+	/* clear next ptr */
+	spcie_cap_header &= ~GENMASK(31, 20);
+
+	/* set next ptr to next ptr of ATS_CAP */
+	spcie_cap_header |= next_cap_header & GENMASK(31, 20);
+
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writel_dbi(pci, spcie_cap_offset, spcie_cap_header);
+	dw_pcie_dbi_ro_wr_dis(pci);

This code is mostly generic. So please move it to pcie-designware-ep.c. The function should just accept two CAP IDs (prev and target) and hide the target capability. Like,

	dw_pcie_hide_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI,
				    PCI_EXT_CAP_ID_ATS);

Its too bad that we cannot traverse back from the target capability. We could open code dw_pcie_ep_find_ext_capability() to keep track of the prev_cap_offset though.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#11 lore raw
In reply to Manivannan Sadhasivam
Quoted 7 lines · Manivannan Sadhasivam
Quoted 4 lines · Niklas Cassel

Remove the superfluous function dw_pcie_ep_find_ext_capability(), as it is virtually identical to dw_pcie_find_ext_capability().

I will be busy so will probably not have time to resin this for 1-2 weeks.

Perhaps patch 1/2 in this series could be picked up in the meantime, since it really has nothing to do with patch 2/2.

Kind regards,
Niklas

#12 lore raw

Hello,

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Remove the superfluous function dw_pcie_ep_find_ext_capability(), as it is virtually identical to dw_pcie_find_ext_capability().

I will be busy so will probably not have time to resin this for 1-2 weeks.

Perhaps patch 1/2 in this series could be picked up in the meantime, since it really has nothing to do with patch 2/2.

Sounds good to me. I will pick it up as a standalone change.

Thank you!

Krzysztof

#4 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Bjorn Helgaas

Hello,

[...]

Quoted 13 lines

Can you include something here about what the issue is? Based on the subject line and the patch, I assume something is wrong with the ATS Capability? I guess this is some kind of rk3588 defect, right?

Quoted 9 lines

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

Rockchip folks, anything to add about this issue? Perhaps there is an erratum about this? Any code reviews? Anything?

Western Digital folks are doing you a lot of favour with all the upstream work they do maintaining drivers for your platforms. But it would be nice if Rockchip took some ownership. I have seen none recently. No reviews, not even an Acked-by, nothing. A bit of a letdown, if you ask me.

Thank you!

Krzysztof

Acked-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Niklas Cassel
Quoted 8 lines · Niklas Cassel

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Quoted 6 lines

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way,

Niklas, Thanks for reporting this issue. It's been a while before
getting confirmation from the design team. Now I can confirm the ATS support for RK3588 is only available running as RC but I'm still requesting erratum about this issue if possible.

Quoted 73 lines

we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

---
  drivers/pci/controller/dwc/pcie-dw-rockchip.c | 46 +++++++++++++++++++
  1 file changed, 46 insertions(+)
drivers/pci/controller/dwc/pcie-dw-rockchip.c +46 -0
index 836ea10eafbb..2be005c1a161 100644
@@ -242,6 +242,51 @@ static const struct dw_pcie_host_ops rockchip_pcie_host_ops = {
  	.init = rockchip_pcie_host_init,
  };
 
+/*
+ * ATS does not work on rk3588 when running in EP mode.
+ * After a host has enabled ATS on the EP side, it will send an IOTLB
+ * invalidation request to the EP side. The rk3588 will never send a completion
+ * back and eventually the host will print an IOTLB_INV_TIMEOUT error, and the
+ * EP will not be operational. If we hide the ATS cap, things work as expected.
+ */
+static void rockchip_pcie_ep_hide_broken_ats_cap_rk3588(struct dw_pcie_ep *ep)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
+	struct device *dev = pci->dev;
+	unsigned int spcie_cap_offset, next_cap_offset;
+	u32 spcie_cap_header, next_cap_header;
+
+	/* only hide the ATS cap for rk3588 running in EP mode */
+	if (!of_device_is_compatible(dev->of_node, "rockchip,rk3588-pcie-ep"))
+		return;
+
+	spcie_cap_offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI);
+	if (!spcie_cap_offset)
+		return;
+
+	spcie_cap_header = dw_pcie_readl_dbi(pci, spcie_cap_offset);
+	next_cap_offset = PCI_EXT_CAP_NEXT(spcie_cap_header);
+
+	next_cap_header = dw_pcie_readl_dbi(pci, next_cap_offset);
+	if (PCI_EXT_CAP_ID(next_cap_header) != PCI_EXT_CAP_ID_ATS)
+		return;
+
+	/* clear next ptr */
+	spcie_cap_header &= ~GENMASK(31, 20);
+
+	/* set next ptr to next ptr of ATS_CAP */
+	spcie_cap_header |= next_cap_header & GENMASK(31, 20);
+
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writel_dbi(pci, spcie_cap_offset, spcie_cap_header);
+	dw_pcie_dbi_ro_wr_dis(pci);
+}
+
+static void rockchip_pcie_ep_pre_init(struct dw_pcie_ep *ep)
+{
+	rockchip_pcie_ep_hide_broken_ats_cap_rk3588(ep);
+}
+
  static void rockchip_pcie_ep_init(struct dw_pcie_ep *ep)
  {
  	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
@@ -314,6 +359,7 @@ rockchip_pcie_get_features(struct dw_pcie_ep *ep)
 
  static const struct dw_pcie_ep_ops rockchip_pcie_ep_ops = {
  	.init = rockchip_pcie_ep_init,
+	.pre_init = rockchip_pcie_ep_pre_init,
  	.raise_irq = rockchip_pcie_raise_irq,
  	.get_features = rockchip_pcie_get_features,
  };
#9 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

Hello Shawn,

Quoted 16 lines · Shawn Lin
Quoted 8 lines · Niklas Cassel

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Quoted 6 lines

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way,

Quoted 6 lines

Niklas, Thanks for reporting this issue. It's been a while before getting confirmation from the design team. Now I can confirm the ATS support for RK3588 is only available running as RC but I'm still requesting erratum about this issue if possible.

Thank you for confirming!

Considering that rock5b running in RC mode:

# lspci -vvvs 0000:00:00.0 | grep Capa
        Capabilities: [40] Power Management version 3
        Capabilities: [50] MSI: Enable+ Count=16/32 Maskable+ 64bit+
        Capabilities: [70] Express (v2) Root Port (Slot-), IntMsgNum 8
        Capabilities: [b0] MSI-X: Enable- Count=128 Masked-
        Capabilities: [100 v2] Advanced Error Reporting
        Capabilities: [148 v1] Secondary PCI Express
        Capabilities: [190 v1] L1 PM Substates
        Capabilities: [1d0 v1] Vendor Specific Information: ID=0002 Rev=4 Len=100 <?>
        Capabilities: [2d0 v1] Vendor Specific Information: ID=0006 Rev=0 Len=018 <?>

and rock5b running in EP mode:

# lspci -vvvs 0000:01:00.0 | grep Capa
        Capabilities: [40] Power Management version 3
        Capabilities: [50] MSI: Enable+ Count=32/32 Maskable+ 64bit+
        Capabilities: [70] Express (v2) Endpoint, IntMsgNum 8
        Capabilities: [b0] MSI-X: Enable- Count=2048 Masked-
        Capabilities: [100 v2] Advanced Error Reporting
        Capabilities: [148 v1] Secondary PCI Express
        Capabilities: [178 v1] Page Request Interface (PRI)
                Page Request Capacity: 00000001, Page Request Allocation: 00000000
        Capabilities: [188 v1] Latency Tolerance Reporting
        Capabilities: [190 v1] L1 PM Substates
        Capabilities: [1a0 v1] Dynamic Power Allocation <?>
        Capabilities: [1d0 v1] Vendor Specific Information: ID=0002 Rev=4 Len=100 <?>
        Capabilities: [2d0 v1] Vendor Specific Information: ID=0006 Rev=0 Len=018 <?>
        Capabilities: [2e8 v1] Physical Resizable BAR

already exposes different Capabilities (depending on the mode the PCIe controller is running in), I would say that it slightly confusing that Synopsys chose not to hide the ATS Capability when the PCIe controller is running in EP mode.
So, I would guess that there is an errata for this.

But I think that your confirmation is enough.
Will take a while before I can send out a V2 though, but quite confident that we can get something merged in time for 6.15.

Kind regards,
Niklas

#7 lore raw

Re: [PATCH 2/2] PCI: dw-rockchip: hide broken ATS capability

In reply to Manivannan Sadhasivam

Hello Mani,

Quoted 46 lines · Manivannan Sadhasivam
Quoted 45 lines · Niklas Cassel

When running the rk3588 in endpoint mode, with an Intel host with IOMMU enabled, the host side prints:
DMAR: VT-d detected Invalidation Time-out Error: SID 0

When running the rk3588 in endpoint mode, with an AMD host with IOMMU enabled, the host side prints:
iommu ivhd0: AMD-Vi: Event logged [IOTLB_INV_TIMEOUT device=63:00.0 address=0x42b5b01a0]

Usually, to handle these issues, we add a quirk for the PCI vendor and device ID in drivers/pci/quirks.c with quirk_no_ats(). That is because we cannot usually modify the capabilities on the EP side.

In this case, we can modify the capabilties on the EP side. Thus, hide the broken ATS capability on rk3588 when running in EP mode. That way, we don't need any quirk on the host side, and we see no errors on the host side, and we can run pci_endpoint_test successfully, with the IOMMU enabled on the host side.

---
 drivers/pci/controller/dwc/pcie-dw-rockchip.c | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
drivers/pci/controller/dwc/pcie-dw-rockchip.c +14 -0
index 836ea10eafbb..2be005c1a161 100644
@@ -242,6 +242,51 @@ static const struct dw_pcie_host_ops rockchip_pcie_host_ops = {
 	.init = rockchip_pcie_host_init,
 };
 
+/*
+ * ATS does not work on rk3588 when running in EP mode.
+ * After a host has enabled ATS on the EP side, it will send an IOTLB
+ * invalidation request to the EP side. The rk3588 will never send a completion
+ * back and eventually the host will print an IOTLB_INV_TIMEOUT error, and the
+ * EP will not be operational. If we hide the ATS cap, things work as expected.
+ */
+static void rockchip_pcie_ep_hide_broken_ats_cap_rk3588(struct dw_pcie_ep *ep)
+{
+	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
+	struct device *dev = pci->dev;
+	unsigned int spcie_cap_offset, next_cap_offset;
+	u32 spcie_cap_header, next_cap_header;
+
Quoted 8 lines

drivers/pci/controller/dwc/pcie-dw-rockchip.c static const struct dw_pcie_host_ops rockchip_pcie_host_ops = { · line 261

Quoted 3 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +3 -0
+	/* only hide the ATS cap for rk3588 running in EP mode */
+	if (!of_device_is_compatible(dev->of_node, "rockchip,rk3588-pcie-ep"))
+		return;

Compatible checks always tend to extend. So please use a boolean flag to identify the quirk in 'struct rockchip_pcie_of_data' and set it in 'rockchip_pcie_ep_of_data_rk3588'. This way, other SoCs could also reuse the flag if required.

I agree that in many cases, compatible checks tend to extend.

However, the ATS capability is broken only for rk3588, thus we need to provide the ATS cap_id and the cap_id for the CAP immediately preceding the ATS cap_id.

The linked list of capabilities will be different for every SoC. If we look at the only other SoC supported by this EPC, rk3568, it does not even have the ATS cap in the linked list.

Thus, I think that creating a boolean flag for this is wrong. Even in the unlikely scenario that there will come a Rockchip SoC in the future with an identical linked list of capabilities, I think that what you are proposing this is premature optimization (AKA the root of all evil :P)

Quoted 27 lines
Quoted 12 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +11 -0
+
+	spcie_cap_offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI);
+	if (!spcie_cap_offset)
+		return;
+
+	spcie_cap_header = dw_pcie_readl_dbi(pci, spcie_cap_offset);
+	next_cap_offset = PCI_EXT_CAP_NEXT(spcie_cap_header);
+
+	next_cap_header = dw_pcie_readl_dbi(pci, next_cap_offset);
+	if (PCI_EXT_CAP_ID(next_cap_header) != PCI_EXT_CAP_ID_ATS)
+		return;

drivers/pci/controller/dwc/pcie-dw-rockchip.c static const struct dw_pcie_host_ops rockchip_pcie_host_ops = { · line 282

Quoted 10 lines
drivers/pci/controller/dwc/pcie-dw-rockchip.c +10 -0
+
+	/* clear next ptr */
+	spcie_cap_header &= ~GENMASK(31, 20);
+
+	/* set next ptr to next ptr of ATS_CAP */
+	spcie_cap_header |= next_cap_header & GENMASK(31, 20);
+
+	dw_pcie_dbi_ro_wr_en(pci);
+	dw_pcie_writel_dbi(pci, spcie_cap_offset, spcie_cap_header);
+	dw_pcie_dbi_ro_wr_dis(pci);

This code is mostly generic. So please move it to pcie-designware-ep.c. The function should just accept two CAP IDs (prev and target) and hide the target capability. Like,

Quoted 2 lines
	dw_pcie_hide_ext_capability(pci, PCI_EXT_CAP_ID_SECPCI,
				    PCI_EXT_CAP_ID_ATS);

Sure, I will create a generic function in pcie-designware-ep.c.

Quoted 4 lines

Its too bad that we cannot traverse back from the target capability. We could open code dw_pcie_ep_find_ext_capability() to keep track of the prev_cap_offset though.

I'd rather skip this. Seems like too many special cases.
E.g. what should dw_pcie_ep_find_ext_capability() return if there is no prev? It seems easier to just need to provide prev_cap_id as well.

Kind regards,
Niklas