Skip to contentSkip to conversation

clocksource/timer-econet-en751221: Support irq number per timer

PATCH New Caleb James DeLisle 2 participants 8 messages Nov 17

Subsystems (4)
lore
Share & tools
lore

Caleb James DeLisle

author maintainer
#0 lore raw

v12 cover clocksource/timer-econet-en751221: Support irq number per timer

This timer appears on both the EcoNet EN751221 (34Kc) and on the EN751627 / EN7528 (1004Kc) SoCs. On the 34Kc the interrupt controller uses a percpu interrupt while on the others it uses an individual IRQ number for each CPU timer. Add support for EN751627 and EN7528. Add support for IRQ number for each CPU timer under the econet,en751627-timer compatible.

Changes since v11:
* Update to tip/timers/core Nov 1, see base-commit.

Changes since v10:
* Unconditionally setup clock_event_device for all possible CPUs so that error log will not access uninitialized data. Identified by:

Changes since v9:
* Use dynamic size irq and iomem block arrays based on DT
* Update commit message explicating that CPU hotplug is intentionally not supported

Changes since v8:
* Actually git commit changes from v7. Guys, I sincerely apologize for the spam.

Changes since v7:
* Use %zd for logging with ARRAY_SIZE to avoid compiler errors if test-compiling on 64 bit.

Changes since v6:
* Rebase to tip/timers/core (base-commit below)

Changes since v5:
* Integrate patch fix refcount leak from:

* NULL econet_timer.membase after unmapping
* NULL check dev->event_handler in case clockevents_config_and_register failed to fully register.
* v5: https://lore.kernel.org/linux-mips/[email protected]/

Changes from v4:
* Fixed incorrect compatible in DT schema
* Bracing in out_irq_free if statement
* I did not move cpuhp_setup_state past the point of no return
* v4: https://lore.kernel.org/linux-mips/[email protected]

Changes from v3:
* off-by-one in out_irq_free
* Gracefully fail if too many address resources are provided in the DT
* Allow fewer actual timers than num_possible_cpus, as long as they never start
* v3: https://lore.kernel.org/linux-mips/[email protected]

v3 note:

I decided to take this patchset in a slightly different direction from v2. There is a certain amount of initialization that once it has run, it cannot be reverted in case of error. The original driver init was a mixture of revertible and non-revertible code which made it too easy for code which is designed with best practices in mind to in fact introduce a bug. Therefore I start by making the init process more legible but without any business logic changes, then fix a race condition by keeping IRQ disabled until after cevt is setup, then finally add non-percpu IRQ support.

* v2: https://lore.kernel.org/linux-mips/[email protected]

Changes from v1:
* Split changes over 3 refactoring patches + main patch
* Remove driver discussion from dt commit message
* v1: https://lore.kernel.org/linux-mips/[email protected]/

Caleb James DeLisle (5):
dt-bindings: timer: econet: Update EN751627 for multi-IRQ clocksource/timer-econet-en751221: Init teardown on error if possible clocksource/timer-econet-en751221: Disable IRQ until cevt registered clocksource/timer-econet-en751221: Support EN751627 without percpu IRQ clocksource/timer-econet-en751221: Do not use NR_CPUS for resource
array size

 .../bindings/timer/econet,en751221-timer.yaml |  31 ++-
 drivers/clocksource/timer-econet-en751221.c   | 206 ++++++++++++++----
 2 files changed, 181 insertions(+), 56 deletions(-)

base-commit: 0359ecff58853a491dd0f09792fea8e87235ae06

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH v12 1/5 dt-bindings: timer: econet: Update EN751627 for multi-IRQ

This hardware is found in the EN751221 SoC family as well as the EN751627. The former uses a percpu IRQ for all timers while the latter uses an individual IRQ number per timer.

---
 .../bindings/timer/econet,en751221-timer.yaml | 31 ++++++++++++-------
 1 file changed, 20 insertions(+), 11 deletions(-)
Documentation/devicetree/bindings/timer/econet,en751221-timer.yaml +20 -11
index c1e7c2b6afde..ac25785ecfdf 100644
@@ -12,24 +12,23 @@ maintainers:
 description:
   The EcoNet High Precision Timer (HPT) is a timer peripheral found in various
   EcoNet SoCs, including the EN751221 and EN751627 families. It provides per-VPE
