Skip to contentSkip to conversation

Fix ARM Generic Timer interrupt parsing

PATCH In review Andrei Cherechesu (OSS) 4 participants 8 messages Mar 13, 2023 – Mar 22, 2023

lore
Share & tools
lore
#0 lore raw

v3 cover Fix ARM Generic Timer interrupt parsing

From: Andrei Cherechesu <[email protected]>

This 2-patch series fixes the parsing of the ARM Generic Timer interrupts from the device tree.

If the generic timer interrupts order in the DT was different than the expected order in Xen code, these interrupts would no longer be correctly parsed and registered by Xen, and would result in boot failure.

This method with using "interrupt-names" for the generic timer interrupts instead of having them hardcoded in the DTB in a specific order is the newer approach already implemented in Linux. Xen did not have the necessary code for this approach, and it has been implemented by the means of this patch series.

Functionality should remain the same if "interrupt-names" is not present in the Generic Timer DTB node of the platform, but the interrupts should then still be present in the expected "sec-phys", "phys", "virt", "hyp-phys", "hyp-virt" order. If "interrupt-names" is present, now it is also correctly handled.

Changes v2->v3:
- Fixed 2nd commit message to mention that treating 0 as a valid
return for platform_get_irq would lead to a BUG() when trying to reserve vIRQ being SGI instead of "during vgic_reserve_virq()".
- Moved timer_irq_names[] in the init_dt_xen_time() function
as it's only used there, and marked it as __initconst and also as const ptr.

Changes v1->v2:
- Rebased on latest staging as of 2023-03-09
- Fixed coding style of comment added in 2nd commit
- Added to 2nd commit message explanation as to why 0 should
be treated as an error case for platform_get_irq

Andrei Cherechesu (2):
arch/arm: irq: Add platform_get_irq_byname() implementation arch/arm: time: Add support for parsing interrupts by names

 xen/arch/arm/include/asm/irq.h        |  2 ++
 xen/arch/arm/include/asm/time.h       |  3 ++-
 xen/arch/arm/irq.c                    | 14 +++++++++++
 xen/arch/arm/time.c                   | 26 +++++++++++++++++---
 xen/drivers/passthrough/arm/smmu-v3.c | 35 +++++----------------------
 5 files changed, 46 insertions(+), 34 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/2 arch/arm: irq: Add platform_get_irq_byname() implementation

From: Andrei Cherechesu <[email protected]>

Moved implementation for the function which parses the IRQs of a DT node by the "interrupt-names" property from the SMMU-v3 driver to the IRQ core code and made it non-static to be used as helper.

Also changed it to receive a "struct dt_device_node*" as parameter, like its counterpart, platform_get_irq(). Updated its usage inside the SMMU-v3 driver accordingly.

---
 xen/arch/arm/include/asm/irq.h        |  2 ++
 xen/arch/arm/irq.c                    | 14 +++++++++++
 xen/drivers/passthrough/arm/smmu-v3.c | 35 +++++----------------------
 3 files changed, 22 insertions(+), 29 deletions(-)
xen/arch/arm/include/asm/irq.h +2 -0
index 245f49dcba..af94f41994 100644
@@ -89,6 +89,8 @@ int irq_set_type(unsigned int irq, unsigned int type);
 
 int platform_get_irq(const struct dt_device_node *device, int index);
 
+int platform_get_irq_byname(struct dt_device_node *np, const char *name);
+
 void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask);
 
 /*
xen/arch/arm/irq.c +14 -0
index 79718f68e7..ded495792b 100644
@@ -718,6 +718,20 @@ int platform_get_irq(const struct dt_device_node *device, int index)
     return irq;
 }
 
+int platform_get_irq_byname(struct dt_device_node *np, const char *name)
+{
+	int index;
+
+	if ( unlikely(!name) )
+		return -EINVAL;
+
+	index = dt_property_match_string(np, "interrupt-names", name);
+	if ( index < 0 )
+		return index;
+
+	return platform_get_irq(np, index);
+}
+
 /*
  * Local variables:
  * mode: C
xen/drivers/passthrough/arm/smmu-v3.c +6 -29
index d58c5cd0bf..bfdb62b395 100644
@@ -200,30 +200,6 @@ static inline void dev_iommu_priv_set(struct device *dev, void *priv)
 	fwspec->iommu_priv = priv;
 }
 
-static int platform_get_irq_byname_optional(struct device *dev,
-				const char *name)
-{
-	int index, ret;
-	struct dt_device_node *np  = dev_to_dt(dev);
-
-	if (unlikely(!name))
-		return -EINVAL;
-
-	index = dt_property_match_string(np, "interrupt-names", name);
-	if (index < 0) {
-		dev_info(dev, "IRQ %s not found\n", name);
-		return index;
-	}
-
-	ret = platform_get_irq(np, index);
-	if (ret < 0) {
-		dev_err(dev, "failed to get irq index %d\n", index);
-		return -ENODEV;
-	}
-
-	return ret;
-}
-
 /* Start of Linux SMMUv3 code */
 static bool disable_bypass = 1;
 
