Skip to contentSkip to conversation

irqchip/qcom-pdc: don't read version register if it is not available

PATCH In review Dmitry Baryshkov 5 participants 12 messages Aug 25, 2023 – Aug 29, 2023

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

PATCH 1/2 irqchip/qcom-pdc: don't read version register if it is not available

On Qualcomm SM8150 the PDC resource has size 0x400. When PDC driver tries to read the version register (0x1000), it reads past the end of this resource, causing kernel crash.

Check the size of PDC resource before reading the PDC_VERSION register.

Fixes bc82cc42644b ("irqchip/qcom-pdc: Add support for v3.2 HW")
---
 drivers/irqchip/qcom-pdc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/irqchip/qcom-pdc.c +9 -1
index 9bb6951257c2..431b213b5abb 100644
@@ -324,6 +324,7 @@ static int pdc_setup_pin_mapping(struct device_node *np)
 static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
 {
 	struct irq_domain *parent_domain, *pdc_domain;
+	struct resource res;
 	int ret;
 
 	pdc_base = of_iomap(node, 0);
@@ -332,7 +333,14 @@ static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
 		return -ENXIO;
 	}
 
-	pdc_version = pdc_reg_read(PDC_VERSION, 0);
+	if (of_address_to_resource(node, 0, &res))
+		return -EINVAL;
+
+	/* compat with old sm8150 DT which had very small region for PDC */
+	if (resource_size(&res) > PDC_VERSION)
+		pdc_version = pdc_reg_read(PDC_VERSION, 0);
+	else
+		pdc_version = 0;
 
 	parent_domain = irq_find_host(parent);
 	if (!parent_domain) {
Signature · 1 line
2.39.2
#1 lore raw

PATCH 2/2 arm64: dts: qcom: sm8150: extend the size of the PDC resource

Follow the example of other platforms and extend the PDC resource region to 0x30000, so that the PDC driver can read the PDC_VERSION register.

---
 arch/arm64/boot/dts/qcom/sm8150.dtsi | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/boot/dts/qcom/sm8150.dtsi +1 -1
index 380712aee977..38c5d6dbd0d7 100644
@@ -3923,7 +3923,7 @@ dispcc: clock-controller@af00000 {
 
 		pdc: interrupt-controller@b220000 {
 			compatible = "qcom,sm8150-pdc", "qcom,pdc";
-			reg = <0 0x0b220000 0 0x400>;
+			reg = <0 0x0b220000 0 0x30000>;
 			qcom,pdc-ranges = <0 480 94>, <94 609 31>,
 					  <125 63 1>;
 			#interrupt-cells = <2>;
Signature · 1 line
2.39.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] arm64: dts: qcom: sm8150: extend the size of the PDC resource

Quoted 5 lines · Dmitry Baryshkov

Follow the example of other platforms and extend the PDC resource region to 0x30000, so that the PDC driver can read the PDC_VERSION register.

---

Fixes 397ad94668c1 ("arm64: dts: qcom: sm8150: Add pdc interrupt controller node")

Konrad

Quoted 16 lines
 arch/arm64/boot/dts/qcom/sm8150.dtsi | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/boot/dts/qcom/sm8150.dtsi +1 -1
index 380712aee977..38c5d6dbd0d7 100644
@@ -3923,7 +3923,7 @@ dispcc: clock-controller@af00000 {
 
 		pdc: interrupt-controller@b220000 {
 			compatible = "qcom,sm8150-pdc", "qcom,pdc";
-			reg = <0 0x0b220000 0 0x400>;
+			reg = <0 0x0b220000 0 0x30000>;
 			qcom,pdc-ranges = <0 480 94>, <94 609 31>,
 					  <125 63 1>;
 			#interrupt-cells = <2>;
#3 lore raw
In reply to Dmitry Baryshkov

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

reg size in device node must be expanded if its too small to access all registers and i think additional check in driver to check if size is good enough would not be of much use.

Thanks,
Maulik

Quoted 9 lines · Dmitry Baryshkov

On Qualcomm SM8150 the PDC resource has size 0x400. When PDC driver tries to read the version register (0x1000), it reads past the end of this resource, causing kernel crash.

Check the size of PDC resource before reading the PDC_VERSION register.

Fixes bc82cc42644b ("irqchip/qcom-pdc: Add support for v3.2 HW")
#4 lore raw
Quoted 7 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

reg size in device node must be expanded if its too small to access all registers and i think additional check in driver to check if size is good enough would not be of much use.

The devicetree change has already been commited and not taking care of this on the driver side would break backwards compatibility with devicetrees without this change.

Konrad

#5 lore raw
In reply to Maulik Shah (mkshah)
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

P.S. Can I please bring to your attention that top-posting is a frowned upon practice.

Quoted 15 lines

Thanks,
Maulik

Quoted 9 lines · Dmitry Baryshkov

On Qualcomm SM8150 the PDC resource has size 0x400. When PDC driver tries to read the version register (0x1000), it reads past the end of this resource, causing kernel crash.

Check the size of PDC resource before reading the PDC_VERSION register.

Fixes bc82cc42644b ("irqchip/qcom-pdc: Add support for v3.2 HW")
Signature · 2 lines
With best wishes
Dmitry
#6 lore raw
Quoted 16 lines · Dmitry Baryshkov
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Quoted 5 lines

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

There is definitely precedents for this sort of behaviour, such as the ARM GICv2 probe code.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw
Quoted 25 lines · Marc Zyngier
Quoted 16 lines · Dmitry Baryshkov
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Quoted 5 lines

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

Quoted 5 lines

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

Ah. Interesting idea. If that's the overall consensus I can send v2 doing this. Not sure what is better though.

Quoted 3 lines

There is definitely precedents for this sort of behaviour, such as the ARM GICv2 probe code.

Signature · 2 lines
With best wishes
Dmitry
#8 lore raw
Quoted 27 lines · Dmitry Baryshkov
Quoted 25 lines · Marc Zyngier
Quoted 21 lines · Dmitry Baryshkov
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

Quoted 8 lines
Quoted 5 lines

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

Ah. Interesting idea. If that's the overall consensus I can send v2 doing this. Not sure what is better though.

Given that DT files are mostly generated using copy-paste by people making a point not to read specifications, odds are that your current patch would end-up applying the v0 behaviour to v3.2 HW.

What could possibly go wrong?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#9 lore raw
In reply to Dmitry Baryshkov
Quoted 26 lines · Dmitry Baryshkov
Quoted 25 lines · Marc Zyngier
Quoted 21 lines · Dmitry Baryshkov
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

Quoted 8 lines
Quoted 5 lines

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

Ah. Interesting idea. If that's the overall consensus I can send v2 doing this. Not sure what is better though.

Please take my PDC 3.2 patch in the same set!

Neil

Quoted 5 lines
Quoted 4 lines

There is definitely precedents for this sort of behaviour, such as the ARM GICv2 probe code.

#10 lore raw
In reply to Dmitry Baryshkov

Hi,

Quoted 21 lines · Dmitry Baryshkov
Quoted 20 lines · Marc Zyngier
Quoted 18 lines · Dmitry Baryshkov
Quoted 11 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

Quoted 7 lines
Quoted 5 lines

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

Ah. Interesting idea. If that's the overall consensus I can send v2 doing this. Not sure what is better though.

if expanding register size and reading version register looks too hacky the other way is to have "qcom,pdc-v3.2" compatible for newer targets post which don't need to read version register to figure out as the compatible string is enough to tell if v3.2 HW behavior needs to apply.

I am ok with either approach but biased towards using version register rather than compatibles.

Thanks,
Maulik

Quoted 2 lines
Quoted 2 lines

There is definitely precedents for this sort of behaviour, such as the ARM GICv2 probe code.

#11 lore raw
In reply to Marc Zyngier
Quoted 24 lines · Marc Zyngier
Quoted 16 lines · Dmitry Baryshkov
Quoted 8 lines · Maulik Shah (mkshah)

Hi Dmitry,

This patch may be useful if there was a case where some PDCs don't have version register populated/available,
In all PDC versions, version register is always available but due to reg size not good enough in device tree for SM8150 it failed to read.

Quoted 4 lines

reg size in device node must be expanded if its too small to access all registers and i think
additional check in driver to check if size is good enough would not be of much use.

Quoted 5 lines

Unfortunately, it doesn't work this way. DT files are ABI. Even if we change the DT, the kernel should continue working with the older version.
Thus, we have to add such bandaid code, which will keep the kernel from crashing if old DT was used.

Quoted 5 lines

You're missing the point: all existing PDC HW have version register. The fact that the DT is crap doesn't invalidate this simple fact. It is thus perfectly possible for the driver to *ignore* the crap and do the right thing by expanding the size of the mapping, rather than falling back to the non-versioned code.

I wasn't sure if this would cross the "too hacky" boundary, but if you're fine with it, I'll happily ack this approach.

Konrad