-  count/compare registers and a per-CPU control register, with a single interrupt
-  line using a percpu-devid interrupt mechanism.
+  count/compare registers and a per-CPU control register. On EN751221 it uses a
+  single interrupt line using a percpu-devid interrupt mechanism, and on
+  EN751627 it uses an interrupt per VPE.
 
 properties:
   compatible:
-    oneOf:
-      - const: econet,en751221-timer
-      - items:
-          - const: econet,en751627-timer
-          - const: econet,en751221-timer
+    enum:
+      - econet,en751221-timer
+      - econet,en751627-timer
 
   reg:
     minItems: 1
     maxItems: 2
 
   interrupts:
-    maxItems: 1
-    description: A percpu-devid timer interrupt shared across CPUs.
+    minItems: 1
+    maxItems: 4
 
   clocks:
     maxItems: 1
@@ -52,21 +51,31 @@ allOf:
           items:
             - description: VPE timers 0 and 1
             - description: VPE timers 2 and 3
+        interrupts:
+          description: An interrupt for each timer (one per VPE)
+          minItems: 4
     else:
       properties:
         reg:
           items:
             - description: VPE timers 0 and 1
+        interrupts:
+          description: A percpu-devid timer interrupt shared across timers
+          maxItems: 1
 
 additionalProperties: false
 
 examples:
   - |
+    #include <dt-bindings/interrupt-controller/mips-gic.h>
     timer@1fbf0400 {
-        compatible = "econet,en751627-timer", "econet,en751221-timer";
+        compatible = "econet,en751627-timer";
         reg = <0x1fbf0400 0x100>, <0x1fbe0000 0x100>;
         interrupt-parent = <&intc>;
-        interrupts = <30>;
+        interrupts = <GIC_SHARED 30 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SHARED 29 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SHARED 37 IRQ_TYPE_LEVEL_HIGH>,
+                     <GIC_SHARED 36 IRQ_TYPE_LEVEL_HIGH>;
         clocks = <&hpt_clock>;
     };
   - |
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v12 1/5] dt-bindings: timer: econet: Update EN751627 for multi-IRQ

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: timer: econet: unwieldy per-compatible conditionals in schema --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: timer: econet: unwieldy per-compatible conditionals in schema
--

commit 03be033e22c76ce913aa7cba221851095ea46465
Author: Caleb James DeLisle <[email protected]>

dt-bindings: timer: econet: Update EN751627 for multi-IRQ

This commit updates the EcoNet HPT binding for EN751627 to drop the econet,en751221-timer fallback compatible and require four per-VPE interrupts instead of a single shared percpu-devid interrupt.

Quoted 42 lines
Documentation/devicetree/bindings/timer/econet,en751221-timer.yaml +9 -9
index c1e7c2b6afde1..ac25785ecfdf6 100644
@@ -12,24 +12,23 @@ maintainers:
 description:
   The EcoNet High Precision Timer (HPT) is a timer peripheral found in various
   EcoNet SoCs, including the EN751221 and EN751627 families. It provides per-VPE
-  count/compare registers and a per-CPU control register, with a single interrupt
-  line using a percpu-devid interrupt mechanism.
+  count/compare registers and a per-CPU control register. On EN751221 it uses a
+  single interrupt line using a percpu-devid interrupt mechanism, and on
+  EN751627 it uses an interrupt per VPE.
 
 properties:
   compatible:
-    oneOf:
-      - const: econet,en751221-timer
-      - items:
-          - const: econet,en751627-timer
-          - const: econet,en751221-timer
+    enum:
+      - econet,en751221-timer
+      - econet,en751627-timer
 
   reg:
     minItems: 1
     maxItems: 2
 
   interrupts:
-    maxItems: 1
-    description: A percpu-devid timer interrupt shared across CPUs.
+    minItems: 1
+    maxItems: 4
 
   clocks:
     maxItems: 1
@@ -52,21 +51,31 @@ allOf:
           items:
             - description: VPE timers 0 and 1
             - description: VPE timers 2 and 3
+        interrupts:

Documentation/devicetree/bindings/timer/econet,en751221-timer.yaml allOf: · line 64

Quoted 10 lines
Documentation/devicetree/bindings/timer/econet,en751221-timer.yaml +5 -0
+          description: An interrupt for each timer (one per VPE)
+          minItems: 4
     else:
       properties:
         reg:
           items:
             - description: VPE timers 0 and 1