@@ -2434,6 +2410,7 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	int irq, ret;
 	paddr_t ioaddr, iosize;
 	struct arm_smmu_device *smmu;
+	struct dt_device_node *np = dev_to_dt(pdev);
 
 	smmu = xzalloc(struct arm_smmu_device);
 	if (!smmu)
@@ -2451,7 +2428,7 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	}
 
 	/* Base address */
-	ret = dt_device_get_address(dev_to_dt(pdev), 0, &ioaddr, &iosize);
+	ret = dt_device_get_address(np, 0, &ioaddr, &iosize);
 	if (ret)
 		goto out_free_smmu;
 
@@ -2484,19 +2461,19 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 
 	/* Interrupt lines */
 
-	irq = platform_get_irq_byname_optional(pdev, "combined");
+	irq = platform_get_irq_byname(np, "combined");
 	if (irq > 0)
 		smmu->combined_irq = irq;
 	else {
-		irq = platform_get_irq_byname_optional(pdev, "eventq");
+		irq = platform_get_irq_byname(np, "eventq");
 		if (irq > 0)
 			smmu->evtq.q.irq = irq;
 
-		irq = platform_get_irq_byname_optional(pdev, "priq");
+		irq = platform_get_irq_byname(np, "priq");
 		if (irq > 0)
 			smmu->priq.q.irq = irq;
 
-		irq = platform_get_irq_byname_optional(pdev, "gerror");
+		irq = platform_get_irq_byname(np, "gerror");
 		if (irq > 0)
 			smmu->gerr_irq = irq;
 	}
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH v3 1/2] arch/arm: irq: Add platform_get_irq_byname() implementation

Hi Andrei,

I realized this has already been merged. But I would like to point out a few things for future series.

Quoted 36 lines · Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Moved implementation for the function which parses the IRQs of a DT node by the "interrupt-names" property from the SMMU-v3 driver to the IRQ core code and made it non-static to be used as helper.

Also changed it to receive a "struct dt_device_node*" as parameter, like its counterpart, platform_get_irq(). Updated its usage inside the SMMU-v3 driver accordingly.

---
  xen/arch/arm/include/asm/irq.h        |  2 ++
  xen/arch/arm/irq.c                    | 14 +++++++++++
  xen/drivers/passthrough/arm/smmu-v3.c | 35 +++++----------------------
  3 files changed, 22 insertions(+), 29 deletions(-)
xen/arch/arm/include/asm/irq.h +2 -0
index 245f49dcba..af94f41994 100644
@@ -89,6 +89,8 @@ int irq_set_type(unsigned int irq, unsigned int type);
 
  int platform_get_irq(const struct dt_device_node *device, int index);
 