+        interrupts:
+          description: A percpu-devid timer interrupt shared across timers
+          maxItems: 1

[Severity: Low]
This isn't a bug, but once econet,en751627-timer drops the econet,en751221-timer fallback and defines its own reg and interrupts schemas in the if/else block, only clocks (maxItems: 1) remains shared between the two variants. Would it be cleaner to split econet,en751627-timer into its own binding schema document rather than defining most of the binding inside the conditional?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v12 2/5 clocksource/timer-econet-en751221: Init teardown on error if possible

In reply to Caleb James DeLisle

As a clocksource, much of the initialization process is irreversible and the impact of a failure to initialize is a failure to boot. That said, good practice is to attempt a clean exit if probing fails, and supporting this pattern will reduce the likelihood that future contributions introduce a bug by trying to teardown after it is no longer possible to do so.

Convert the init process into two clearly delineated phases, one which is reverted in case of error, and the other which can't be. Move all IRQ and address resource mapping before that point, and add teardown logic in case of error before the point of no return.

---
 drivers/clocksource/timer-econet-en751221.c | 84 ++++++++++++---------
 1 file changed, 50 insertions(+), 34 deletions(-)
drivers/clocksource/timer-econet-en751221.c +50 -34
index 1859335345b5..714702b9ef12 100644
@@ -24,6 +24,7 @@
 
 static struct {
 	void __iomem	*membase[ECONET_NUM_BLOCKS];
+	int		irq;
 	u32		freq_hz;
 } econet_timer __ro_after_init;
 
@@ -126,22 +127,9 @@ static void __init cevt_dev_init(uint cpu)
 	iowrite32(U32_MAX, reg_compare(cpu));
 }
 
-static int __init cevt_init(struct device_node *np)
+static void __init cevt_init(struct device_node *np)
 {
-	int i, irq, ret;
-
-	irq = irq_of_parse_and_map(np, 0);
-	if (irq <= 0) {
-		pr_err("%pOFn: irq_of_parse_and_map failed", np);
-		return -EINVAL;
-	}
-
-	ret = request_percpu_irq(irq, cevt_interrupt, np->name, &econet_timer_pcpu);
-
-	if (ret < 0) {
-		pr_err("%pOFn: IRQ %d setup failed (%d)\n", np, irq, ret);
-		goto err_unmap_irq;
-	}
+	int i;
 
 	for_each_possible_cpu(i) {
 		struct clock_event_device *cd = &per_cpu(econet_timer_pcpu, i);
@@ -151,21 +139,12 @@ static int __init cevt_init(struct device_node *np)
 					  CLOCK_EVT_FEAT_C3STOP |
 					  CLOCK_EVT_FEAT_PERCPU;
 		cd->set_next_event	= cevt_set_next_event;
-		cd->irq			= irq;
+		cd->irq			= econet_timer.irq;
 		cd->cpumask		= cpumask_of(i);
 		cd->name		= np->name;
 
 		cevt_dev_init(i);
 	}
-
-	cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
-			  "clockevents/econet/timer:starting",
-			  cevt_init_cpu, NULL);
-	return 0;
-
-err_unmap_irq:
-	irq_dispose_mapping(irq);
-	return ret;
 }
 
 static int __init timer_init(struct device_node *np)
@@ -187,22 +166,45 @@ static int __init timer_init(struct device_node *np)
 		econet_timer.membase[i] = of_iomap(np, i);
 		if (!econet_timer.membase[i]) {
 			pr_err("%pOFn: failed to map register [%d]\n", np, i);
-			return -ENXIO;
+			ret = -ENXIO;
+			goto out_membase;
 		}
 	}
 
+	econet_timer.irq = irq_of_parse_and_map(np, 0);
+	if (econet_timer.irq <= 0) {
+		pr_err("%pOFn: irq_of_parse_and_map failed\n", np);
+		ret = -EINVAL;
+		goto out_membase;
+	}
+
+	ret = request_percpu_irq(econet_timer.irq, cevt_interrupt, np->name,
+				 &econet_timer_pcpu);
+
+	if (ret < 0) {
+		pr_err("%pOFn: IRQ %d setup failed (%d)\n", np,
+		       econet_timer.irq, ret);
+		goto out_irq_mapping;
+	}
+
+	cevt_init(np);
+
+	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
+				"clockevents/econet/timer:starting",
+				cevt_init_cpu, NULL);
+	if (ret < 0) {
+		pr_err("%pOFn: cpuhp setup failed (%d)\n", np, ret);
+		goto out_irq_free;
+	}
+
+	/* Point of no return, do not attempt to tear down after this. */
+
 	/* For clocksource purposes always read clock zero, whatever the CPU */
 	ret = clocksource_mmio_init(reg_count(0), np->name,
 				    econet_timer.freq_hz, 301, ECONET_BITS,
 				    clocksource_mmio_readl_up);
-	if (ret) {
-		pr_err("%pOFn: clocksource_mmio_init failed: %d", np, ret);
-		return ret;
-	}
-
-	ret = cevt_init(np);
-	if (ret < 0)
-		return ret;
+	if (ret)
+		pr_err("%pOFn: clocksource_mmio_init failed: %d\n", np, ret);
 
 	sched_clock_register(sched_clock_read, ECONET_BITS,
 			     econet_timer.freq_hz);
@@ -212,6 +214,20 @@ static int __init timer_init(struct device_node *np)
 		(econet_timer.freq_hz / 1000) % 1000);
 
 	return 0;
+
+out_irq_free:
+	free_percpu_irq(econet_timer.irq, &econet_timer_pcpu);
+out_irq_mapping:
+	irq_dispose_mapping(econet_timer.irq);
+out_membase:
+	for (int i = 0; i < ARRAY_SIZE(econet_timer.membase); i++) {
+		if (econet_timer.membase[i]) {
+			iounmap(econet_timer.membase[i]);
+			econet_timer.membase[i] = NULL;
+		}
+	}
+
+	return ret;
 }
 
 TIMER_OF_DECLARE(econet_timer_hpt, "econet,en751221-timer", timer_init);
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

PATCH v12 3/5 clocksource/timer-econet-en751221: Disable IRQ until cevt registered

In reply to Caleb James DeLisle

Eliminate a race condition where cevt_interrupt may trigger before clockevents_config_and_register has been called, and dev->event_handler is at that point NULL. Additionally, NULL check dev->event_handler in interrupt callback just in case clockevents_config_and_register failed.

---
 drivers/clocksource/timer-econet-en751221.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/clocksource/timer-econet-en751221.c +8 -4
index 714702b9ef12..f2c4c1ee0a56 100644
@@ -75,7 +75,10 @@ static irqreturn_t cevt_interrupt(int irq, void *dev_id)
 		return IRQ_NONE;
 
 	iowrite32(ioread32(reg_count(cpu)), reg_compare(cpu));
-	dev->event_handler(dev);
+
+	if (dev->event_handler)
+		dev->event_handler(dev);
+
 	return IRQ_HANDLED;
 }
 
@@ -104,12 +107,11 @@ static int cevt_init_cpu(uint cpu)
 	reg = ioread32(reg_ctl(cpu)) | ctl_bit_enabled(cpu);
 	iowrite32(reg, reg_ctl(cpu));
 
-	enable_percpu_irq(cd->irq, IRQ_TYPE_NONE);
-
-	/* Do this last because it synchronously configures the timer */
 	clockevents_config_and_register(cd, econet_timer.freq_hz,
 					ECONET_MIN_DELTA, ECONET_MAX_DELTA);
 
+	enable_percpu_irq(cd->irq, IRQ_TYPE_NONE);
+
 	return 0;
 }
 
@@ -178,6 +180,8 @@ static int __init timer_init(struct device_node *np)
 		goto out_membase;
 	}
 
+	irq_set_status_flags(econet_timer.irq, IRQ_NOAUTOEN);
+
 	ret = request_percpu_irq(econet_timer.irq, cevt_interrupt, np->name,
 				 &econet_timer_pcpu);
 
Signature · 1 line
2.39.5
#5 lore raw

Re: [PATCH v12 3/5] clocksource/timer-econet-en751221: Disable IRQ until cevt registered

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] clocksource/timer-econet-en751221: missing Fixes trailer for IRQ race fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] clocksource/timer-econet-en751221: missing Fixes trailer for IRQ race fix
--

commit 93affbec7851d413710a9c422ba505847184084a
Author: Caleb James DeLisle <[email protected]>

Quoted 8 lines

clocksource/timer-econet-en751221: Disable IRQ until cevt registered

Eliminate a race condition where cevt_interrupt may trigger before clockevents_config_and_register has been called, and dev->event_handler is at that point NULL. Additionally, NULL check dev->event_handler in interrupt callback just in case clockevents_config_and_register failed.