+int platform_get_irq_byname(struct dt_device_node *np, const char *name);
+
  void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask);
 
  /*
xen/arch/arm/irq.c +0 -0
index 79718f68e7..ded495792b 100644

xen/arch/arm/irq.c int platform_get_irq(const struct dt_device_node *device, int index) · line 721

Quoted 4 lines
xen/arch/arm/irq.c +1 -0
@@ -718,6 +718,20 @@ int platform_get_irq(const struct dt_device_node *device, int index)
      return irq;
  }
 
+int platform_get_irq_byname(struct dt_device_node *np, const char *name)

You are changing the name but don't really explain why. "np" also ought to be const as this is not meant to be modified.

xen/arch/arm/irq.c int platform_get_irq(const struct dt_device_node *device, int index) · line 732

Quoted 11 lines
xen/arch/arm/irq.c +11 -0
+{
+	int index;
+
+	if ( unlikely(!name) )
+		return -EINVAL;
+
+	index = dt_property_match_string(np, "interrupt-names", name);
+	if ( index < 0 )
+		return index;
+
+	return platform_get_irq(np, index);

The existing helper was returning -ENODEV when there is an error. But here, you went differently. This is the sort of thing that ought to be explained in the commit message because it is not obvious why you changed it *and* that you actually checked the callers are OK with that.

Thankfully they are all.

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

Re: [PATCH v3 1/2] arch/arm: irq: Add platform_get_irq_byname() implementation

Hi Julien,

Quoted 42 lines · Julien Grall

Hi Andrei,

I realized this has already been merged. But I would like to point out a few things for future series.

Quoted 36 lines · Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Moved implementation for the function which parses the IRQs of a DT node by the "interrupt-names" property from the SMMU-v3 driver to the IRQ core code and made it non-static to be used as helper.

Also changed it to receive a "struct dt_device_node*" as parameter, like its counterpart, platform_get_irq(). Updated its usage inside the SMMU-v3 driver accordingly.

---
  xen/arch/arm/include/asm/irq.h        |  2 ++ xen/arch/arm/irq.c                    | 14 +++++++++++ xen/drivers/passthrough/arm/smmu-v3.c | 35 +++++----------------------

  3 files changed, 22 insertions(+), 29 deletions(-)
xen/arch/arm/include/asm/irq.h +0 -0
index 245f49dcba..af94f41994 100644
@@ -89,6 +89,8 @@ int irq_set_type(unsigned int irq, unsigned int type);
     int platform_get_irq(const struct dt_device_node *device, int index);

  +int platform_get_irq_byname(struct dt_device_node *np, const char *name);
+
  void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask);
    /*

xen/arch/arm/irq.c +0 -0
index 79718f68e7..ded495792b 100644
@@ -718,6 +718,20 @@ int platform_get_irq(const struct dt_device_node
Quoted 9 lines
Quoted 5 lines
xen/arch/arm/irq.c +0 -0
 *device, int index)
       return irq;
   }

  +int platform_get_irq_byname(struct dt_device_node *np, const char *name)

You are changing the name but don't really explain why. "np" also ought to be const as this is not meant to be modified.

I did not necessarily see it as a name change, but rather as adding a more generic helper to be used across the codebase, and the "_optional" suffix did not seem a good fit since it is an alternative to "platform_get_irq" functionally, and I tried to keep the naming convention. I will have it in mind for future series.

I agree with "np" being const, I saw you have already submitted a patch to change it.

Quoted 17 lines

xen/arch/arm/irq.c int platform_get_irq(const struct dt_device_node *device, int index) · line 732

Quoted 11 lines
xen/arch/arm/irq.c +11 -0
+{
+    int index;
+
+    if ( unlikely(!name) )
+        return -EINVAL;
+
+    index = dt_property_match_string(np, "interrupt-names", name);
+    if ( index < 0 )
+        return index;
+
+    return platform_get_irq(np, index);

The existing helper was returning -ENODEV when there is an error. But here, you went differently. This is the sort of thing that ought to be explained in the commit message because it is not obvious why you changed it *and* that you actually checked the callers are OK with that.

Quoted 1 line

Thankfully they are all.

The existing helper was only visible and used within the scope of smmu-v3.c, so changing it was not a big impact. I agree that it is not obvious, though, and I will mention something like that in future series.

Thank you for reviewing it.

Andrei

Quoted 3 lines

Cheers,

#4 lore raw

PATCH v3 2/2 arch/arm: time: Add support for parsing interrupts by names

In reply to Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Added support for parsing the ARM generic timer interrupts DT node by the "interrupt-names" property, if it is available.

If not available, the usual parsing based on the expected IRQ order is performed.

Also treated returning 0 as an error case for the platform_get_irq() calls, since it is not a valid PPI ID and treating it as a valid case would only cause Xen to BUG() later, when trying to reserve vIRQ being SGI.

Added the "hyp-virt" PPI to the timer PPI list, even though it's currently not in use. If the "hyp-virt" PPI is not found, the hypervisor won't panic.

---
 xen/arch/arm/include/asm/time.h |  3 ++-
 xen/arch/arm/time.c             | 26 ++++++++++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/time.h +2 -1
index 4b401c1110..49ad8c1a6d 100644
@@ -82,7 +82,8 @@ enum timer_ppi
     TIMER_PHYS_NONSECURE_PPI = 1,
     TIMER_VIRT_PPI = 2,
     TIMER_HYP_PPI = 3,
-    MAX_TIMER_PPI = 4,
+    TIMER_HYP_VIRT_PPI = 4,
+    MAX_TIMER_PPI = 5,
 };
 
 /*
xen/arch/arm/time.c +22 -4
index 433d7be909..0b482d7db3 100644
@@ -149,15 +149,33 @@ static void __init init_dt_xen_time(void)
 {
     int res;
     unsigned int i;
+    bool has_names;
+    static const char * const timer_irq_names[MAX_TIMER_PPI] __initconst = {
+        [TIMER_PHYS_SECURE_PPI] = "sec-phys",
+        [TIMER_PHYS_NONSECURE_PPI] = "phys",
+        [TIMER_VIRT_PPI] = "virt",
+        [TIMER_HYP_PPI] = "hyp-phys",
+        [TIMER_HYP_VIRT_PPI] = "hyp-virt",
+    };
+
+    has_names = dt_property_read_bool(timer, "interrupt-names");
 
     /* Retrieve all IRQs for the timer */
     for ( i = TIMER_PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++ )
     {
-        res = platform_get_irq(timer, i);
-
-        if ( res < 0 )
+        if ( has_names )
+            res = platform_get_irq_byname(timer, timer_irq_names[i]);
+        else
+            res = platform_get_irq(timer, i);
+
+        if ( res > 0 )
+            timer_irq[i] = res;
+        /*
+         * Do not panic if "hyp-virt" PPI is not found, since it's not
+         * currently used.
+         */
+        else if ( i != TIMER_HYP_VIRT_PPI )
             panic("Timer: Unable to retrieve IRQ %u from the device tree\n", i);
-        timer_irq[i] = res;
     }
 }
 