[Severity: Low]
This fixes a race condition and potential NULL pointer dereference in cevt_interrupt() and cevt_init_cpu(). Should it include a Fixes: tag for the commit that introduced the driver?

Fixes 3b4c33ac87d0 ("clocksource/drivers: Add EcoNet Timer HPT driver")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#6 lore raw

PATCH v12 4/5 clocksource/timer-econet-en751221: Support EN751627 without percpu IRQ

In reply to Caleb James DeLisle

EN751627 is based on the 1004Kc which uses a different interrupt number for each CPU timer. Support both this and the EN751221 which uses a single percpu interrupt.

CPU hotplug path is intentionally unsupported because this CPU timer is part of a legacy MIPS CPU which never had support for it.

---
 drivers/clocksource/timer-econet-en751221.c | 131 ++++++++++++++++----
 1 file changed, 108 insertions(+), 23 deletions(-)
drivers/clocksource/timer-econet-en751221.c +108 -23
index f2c4c1ee0a56..edb3db3125ce 100644
@@ -24,7 +24,9 @@
 
 static struct {
 	void __iomem	*membase[ECONET_NUM_BLOCKS];
-	int		irq;
+	int		*irqs;
+	int		num_irqs;
+	bool		is_percpu;
 	u32		freq_hz;
 } econet_timer __ro_after_init;
 
@@ -102,6 +104,25 @@ static int cevt_init_cpu(uint cpu)
 	struct clock_event_device *cd = &per_cpu(econet_timer_pcpu, cpu);
 	u32 reg;
 
+	if (!reg_ctl(cpu)) {
+		pr_err("%s: missing address resource for CPU %d\n", cd->name,
+		       cpu);
+		return -EINVAL;
+	}
+	if (cd->irq <= 0) {
+		pr_err("%s: missing IRQ for CPU %d\n", cd->name, cpu);
+		return -EINVAL;
+	}
+	if (!econet_timer.is_percpu) {
+		int ret = irq_force_affinity(cd->irq, cpumask_of(cpu));
+
+		if (ret) {
+			pr_err("%s: failed to set IRQ affinity to CPU %d: %pe\n",
+			       cd->name, cpu, ERR_PTR(ret));
+			return ret;
+		}
+	}
+
 	pr_debug("%s: Setting up clockevent for CPU %d\n", cd->name, cpu);
 
 	reg = ioread32(reg_ctl(cpu)) | ctl_bit_enabled(cpu);
@@ -110,7 +131,10 @@ static int cevt_init_cpu(uint cpu)
 	clockevents_config_and_register(cd, econet_timer.freq_hz,
 					ECONET_MIN_DELTA, ECONET_MAX_DELTA);
 
-	enable_percpu_irq(cd->irq, IRQ_TYPE_NONE);
+	if (econet_timer.is_percpu)
+		enable_percpu_irq(cd->irq, IRQ_TYPE_NONE);
+	else
+		enable_irq(cd->irq);
 
 	return 0;
 }
@@ -141,19 +165,52 @@ static void __init cevt_init(struct device_node *np)
 					  CLOCK_EVT_FEAT_C3STOP |
 					  CLOCK_EVT_FEAT_PERCPU;
 		cd->set_next_event	= cevt_set_next_event;
-		cd->irq			= econet_timer.irq;
 		cd->cpumask		= cpumask_of(i);
 		cd->name		= np->name;
 
-		cevt_dev_init(i);
+		/*
+		 * If not enough IRQs were specified for the CPUs, leave IRQ -1
+		 * and error in cevt_init_cpu
+		 */
+		if (econet_timer.is_percpu)
+			cd->irq = econet_timer.irqs[0];
+		else if (i < econet_timer.num_irqs)
+			cd->irq = econet_timer.irqs[i];
+		else
+			cd->irq = -1;
+
+		/*
+		 * Tolerate CPUs that could exist but don't.
+		 * Fail in cevt_init_cpu when they try to start.
+		 */
+		if (reg_ctl(i) && cd->irq > -1)
+			cevt_dev_init(i);
 	}
 }
 
 static int __init timer_init(struct device_node *np)
 {
-	int num_blocks = DIV_ROUND_UP(num_possible_cpus(), 2);
+	int num_blocks = of_address_count(np);
+	int num_irqs = of_irq_count(np);
 	struct clk *clk;
-	int ret;
+	int ret, i;
+
+	econet_timer.is_percpu = of_device_is_compatible(np, "econet,en751221-timer");
+
+	if (econet_timer.is_percpu && num_irqs != 1) {
+		pr_err("%pOFn: EN751221 clock must have 1 IRQ not %d\n", np,
+		       num_irqs);
+		return -EINVAL;
+	}
+	if (num_blocks > ARRAY_SIZE(econet_timer.membase)) {
+		pr_err("%pOFn: Too many regs: max %zd got %d\n", np,
+		       ARRAY_SIZE(econet_timer.membase), num_blocks);
+		return -EINVAL;
+	}
+	if (num_blocks == 0) {
+		pr_err("%pOFn: At least one reg block must be provided\n", np);
+		return -EINVAL;
+	}
 
 	clk = of_clk_get(np, 0);
 	if (IS_ERR(clk)) {
@@ -164,7 +221,13 @@ static int __init timer_init(struct device_node *np)
 	econet_timer.freq_hz = clk_get_rate(clk);
 	clk_put(clk);
 
-	for (int i = 0; i < num_blocks; i++) {
+	econet_timer.num_irqs = num_irqs;
+	econet_timer.irqs = kcalloc(num_irqs, sizeof(*econet_timer.irqs),
+				    GFP_KERNEL);
+	if (!econet_timer.irqs)
+		return -ENOMEM;
+
+	for (i = 0; i < num_blocks; i++) {
 		econet_timer.membase[i] = of_iomap(np, i);
 		if (!econet_timer.membase[i]) {
 			pr_err("%pOFn: failed to map register [%d]\n", np, i);
@@ -173,22 +236,32 @@ static int __init timer_init(struct device_node *np)
 		}
 	}
 
-	econet_timer.irq = irq_of_parse_and_map(np, 0);
-	if (econet_timer.irq <= 0) {
-		pr_err("%pOFn: irq_of_parse_and_map failed\n", np);
-		ret = -EINVAL;
-		goto out_membase;
+	for (i = 0; i < num_irqs; i++) {
+		econet_timer.irqs[i] = irq_of_parse_and_map(np, i);
+		if (econet_timer.irqs[i] <= 0) {
+			pr_err("%pOFn: failed mapping irq %d\n", np, i);
+			ret = -EINVAL;
+			goto out_irq_mapping;
+		}
 	}
 
-	irq_set_status_flags(econet_timer.irq, IRQ_NOAUTOEN);
-
-	ret = request_percpu_irq(econet_timer.irq, cevt_interrupt, np->name,
-				 &econet_timer_pcpu);
-
-	if (ret < 0) {
-		pr_err("%pOFn: IRQ %d setup failed (%d)\n", np,
-		       econet_timer.irq, ret);
-		goto out_irq_mapping;
+	for (i = 0; i < num_irqs; i++) {
+		irq_set_status_flags(econet_timer.irqs[i], IRQ_NOAUTOEN);
+
+		if (econet_timer.is_percpu)
+			ret = request_percpu_irq(econet_timer.irqs[i],
+						 cevt_interrupt, np->name,
+						 &econet_timer_pcpu);
+		else
+			ret = request_irq(econet_timer.irqs[i], cevt_interrupt,
+					  IRQF_TIMER | IRQF_NOBALANCING,
+					  np->name, NULL);
+
+		if (ret < 0) {
+			pr_err("%pOFn: IRQ %d setup failed: %pe\n", np,
+			       i, ERR_PTR(ret));
+			goto out_irq_free;
+		}
 	}
 
 	cevt_init(np);
@@ -220,9 +293,17 @@ static int __init timer_init(struct device_node *np)
 	return 0;
 
 out_irq_free:
-	free_percpu_irq(econet_timer.irq, &econet_timer_pcpu);
+	while (--i >= 0) {
+		if (econet_timer.is_percpu)
+			free_percpu_irq(econet_timer.irqs[i], &econet_timer_pcpu);
+		else
+			free_irq(econet_timer.irqs[i], NULL);
+	}
 out_irq_mapping:
-	irq_dispose_mapping(econet_timer.irq);
+	for (i = 0; i < num_irqs; i++) {
+		if (econet_timer.irqs[i] > 0)
+			irq_dispose_mapping(econet_timer.irqs[i]);
+	}
 out_membase:
 	for (int i = 0; i < ARRAY_SIZE(econet_timer.membase); i++) {
 		if (econet_timer.membase[i]) {
@@ -231,7 +312,11 @@ static int __init timer_init(struct device_node *np)
 		}
 	}
 
+	kfree(econet_timer.irqs);
+	econet_timer.irqs = NULL;
+
 	return ret;
 }
 
 TIMER_OF_DECLARE(econet_timer_hpt, "econet,en751221-timer", timer_init);
+TIMER_OF_DECLARE(econet_timer_en751627, "econet,en751627-timer", timer_init);
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

PATCH v12 5/5 clocksource/timer-econet-en751221: Do not use NR_CPUS for resource array size

In reply to Caleb James DeLisle

Each pair of VPEs has one timer block, so the number of blocks is half the number of CPUs on the hardware. However, since NR_CPUS is a configuration, it is possible that it will be less than the number of actual CPUs on the hardware which will cause the array to be too small for the specified DT.

Switch to an allocated buffer for storing the iomem resources, as is done for the IRQs.

---
 drivers/clocksource/timer-econet-en751221.c | 29 ++++++++++++++-------
 1 file changed, 20 insertions(+), 9 deletions(-)
drivers/clocksource/timer-econet-en751221.c +20 -9
index edb3db3125ce..9f28cc238372 100644
@@ -19,11 +19,10 @@
 #define ECONET_BITS			32
 #define ECONET_MIN_DELTA		0x00001000
 #define ECONET_MAX_DELTA		GENMASK(ECONET_BITS - 2, 0)
-/* 34Kc hardware has 1 block and 1004Kc has 2. */
-#define ECONET_NUM_BLOCKS		DIV_ROUND_UP(NR_CPUS, 2)
 
 static struct {
-	void __iomem	*membase[ECONET_NUM_BLOCKS];
+	void __iomem	**membase;
+	int		num_membase;
 	int		*irqs;
 	int		num_irqs;
 	bool		is_percpu;
@@ -37,6 +36,10 @@ static DEFINE_PER_CPU(struct clock_event_device, econet_timer_pcpu);
  */
 static inline void __iomem *reg_ctl(u32 timer_n)
 {
+	/* reg_ctl() is used to check if a clock exists */
+	if (timer_n >> 1 >= econet_timer.num_membase)
+		return NULL;
+
 	return econet_timer.membase[timer_n >> 1];
 }
 
@@ -202,11 +205,6 @@ static int __init timer_init(struct device_node *np)
 		       num_irqs);
 		return -EINVAL;
 	}
-	if (num_blocks > ARRAY_SIZE(econet_timer.membase)) {
-		pr_err("%pOFn: Too many regs: max %zd got %d\n", np,
-		       ARRAY_SIZE(econet_timer.membase), num_blocks);
-		return -EINVAL;
-	}
 	if (num_blocks == 0) {
 		pr_err("%pOFn: At least one reg block must be provided\n", np);
 		return -EINVAL;
@@ -227,6 +225,15 @@ static int __init timer_init(struct device_node *np)
 	if (!econet_timer.irqs)
 		return -ENOMEM;
 
+	econet_timer.num_membase = num_blocks;
+	econet_timer.membase = kcalloc(num_blocks,
+				       sizeof(*econet_timer.membase),
+				       GFP_KERNEL);
+	if (!econet_timer.membase) {
+		ret = -ENOMEM;
+		goto out_kfree_irqs;
+	}
+
 	for (i = 0; i < num_blocks; i++) {
 		econet_timer.membase[i] = of_iomap(np, i);
 		if (!econet_timer.membase[i]) {
@@ -305,13 +312,17 @@ static int __init timer_init(struct device_node *np)
 			irq_dispose_mapping(econet_timer.irqs[i]);
 	}
 out_membase:
-	for (int i = 0; i < ARRAY_SIZE(econet_timer.membase); i++) {
+	for (int i = 0; i < num_blocks; i++) {
 		if (econet_timer.membase[i]) {
 			iounmap(econet_timer.membase[i]);
 			econet_timer.membase[i] = NULL;
 		}
 	}
 
+	kfree(econet_timer.membase);
+	econet_timer.membase = NULL;
+
+out_kfree_irqs:
 	kfree(econet_timer.irqs);
 	econet_timer.irqs = NULL;
 
Signature · 1 line
2.39.5