Signature · 1 line
2.35.1
Reviewed-by on v3 2/2
#5 lore raw

Re: [PATCH v3 2/2] arch/arm: time: Add support for parsing interrupts by names

HI Andrei,

Quoted 15 lines · Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Added support for parsing the ARM generic timer interrupts DT node by the "interrupt-names" property, if it is available.

If not available, the usual parsing based on the expected IRQ order is performed.

Also treated returning 0 as an error case for the platform_get_irq() calls, since it is not a valid PPI ID and treating it as a valid case would only cause Xen to BUG() later, when trying to reserve vIRQ being SGI.

Quoted 5 lines

Added the "hyp-virt" PPI to the timer PPI list, even though it's currently not in use. If the "hyp-virt" PPI is not found, the hypervisor won't panic.

Cheers
Bertrand

Quoted 64 lines

---
xen/arch/arm/include/asm/time.h | 3 ++-
xen/arch/arm/time.c | 26 ++++++++++++++++++++++----

2 files changed, 24 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/time.h +2 -1
index 4b401c1110..49ad8c1a6d 100644
@@ -82,7 +82,8 @@ enum timer_ppi
    TIMER_PHYS_NONSECURE_PPI = 1,
    TIMER_VIRT_PPI = 2,
    TIMER_HYP_PPI = 3,
-    MAX_TIMER_PPI = 4,
+    TIMER_HYP_VIRT_PPI = 4,
+    MAX_TIMER_PPI = 5,
 };
 
 /*
xen/arch/arm/time.c +22 -4
index 433d7be909..0b482d7db3 100644
@@ -149,15 +149,33 @@ static void __init init_dt_xen_time(void)
 {
    int res;
    unsigned int i;
+    bool has_names;
+    static const char * const timer_irq_names[MAX_TIMER_PPI] __initconst = {
+        [TIMER_PHYS_SECURE_PPI] = "sec-phys",
+        [TIMER_PHYS_NONSECURE_PPI] = "phys",
+        [TIMER_VIRT_PPI] = "virt",
+        [TIMER_HYP_PPI] = "hyp-phys",
+        [TIMER_HYP_VIRT_PPI] = "hyp-virt",
+    };
+
+    has_names = dt_property_read_bool(timer, "interrupt-names");
 
    /* Retrieve all IRQs for the timer */
    for ( i = TIMER_PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++ )
    {
-        res = platform_get_irq(timer, i);
-
-        if ( res < 0 )
+        if ( has_names )
+            res = platform_get_irq_byname(timer, timer_irq_names[i]);
+        else
+            res = platform_get_irq(timer, i);
+
+        if ( res > 0 )
+            timer_irq[i] = res;
+        /*
+         * Do not panic if "hyp-virt" PPI is not found, since it's not
+         * currently used.
+         */
+        else if ( i != TIMER_HYP_VIRT_PPI )
            panic("Timer: Unable to retrieve IRQ %u from the device tree\n", i);
-        timer_irq[i] = res;
    }
 }
 
Signature · 1 line
2.35.1
Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] arch/arm: time: Add support for parsing interrupts by names

In reply to Andrei Cherechesu (OSS)
Quoted 15 lines · Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Added support for parsing the ARM generic timer interrupts DT node by the "interrupt-names" property, if it is available.

If not available, the usual parsing based on the expected IRQ order is performed.

Also treated returning 0 as an error case for the platform_get_irq() calls, since it is not a valid PPI ID and treating it as a valid case would only cause Xen to BUG() later, when trying to reserve vIRQ being SGI.

Quoted 5 lines

Added the "hyp-virt" PPI to the timer PPI list, even though it's currently not in use. If the "hyp-virt" PPI is not found, the hypervisor won't panic.

~Michal

#7 lore raw

Re: [PATCH v3 2/2] arch/arm: time: Add support for parsing interrupts by names

In reply to Andrei Cherechesu (OSS)

Hi,

Quoted 8 lines · Andrei Cherechesu (OSS)

From: Andrei Cherechesu <[email protected]>

Added support for parsing the ARM generic timer interrupts DT node by the "interrupt-names" property, if it is available.

If not available, the usual parsing based on the expected IRQ order is performed.

Quoted 8 lines

Also treated returning 0 as an error case for the platform_get_irq() calls, since it is not a valid PPI ID and treating it as a valid case would only cause Xen to BUG() later, when trying to reserve vIRQ being SGI.

Added the "hyp-virt" PPI to the timer PPI list, even though it's currently not in use. If the "hyp-virt" PPI is not found, the hypervisor won't panic.

I know this was already merged. But it would have been nice to explain why this is added. As this stands, it looks unnecessary dead code which would have been okay if ...

Quoted 49 lines
---
  xen/arch/arm/include/asm/time.h |  3 ++-
  xen/arch/arm/time.c             | 26 ++++++++++++++++++++++----
  2 files changed, 24 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/time.h +2 -1
index 4b401c1110..49ad8c1a6d 100644
@@ -82,7 +82,8 @@ enum timer_ppi
      TIMER_PHYS_NONSECURE_PPI = 1,
      TIMER_VIRT_PPI = 2,
      TIMER_HYP_PPI = 3,
-    MAX_TIMER_PPI = 4,
+    TIMER_HYP_VIRT_PPI = 4,
+    MAX_TIMER_PPI = 5,
  };
 
  /*
xen/arch/arm/time.c +13 -3
index 433d7be909..0b482d7db3 100644
@@ -149,15 +149,33 @@ static void __init init_dt_xen_time(void)
  {
      int res;
      unsigned int i;
+    bool has_names;
+    static const char * const timer_irq_names[MAX_TIMER_PPI] __initconst = {
+        [TIMER_PHYS_SECURE_PPI] = "sec-phys",
+        [TIMER_PHYS_NONSECURE_PPI] = "phys",
+        [TIMER_VIRT_PPI] = "virt",
+        [TIMER_HYP_PPI] = "hyp-phys",
+        [TIMER_HYP_VIRT_PPI] = "hyp-virt",
+    };
+
+    has_names = dt_property_read_bool(timer, "interrupt-names");
 
      /* Retrieve all IRQs for the timer */
      for ( i = TIMER_PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++ )
      {
-        res = platform_get_irq(timer, i);
-
-        if ( res < 0 )
+        if ( has_names )
+            res = platform_get_irq_byname(timer, timer_irq_names[i]);
+        else

xen/arch/arm/time.c static void __init init_dt_xen_time(void) · line 178

Quoted 10 lines
xen/arch/arm/time.c +9 -0
+            res = platform_get_irq(timer, i);
+
+        if ( res > 0 )
+            timer_irq[i] = res;
+        /*
+         * Do not panic if "hyp-virt" PPI is not found, since it's not
+         * currently used.
+         */
+        else if ( i != TIMER_HYP_VIRT_PPI )
              panic("Timer: Unable to retrieve IRQ %u from the device tree\n", i);

... this wasn't necessary.

xen/arch/arm/time.c static void __init init_dt_xen_time(void) · line 181

Quoted 4 lines
xen/arch/arm/time.c +0 -1
-        timer_irq[i] = res;
      }
  }
 

Cheers,

Signature · 1 line
Julien Grall