MIPS: GIC clocksource/irqchip improvements and fixes for multi-cluster systems
Subsystems (4)
v3 cover MIPS: GIC clocksource/irqchip improvements and fixes for multi-cluster systems
This series addresses multi-cluster MIPS GIC handling: it fixes a few cross-cluster and lock bugs, and allows the GIC timer to be used as a fast local clocksource on multi-cluster systems by synchronizing the per-cluster counters.
If you are wondering how a single patch in version one turned into a five-patch series in version two, the answser is PROVE_LOCKING which found some issues that this series addresses.
The first three patches are for the irq-mips-gic irqchip driver.
The first patch fixes the for_each_online_cpu_gic() macro which was releasing an unheld per-CPU cm_core_lock on every invocation on multi-cluster hardware. Move both the acquire and release into __gic_with_next_online_cpu() so they stay balanced and the for_each macro accesses all VP on a multi-cluster system.
The second patch fixes a recursive acquisition of gic_lock in gic_set_affinity() when moving interrupt SMP affinity across clusters, caused by a call to gic_set_type(). Split out a gic_set_type_locked() helper to resolve the issue.
The third patch enables the interrupt when moving affinity across clusters. The interrupt mask is per-cluster, so after a cross-cluster affinity change the interrupt was left disabled in the destination cluster. Handle the mask explicitly on both the old and the new side.
The last two patches are for the mips-gic-timer clocksource driver.
The fourth patch is what changed from version 2 of the series. It simplifies gic_next_event() as it is guaranteed to be invoked on the same CPU of the event. And the flag CLOCK_EVT_FEAT_PERCPU is added to better document that guarantee.
The last patch is the main functional change, carried over from v1. It synchronizes each secondary cluster's GIC counter to cluster 0's counter as its CPUs come online, and once every cluster is in sync, promote the clocksource back from the slow cross-cluster gic_hpt_read_multicluster() path to the fast local gic_hpt_read(), also re-enabling the GIC VDSO clock mode. On the dual-cluster Mobileye EyeQ6H SoC this makes clock_gettime(CLOCK_MONOTONIC) about four times faster on the secondary cluster and replaces the jiffies-based sched_clock with the high-precision GIC counter.
This series was tested on Mobileye SoCs with different cluster configurations:
* The EyeQ5 with two clusters but only one is equipped with VP (VP topology {2,2,2,2},{} total 8).
* The EyeQ6Lplus which is strictly single cluster (VP topology {4,4} total 8).
* The EyeQ6H with two identical clusters
(VP topology {4,4,4,4},{4,4,4,4} total 32).
Although I am posting this as a single series since the patches are all related to the GIC, the changes to the irqchip driver and to the clocksource driver are independent from each other and can be merged in different trees. I can split the series in two if it helps reviewing/merging.
---
Changes in v3:
- Rebased on v7.3-rc1.
- Simplified gic_next_event() as it is strictly per-CPU instead of fixing
unused code.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Four new patches described above fixing multi-cluster
handling in the GIC irqchip and clocksource drivers.
- In the last patch, gic_clocksource_promote() is now deferred via a
workqueue instead of a CPUHP ONLINE callback, to avoid a lockdep
issue on clocksource_mutex.
- Link to v1: https://patch.msgid.link/[email protected]
To: Thomas Bogendoerfer <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Dragan Mladjenovic <[email protected]>
To: Chao-ying Fu <[email protected]>
To: Aleksandar Rikalo <[email protected]>
To: Paul Burton <[email protected]>
To: Daniel Lezcano <[email protected]>
To: Radu Rendec <[email protected]>
Cc: Thomas Petazzoni <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
Benoît Monin (5):
irqchip/mips-gic: Fix unbalanced cm_core_lock in for_each_online_cpu_gic() irqchip/mips-gic: Fix recursive acquisition of gic_lock in gic_set_affinity() irqchip/mips-gic: Enable interrupt when moving affinity across clusters
clocksource: mips-gic-timer: Simplify gic_next_event() for per-cpu timer
clocksource: mips-gic-timer: Use local counter on synced multi-cluster systems
drivers/clocksource/mips-gic-timer.c | 127 +++++++++++++++++++++++++++++++---- drivers/irqchip/irq-mips-gic.c | 51 ++++++++------ 2 files changed, 144 insertions(+), 34 deletions(-)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260717-sync-gic-counters-1cd6b40b968e
Best regards,
Signature · 3 lines
Benoît Monin, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
PATCH v3 1/5 irqchip/mips-gic: Fix unbalanced cm_core_lock in for_each_online_cpu_gic()
Commit d9e2ed610a60 ("irqchip/mips-gic: Support multi-cluster in for_each_online_cpu_gic()") added a gic_unlock_cluster() call to the macro's loop increment, which unconditionally invokes mips_cm_unlock_other() on multi-cluster systems. However nothing in the loop ever acquires the corresponding mips_cm_lock_other(), so on multi-cluster hardware every invocation of for_each_online_cpu_gic() releases an unheld per-CPU cm_core_lock.
With CONFIG_PROVE_LOCKING this triggers a "bad unlock balance detected" warning at boot, e.g. from gic_irq_domain_map() while mapping local interrupts. Only the first occurrence is reported, since the first warning permanently disables lockdep (debug_locks = 0); the unbalanced release itself silently persists.
Fix this by moving both the acquire and release into __gic_with_next_online_cpu() so they stay balanced. When advancing to a CPU in a remote cluster, lock the CM redirect block for that cluster via mips_cm_lock_other(); when leaving a remote cluster (or finishing the iteration) release it with mips_cm_unlock_other(). Local-cluster CPUs require no locking, so single-cluster systems are unaffected. This also makes the redirect region behave correctly when accessing local register blocks of CPUs in other clusters.
Drop the now-unused gic_unlock_cluster() helper and its call from the for_each_online_cpu_gic() increment.
--- drivers/irqchip/irq-mips-gic.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
drivers/irqchip/irq-mips-gic.c +6 -14
@@ -70,6 +70,10 @@ static int __gic_with_next_online_cpu(int prev) {unsigned int cpu; +/* Release the redirect/other region lock to the previous CPU, if any. */ +if (prev >=0 ) +mips_cm_unlock_other (); +/* Discover the next online CPU */ cpu =cpumask_next (prev, cpu_online_mask);
@@ -77,23 +81,12 @@ static int __gic_with_next_online_cpu(int prev)if (cpu >= nr_cpu_ids)return cpu; -/* -* Move the access lock to the next CPU's GIC local register block. -* -* Set GIC_VL_OTHER. Since the caller holds gic_lock nothing can -* clobber the written value. -*/ -write_gic_vl_other (mips_cm_vp_id (cpu)); +/* Lock access to redirect/other region to the next CPU */ +mips_cm_lock_other_cpu (cpu, CM_GCR_Cx_OTHER_BLOCK_LOCAL);return cpu; } -static inline void gic_unlock_cluster (void ) -{ -if (mips_cps_multicluster_cpus ()) -mips_cm_unlock_other (); -} -/** * for_each_online_cpu_gic() - Iterate over online CPUs, access local registers * @cpu: An integer variable to hold the current CPU number
@@ -108,7 +101,6 @@ static inline void gic_unlock_cluster(void)guard (raw_spinlock_irqsave)(gic_lock); \for ((cpu) =__gic_with_next_online_cpu (-1 ); \ (cpu) < nr_cpu_ids; \ -gic_unlock_cluster (), \ (cpu) =__gic_with_next_online_cpu (cpu))/**
Signature · 1 line
2.55.0
Re: [PATCH v3 1/5] irqchip/mips-gic: Fix unbalanced cm_core_lock in for_each_online_cpu_gic()
Quoted 84 lines · Benoît Monin
Commit
d9e2ed610a60("irqchip/mips-gic: Support multi-cluster infor_each_online_cpu_gic()") added agic_unlock_cluster()call to the macro's loop increment, which unconditionally invokesmips_cm_unlock_other()on multi-cluster systems. However nothing in the loop ever acquires the correspondingmips_cm_lock_other(), so on multi-cluster hardware every invocation offor_each_online_cpu_gic()releases an unheld per-CPUcm_core_lock.With
CONFIG_PROVE_LOCKINGthis triggers a "bad unlock balance detected" warning at boot, e.g. fromgic_irq_domain_map()while mapping local interrupts. Only the first occurrence is reported, since the first warning permanently disables lockdep (debug_locks= 0); the unbalanced release itself silently persists.Fix this by moving both the acquire and release into
__gic_with_next_online_cpu()so they stay balanced. When advancing to a CPU in a remote cluster, lock the CM redirect block for that cluster viamips_cm_lock_other(); when leaving a remote cluster (or finishing the iteration) release it withmips_cm_unlock_other(). Local-cluster CPUs require no locking, so single-cluster systems are unaffected. This also makes the redirect region behave correctly when accessing local register blocks of CPUs in other clusters.Drop the now-unused
gic_unlock_cluster()helper and its call from thefor_each_online_cpu_gic()increment.Fixes d9e2ed610a60 ("irqchip/mips-gic: Support multi-cluster in for_each_online_cpu_gic()")Signed-off-by Benoît Monin <[email protected]>--- drivers/irqchip/irq-mips-gic.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)drivers/irqchip/irq-mips-gic.c +6
-14@@ -70,6 +70,10 @@ static int __gic_with_next_online_cpu(int prev) {unsigned int cpu; +/* Release the redirect/other region lock to the previous CPU, if any. */ +if (prev >=0 ) +mips_cm_unlock_other (); +/* Discover the next online CPU */ cpu =cpumask_next (prev, cpu_online_mask);@@ -77,23 +81,12 @@ static int __gic_with_next_online_cpu(int prev)if (cpu >= nr_cpu_ids)return cpu; -/* -* Move the access lock to the next CPU's GIC local register block. -* -* Set GIC_VL_OTHER. Since the caller holds gic_lock nothing can -* clobber the written value. -*/ -write_gic_vl_other (mips_cm_vp_id (cpu)); +/* Lock access to redirect/other region to the next CPU */ +mips_cm_lock_other_cpu (cpu, CM_GCR_Cx_OTHER_BLOCK_LOCAL);return cpu; } -static inline void gic_unlock_cluster (void ) -{ -if (mips_cps_multicluster_cpus ()) -mips_cm_unlock_other (); -} -/** * for_each_online_cpu_gic() - Iterate over online CPUs, access local registers * @cpu: An integer variable to hold the current CPU number @@ -108,7 +101,6 @@ static inline void gic_unlock_cluster(void)guard (raw_spinlock_irqsave)(gic_lock); \for ((cpu) =__gic_with_next_online_cpu (-1 ); \ (cpu) < nr_cpu_ids; \ -gic_unlock_cluster (), \ (cpu) =__gic_with_next_online_cpu (cpu))/** Signature · 2 lines
-- 2.55.0
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
PATCH v3 2/5 irqchip/mips-gic: Fix recursive acquisition of gic_lock in gic_set_affinity()
In reply to Benoît MoninCommit 322a90638768 ("irqchip/mips-gic: Multi-cluster support") made gic_set_affinity() call gic_set_type() when an interrupt's affinity is moved between clusters. gic_set_type() takes gic_lock with raw_spin_lock_irqsave(), but gic_set_affinity() holds gic_lock at that point, so the same raw spinlock is acquired twice, triggering a deadlock.
Split gic_set_type() into a gic_set_type_locked() helper that assumes gic_lock is already held. gic_set_type() takes gic_lock and calls the helper, and gic_set_affinity() calls gic_set_type_locked() directly, since it already holds gic_lock.
--- drivers/irqchip/irq-mips-gic.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/irqchip/irq-mips-gic.c +10 -5
@@ -297,14 +297,14 @@ static void gic_ack_irq(struct irq_data *d) } } -static int gic_set_type (struct irq_data *d,unsigned int type) +static int gic_set_type_locked (struct irq_data *d,unsigned int type) {unsigned int irq, pol, trig, dual; -unsigned long flags; + +lockdep_assert_held (&gic_lock); irq =GIC_HWIRQ_TO_SHARED (d->hwirq); -raw_spin_lock_irqsave (&gic_lock, flags);switch (type &IRQ_TYPE_SENSE_MASK ) {case IRQ_TYPE_EDGE_FALLING : pol =GIC_POL_FALLING_EDGE ;
@@ -351,11 +351,16 @@ static int gic_set_type(struct irq_data *d, unsigned int type)else irq_set_chip_handler_name_locked (d, &gic_level_irq_controller, handle_level_irq,NULL ); -raw_spin_unlock_irqrestore (&gic_lock, flags);return 0 ; } +static int gic_set_type (struct irq_data *d,unsigned int type) +{ +guard (raw_spinlock_irqsave)(&gic_lock); +return gic_set_type_locked (d, type); +} +#ifdef CONFIG_SMP static int gic_set_affinity (struct irq_data *d,const struct cpumask *cpumask,bool force)
@@ -407,7 +412,7 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,* trigger type in the new cluster. */ if (cl != old_cl) -gic_set_type (d,irqd_get_trigger_type (d)); +gic_set_type_locked (d,irqd_get_trigger_type (d));/* Route the interrupt to its new VP(E) */ if (gic_irq_lock_cluster (d)) {
Signature · 1 line
2.55.0
Re: [PATCH v3 2/5] irqchip/mips-gic: Fix recursive acquisition of gic_lock in gic_set_affinity()
Quoted 69 lines · Benoît Monin
Commit
322a90638768("irqchip/mips-gic: Multi-cluster support") madegic_set_affinity()callgic_set_type()when an interrupt's affinity is moved between clusters.gic_set_type()takesgic_lockwithraw_spin_lock_irqsave(), butgic_set_affinity()holdsgic_lockat that point, so the same raw spinlock is acquired twice, triggering a deadlock.Split
gic_set_type()into agic_set_type_locked()helper that assumesgic_lockis already held.gic_set_type()takesgic_lockand calls the helper, andgic_set_affinity()callsgic_set_type_locked()directly, since it already holdsgic_lock.Fixes 322a90638768 ("irqchip/mips-gic: Multi-cluster support")Signed-off-by Benoît Monin <[email protected]>--- drivers/irqchip/irq-mips-gic.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)drivers/irqchip/irq-mips-gic.c +10
-5@@ -297,14 +297,14 @@ static void gic_ack_irq(struct irq_data *d) } } -static int gic_set_type (struct irq_data *d,unsigned int type) +static int gic_set_type_locked (struct irq_data *d,unsigned int type) {unsigned int irq, pol, trig, dual; -unsigned long flags; + +lockdep_assert_held (&gic_lock); irq =GIC_HWIRQ_TO_SHARED (d->hwirq); -raw_spin_lock_irqsave (&gic_lock, flags);switch (type &IRQ_TYPE_SENSE_MASK ) {case IRQ_TYPE_EDGE_FALLING : pol =GIC_POL_FALLING_EDGE ;@@ -351,11 +351,16 @@ static int gic_set_type(struct irq_data *d, unsigned int type)else irq_set_chip_handler_name_locked (d, &gic_level_irq_controller, handle_level_irq,NULL ); -raw_spin_unlock_irqrestore (&gic_lock, flags);return 0 ; } +static int gic_set_type (struct irq_data *d,unsigned int type) +{ +guard (raw_spinlock_irqsave)(&gic_lock); +return gic_set_type_locked (d, type); +} +#ifdef CONFIG_SMP static int gic_set_affinity (struct irq_data *d,const struct cpumask *cpumask,bool force)@@ -407,7 +412,7 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,* trigger type in the new cluster. */ if (cl != old_cl) -gic_set_type (d,irqd_get_trigger_type (d)); +gic_set_type_locked (d,irqd_get_trigger_type (d));/* Route the interrupt to its new VP(E) */ if (gic_irq_lock_cluster (d)) {Signature · 2 lines
-- 2.55.0
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
PATCH v3 3/5 irqchip/mips-gic: Enable interrupt when moving affinity across clusters
In reply to Benoît MoninWhen an interrupt's affinity is moved to a CPU in another cluster, gic_set_affinity() updates the routing (GIC_SH_MAP_VP) and trigger type in the destination cluster, but never touched the interrupt's mask state.
The interrupt mask is per-cluster. After such a move the interrupt was left disabled in the destination cluster, so it never fires despite being correctly routed to its new VP.
Handle the mask explicitly on a cross-cluster affinity change: in the old cluster, write GIC_SH_RMASK to disable the interrupt while clearing the route so it is no longer delivered. And in the new cluster, set the mask to enable the interrupt along with reconfiguring the trigger type.
--- drivers/irqchip/irq-mips-gic.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/irqchip/irq-mips-gic.c +13 -3
@@ -390,14 +390,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, stop routing the -* interrupt to any VP(E) in the old cluster. +* interrupt to any VP(E) in the old cluster and disable +* the interrupt in that cluster. */ if (cl != old_cl) {if (gic_irq_lock_cluster (d)) {write_gic_redir_map_vp (irq,0 ); +write_gic_redir_rmask (irq);mips_cm_unlock_other (); }else {write_gic_map_vp (irq,0 ); +write_gic_rmask (irq); } }
@@ -409,10 +412,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, configure the interrupt -* trigger type in the new cluster. +* trigger type and enable the interrupt in the new cluster. */ -if (cl != old_cl) +if (cl != old_cl) {gic_set_type_locked (d,irqd_get_trigger_type (d)); +if (gic_irq_lock_cluster (d)) { +write_gic_redir_smask (irq); +mips_cm_unlock_other (); + }else { +write_gic_smask (irq); + } + }/* Route the interrupt to its new VP(E) */ if (gic_irq_lock_cluster (d)) {
Signature · 1 line
2.55.0
Thomas Bogendoerfer
maintainerRe: [PATCH v3 3/5] irqchip/mips-gic: Enable interrupt when moving affinity across clusters
Quoted 49 lines · Benoît Monin
When an interrupt's affinity is moved to a CPU in another cluster,
gic_set_affinity()updates the routing (GIC_SH_MAP_VP) and trigger type in the destination cluster, but never touched the interrupt's mask state.The interrupt mask is per-cluster. After such a move the interrupt was left disabled in the destination cluster, so it never fires despite being correctly routed to its new VP.
Handle the mask explicitly on a cross-cluster affinity change: in the old cluster, write
GIC_SH_RMASKto disable the interrupt while clearing the route so it is no longer delivered. And in the new cluster, set the mask to enable the interrupt along with reconfiguring the trigger type.Fixes 322a90638768 ("irqchip/mips-gic: Multi-cluster support")Signed-off-by Benoît Monin <[email protected]>--- drivers/irqchip/irq-mips-gic.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)drivers/irqchip/irq-mips-gic.c +5
-2@@ -390,14 +390,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, stop routing the -* interrupt to any VP(E) in the old cluster. +* interrupt to any VP(E) in the old cluster and disable +* the interrupt in that cluster. */ if (cl != old_cl) {if (gic_irq_lock_cluster (d)) {write_gic_redir_map_vp (irq,0 ); +write_gic_redir_rmask (irq);mips_cm_unlock_other (); }else {write_gic_map_vp (irq,0 ); +write_gic_rmask (irq); } }@@ -409,10 +412,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, configure the interrupt -* trigger type in the new cluster. +* trigger type and enable the interrupt in the new cluster. */
drivers/irqchip/irq-mips-gic.c static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask, · line 425
Quoted 10 lines
drivers/irqchip/irq-mips-gic.c +8
-1-if (cl != old_cl) +if (cl != old_cl) {gic_set_type_locked (d,irqd_get_trigger_type (d)); +if (gic_irq_lock_cluster (d)) { +write_gic_redir_smask (irq); +mips_cm_unlock_other (); + }else { +write_gic_smask (irq); + } + }
shouldn't this be done depending on the mask state in the old cluster ?
Thomas.
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
Re: [PATCH v3 3/5] irqchip/mips-gic: Enable interrupt when moving affinity across clusters
Quoted 37 lines · Thomas Bogendoerfer
Quoted 37 lines
drivers/irqchip/irq-mips-gic.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)drivers/irqchip/irq-mips-gic.c +7
-3@@ -390,14 +390,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, stop routing the -* interrupt to any VP(E) in the old cluster. +* interrupt to any VP(E) in the old cluster and disable +* the interrupt in that cluster. */ if (cl != old_cl) {if (gic_irq_lock_cluster (d)) {write_gic_redir_map_vp (irq,0 ); +write_gic_redir_rmask (irq);mips_cm_unlock_other (); }else {write_gic_map_vp (irq,0 ); +write_gic_rmask (irq); } }@@ -409,10 +412,17 @@ static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,/* * If we're moving affinity between clusters, configure the interrupt -* trigger type in the new cluster. +* trigger type and enable the interrupt in the new cluster. */ -if (cl != old_cl) +if (cl != old_cl) {gic_set_type_locked (d,irqd_get_trigger_type (d)); +if (gic_irq_lock_cluster (d)) {
Quoted 8 lines
drivers/irqchip/irq-mips-gic.c static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask, · line 425
Quoted 6 lines
drivers/irqchip/irq-mips-gic.c +6
-0+write_gic_redir_smask (irq); +mips_cm_unlock_other (); + }else { +write_gic_smask (irq); + } + }shouldn't this be done depending on the mask state in the old cluster ?
Good point, the interrupt could be disabled when changing its affinity. I'll fix that.
Best regards,
Signature · 3 lines
Benoît Monin, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
PATCH v3 4/5 clocksource: mips-gic-timer: Simplify gic_next_event() for per-cpu timer
In reply to Benoît MoninThe GIC clock event device is registered per CPU and is flagged with CLOCK_EVT_FEAT_C3STOP. This guarantees that the set_next_event() callback is always invoked on the CPU of the event.
Drop the code path configuring the compare register of another CPU as it is never called and does not correctly handle the case of having multiple clusters, and document this guarantee by adding the CLOCK_EVT_FEAT_PERCPU flag to the clock event device features.
--- drivers/clocksource/mips-gic-timer.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/clocksource/mips-gic-timer.c +3 -7
@@ -52,18 +52,13 @@ static u64 notrace gic_read_count(void)static int gic_next_event (unsigned long delta,struct clock_event_device *evt) { -int cpu =cpumask_first (evt->cpumask);u64 cnt;int res; cnt =gic_read_count (); cnt += (u64 )delta; -if (cpu ==raw_smp_processor_id ()) { -write_gic_vl_compare (cnt); - }else { -write_gic_vl_other (mips_cm_vp_id (cpu)); -write_gic_vo_compare (cnt); - } +write_gic_vl_compare (cnt); + res = ((int )(gic_read_count () - cnt) >=0 ) ? -ETIME :0 ;return res; }
@@ -82,6 +77,7 @@ static void gic_clockevent_cpu_init(unsigned int cpu, { cd->name ="MIPS GIC" ; cd->features =CLOCK_EVT_FEAT_ONESHOT | +CLOCK_EVT_FEAT_PERCPU |CLOCK_EVT_FEAT_C3STOP ; cd->rating =350 ;
Signature · 1 line
2.55.0
Re: [PATCH v3 4/5] clocksource: mips-gic-timer: Simplify gic_next_event() for per-cpu timer
Quoted 50 lines · Benoît Monin
The GIC clock event device is registered per CPU and is flagged with
CLOCK_EVT_FEAT_C3STOP. This guarantees that theset_next_event()callback is always invoked on the CPU of the event.Drop the code path configuring the compare register of another CPU as it is never called and does not correctly handle the case of having multiple clusters, and document this guarantee by adding the
CLOCK_EVT_FEAT_PERCPUflag to the clock event device features.Signed-off-by Benoît Monin <[email protected]>--- drivers/clocksource/mips-gic-timer.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)drivers/clocksource/mips-gic-timer.c +3
-7@@ -52,18 +52,13 @@ static u64 notrace gic_read_count(void)static int gic_next_event (unsigned long delta,struct clock_event_device *evt) { -int cpu =cpumask_first (evt->cpumask);u64 cnt;int res; cnt =gic_read_count (); cnt += (u64 )delta; -if (cpu ==raw_smp_processor_id ()) { -write_gic_vl_compare (cnt); - }else { -write_gic_vl_other (mips_cm_vp_id (cpu)); -write_gic_vo_compare (cnt); - } +write_gic_vl_compare (cnt); + res = ((int )(gic_read_count () - cnt) >=0 ) ? -ETIME :0 ;return res; }@@ -82,6 +77,7 @@ static void gic_clockevent_cpu_init(unsigned int cpu, { cd->name ="MIPS GIC" ; cd->features =CLOCK_EVT_FEAT_ONESHOT | +CLOCK_EVT_FEAT_PERCPU |CLOCK_EVT_FEAT_C3STOP ; cd->rating =350 ;Signature · 2 lines
-- 2.55.0
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
PATCH v3 5/5 clocksource: mips-gic-timer: Use local counter on synced multi-cluster systems
In reply to Benoît MoninIn a multi-cluster MIPS system there is one GIC per cluster, each with its own independent counter. These counters are not synchronized in hardware and can drift relative to one another, which is why multi- cluster systems currently fall back to gic_hpt_read_multicluster():
every clocksource read is redirected to cluster 0's counter via a cross-cluster register access.
Instead, actively synchronize the counter of each secondary cluster to cluster 0's counter as its CPUs come online. When the first CPU of a cluster starts and the GIC counter is stopped, gic_sync_counter_64() is used to align the local counter with cluster 0 on systems using 64-bit CM accesses:
- the local counter is stopped, loaded with cluster 0's counter value
plus an accumulated offset, and restarted;
- the alignment is checked by reading the local counter, cluster 0's counter and the local counter again (t0, t1, t2). If t1 lies between t0 and t2 the two counters are considered in sync;
- otherwise the offset is refined by half of the measured error to compensate for the cross-cluster access latency, and the process is retried.
After a short delay the alignment is re-checked to confirm the counters have not drifted apart, and only then is the cluster recorded as synchronized in gic_synced_cl_map. Clusters with no cores and cluster 0 itself are marked synchronized up-front.
Once every cluster is synchronized, gic_clocksource_promote() switches the clocksource back from gic_hpt_read_multicluster() to the fast local gic_hpt_read(), re-registers it, and re-enables the GIC VDSO clock mode. It also registers the GIC counter as the sched_clock. Systems where synchronization cannot be achieved keep using the safe cross-cluster read path.
Since gic_clocksource_promote() calls clocksource_unregister() and clocksource_register_hz() which internally use a mutex, it cannot be called directly from the CPU hotplug STARTING callback because interrupts are disabled. Instead the call is deferred to a workqueue.
gic_clocksource_promote() also registers the local GIC counter as the sched clock via sched_clock_register(). This is valid from a non-__init context because sched_clock_register() lost its __init marker in commit 84b1a903aed8 ("time/sched_clock: Export symbol for sched_clock register function").
Note that the clocksource is only promoted once when all clusters are first online and all GIC counters are in sync. It is assumed that even if a cluster is fully powered-off then on later, gic_sync_counter_64() will be able to synchronize it once again. Said differently: there is no support to "demote" the clocksource.
On the dual-cluster Mobileye EyeQ6H SoC, this allows four times faster clock_gettime(CLOCK_MONOTONIC) and a much higher precision sched_clock instead of jiffies.
--- drivers/clocksource/mips-gic-timer.c | 117 +++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 5 deletions(-)
drivers/clocksource/mips-gic-timer.c +112 -5
@@ -6,6 +6,7 @@#include <linux/clk.h> #include <linux/clockchips.h> #include <linux/cpu.h> +#include <linux/delay.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/notifier.h>
@@ -21,6 +22,8 @@ static int gic_timer_irq;static unsigned int gic_frequency;static unsigned int gic_count_width;static bool __read_mostly gic_clock_unstable; +static unsigned long *gic_synced_cl_map; +static struct work_struct gic_promote_work;static void gic_clocksource_unstable (char *reason);
@@ -102,10 +105,71 @@ static void gic_update_frequency(void *data)clockevents_update_freq (this_cpu_ptr (&gic_clockevent_device), rate); } +/* Number of iterations to synchronize the local GIC counter */ +#define GIC_SYNC_ITERATIONS 4 + +/* Delay in us to check if the local GIC counter is still in sync with cluster 0 */ +#define GIC_SYNC_CHECK_DELAY 100 + +static void gic_sync_counter_64 (unsigned int cluster) +{ +unsigned int config =read_gic_config (); +u64 t0, t1, t2; +s64 offset =0 ; + +mips_cm_lock_other (0 ,0 ,0 , CM_GCR_Cx_OTHER_BLOCK_GLOBAL); + +for (int i =0 ; i <GIC_SYNC_ITERATIONS ; i++) { +write_gic_config (config |GIC_CONFIG_COUNTSTOP ); +write_gic_counter (read_gic_redir_counter () + offset); +write_gic_config (config & ~GIC_CONFIG_COUNTSTOP ); + + t0 =read_gic_counter (); + t1 =read_gic_redir_counter (); + t2 =read_gic_counter (); + +if (time_in_range64 (t1, t0, t2)) +break ; + +/* +* Compute the offset to apply to the local counter +* so that (t1 - t0) equals (t2 - t1). +*/ + offset += (s64)(2 * t1 - t0 - t2) /2 ; + } + +mips_cm_unlock_other (); + +if (!time_in_range64 (t1, t0, t2)) +return ; + +udelay (GIC_SYNC_CHECK_DELAY ); + +mips_cm_lock_other (0 ,0 ,0 , CM_GCR_Cx_OTHER_BLOCK_GLOBAL); + t0 =read_gic_counter (); + t1 =read_gic_redir_counter (); + t2 =read_gic_counter (); +mips_cm_unlock_other (); + +/* If so, mark the cluster as synchronized */ +if (time_in_range64 (t1, t0, t2) && gic_synced_cl_map) +bitmap_set (gic_synced_cl_map, cluster,1 ); +} +static int gic_starting_cpu (unsigned int cpu) { -/* Ensure the GIC counter is running */ -clear_gic_config (GIC_CONFIG_COUNTSTOP ); +unsigned int cluster =cpu_cluster (&cpu_data[cpu]); + +if (read_gic_config () &GIC_CONFIG_COUNTSTOP ) { +clear_gic_config (GIC_CONFIG_COUNTSTOP ); + +if (cluster && mips_cm_is64 && !gic_clock_unstable) +gic_sync_counter_64 (cluster); + +if (gic_synced_cl_map && +bitmap_full (gic_synced_cl_map,mips_cps_numclusters ())) +schedule_work (&gic_promote_work); + }gic_clockevent_cpu_init (cpu,this_cpu_ptr (&gic_clockevent_device));return 0 ;
@@ -209,8 +273,33 @@ static void gic_clocksource_unstable(char *reason)clocksource_mark_unstable (&gic_clocksource); } +static void gic_clocksource_promote (struct work_struct *work) +{ +if (gic_clock_unstable || gic_clocksource.read == &gic_hpt_read) +return ; + +if (clocksource_unregister (&gic_clocksource) <0 ) +return ; + + gic_clocksource.read = &gic_hpt_read; +#ifdef CONFIG_GENERIC_GETTIMEOFDAY + gic_clocksource.vdso_clock_mode =VDSO_CLOCKMODE_GIC ; +#endif + +if (clocksource_register_hz (&gic_clocksource, gic_frequency) <0 ) +return ; + +if (mips_cm_revision () >=CM_REV_CM3 || !IS_ENABLED (CONFIG_CPU_FREQ )) { +sched_clock_register (mips_cm_is64 ? + gic_read_count_64 : gic_read_count_2x32, + gic_count_width, gic_frequency); + } +} +static int __init __gic_clocksource_init (void ) { +unsigned int numclusters; +bool synced = false;int ret;/* Set clocksource mask. */
@@ -222,14 +311,32 @@ static int __init __gic_clocksource_init(void)/* Calculate a somewhat reasonable rating value. */ if (mips_cm_revision () >=CM_REV_CM3 || !IS_ENABLED (CONFIG_CPU_FREQ )) - gic_clocksource.rating =300 ;/* Good when frequecy is stable */ + gic_clocksource.rating =300 ;/* Good when frequency is stable */ else gic_clocksource.rating =200 ; gic_clocksource.rating +=clamp (gic_frequency /10000000 ,0 ,99 ); -if (mips_cps_multicluster_cpus ()) { + numclusters =mips_cps_numclusters (); +if (numclusters >1 ) + gic_synced_cl_map =bitmap_zalloc (numclusters,GFP_KERNEL ); + +/* +* Mark cluster 0 as synchronized (with itself), and all clusters +* without cores since there is no local GIC counter access on those. +*/ +if (gic_synced_cl_map) { +bitmap_set (gic_synced_cl_map,0 ,1 ); +for (unsigned int cl =0 ; cl < numclusters; cl++) { +if (!mips_cps_numcores (cl)) +bitmap_set (gic_synced_cl_map, cl,1 ); + } + synced =bitmap_full (gic_synced_cl_map, numclusters); + } + +if (numclusters >1 && !synced) { gic_clocksource.read = &gic_hpt_read_multicluster; gic_clocksource.vdso_clock_mode =VDSO_CLOCKMODE_NONE ; +INIT_WORK (&gic_promote_work, gic_clocksource_promote); } ret =clocksource_register_hz (&gic_clocksource, gic_frequency);
@@ -288,7 +395,7 @@ static int __init gic_clocksource_of_init(struct device_node *node)* change performed by the CPC core clocks divider. */ if ((mips_cm_revision () >=CM_REV_CM3 || !IS_ENABLED (CONFIG_CPU_FREQ )) && - !mips_cps_multicluster_cpus ()) { + gic_clocksource.read == &gic_hpt_read) {sched_clock_register (mips_cm_is64 ? gic_read_count_64 : gic_read_count_2x32, gic_count_width, gic_frequency);
Signature · 1 line
2.55.0
Thomas Bogendoerfer
maintainerRe: [PATCH v3 5/5] clocksource: mips-gic-timer: Use local counter on synced multi-cluster systems
Quoted 12 lines · Benoît Monin
[...]
drivers/clocksource/mips-gic-timer.c +11
-0+static void gic_clocksource_promote (struct work_struct *work) +{ +if (gic_clock_unstable || gic_clocksource.read == &gic_hpt_read) +return ; + +if (clocksource_unregister (&gic_clocksource) <0 ) +return ; + + gic_clocksource.read = &gic_hpt_read; +#ifdef CONFIG_GENERIC_GETTIMEOFDAY + gic_clocksource.vdso_clock_mode =VDSO_CLOCKMODE_GIC ;
drivers/clocksource/mips-gic-timer.c static void gic_clocksource_unstable(char *reason) · line 296
Quoted 10 lines
drivers/clocksource/mips-gic-timer.c +10
-0+#endif + +if (clocksource_register_hz (&gic_clocksource, gic_frequency) <0 ) +return ; + +if (mips_cm_revision () >=CM_REV_CM3 || !IS_ENABLED (CONFIG_CPU_FREQ )) { +sched_clock_register (mips_cm_is64 ? + gic_read_count_64 : gic_read_count_2x32, + gic_count_width, gic_frequency); + }
this feels like a lot of work for just replacing the read method.
Why not introduce a flag in gic_hpt_read(), which indicates whether it can read the counter directly or need to do it via gpt_hpt_read_multicluster() ?
Thomas.
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
Re: [PATCH v3 5/5] clocksource: mips-gic-timer: Use local counter on synced multi-cluster systems
Quoted 25 lines · Thomas Bogendoerfer
Quoted 12 lines
[...]
drivers/clocksource/mips-gic-timer.c +11
-0+static void gic_clocksource_promote (struct work_struct *work) +{ +if (gic_clock_unstable || gic_clocksource.read == &gic_hpt_read) +return ; + +if (clocksource_unregister (&gic_clocksource) <0 ) +return ; + + gic_clocksource.read = &gic_hpt_read; +#ifdef CONFIG_GENERIC_GETTIMEOFDAY + gic_clocksource.vdso_clock_mode =VDSO_CLOCKMODE_GIC ;drivers/clocksource/mips-gic-timer.c static void gic_clocksource_unstable(char *reason) · line 296
Quoted 10 lines
drivers/clocksource/mips-gic-timer.c +10
-0+#endif + +if (clocksource_register_hz (&gic_clocksource, gic_frequency) <0 ) +return ; + +if (mips_cm_revision () >=CM_REV_CM3 || !IS_ENABLED (CONFIG_CPU_FREQ )) { +sched_clock_register (mips_cm_is64 ? + gic_read_count_64 : gic_read_count_2x32, + gic_count_width, gic_frequency); + }this feels like a lot of work for just replacing the read method.
Quoted 3 lines
Why not introduce a flag in
gic_hpt_read(), which indicates whether it can read the counter directly or need to do it viagpt_hpt_read_multicluster()?
Yes, this way I can avoid the clocksource_unregister()/register(). I'll do that.
Best regards,
Signature · 3 lines
Benoît Monin, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Thomas Gleixner
maintainerOn Mon, Sep 07 2026 at 14:46, Benoît Monin wrote:
Can the MIPS people who care about this code spend some time on reviewing this pile?
https://lore.kernel.org/871pb9841j.ffs@fw13
Quoted 107 lines
This series addresses multi-cluster MIPS GIC handling: it fixes a few cross-cluster and lock bugs, and allows the GIC timer to be used as a fast local clocksource on multi-cluster systems by synchronizing the per-cluster counters.
If you are wondering how a single patch in version one turned into a five-patch series in version two, the answser is
PROVE_LOCKINGwhich found some issues that this series addresses.The first three patches are for the irq-mips-gic irqchip driver.
The first patch fixes the
for_each_online_cpu_gic()macro which was releasing an unheld per-CPUcm_core_lockon every invocation on multi-cluster hardware. Move both the acquire and release into__gic_with_next_online_cpu()so they stay balanced and thefor_eachmacro accesses all VP on a multi-cluster system.The second patch fixes a recursive acquisition of
gic_lockingic_set_affinity()when moving interrupt SMP affinity across clusters, caused by a call togic_set_type(). Split out agic_set_type_locked()helper to resolve the issue.The third patch enables the interrupt when moving affinity across clusters. The interrupt mask is per-cluster, so after a cross-cluster affinity change the interrupt was left disabled in the destination cluster. Handle the mask explicitly on both the old and the new side.
The last two patches are for the mips-gic-timer clocksource driver.
The fourth patch is what changed from version 2 of the series. It simplifies
gic_next_event()as it is guaranteed to be invoked on the same CPU of the event. And the flagCLOCK_EVT_FEAT_PERCPUis added to better document that guarantee.The last patch is the main functional change, carried over from v1. It synchronizes each secondary cluster's GIC counter to cluster 0's counter as its CPUs come online, and once every cluster is in sync, promote the clocksource back from the slow cross-cluster
gic_hpt_read_multicluster()path to the fast localgic_hpt_read(), also re-enabling the GIC VDSO clock mode. On the dual-cluster Mobileye EyeQ6H SoC this makesclock_gettime(CLOCK_MONOTONIC)about four times faster on the secondary cluster and replaces the jiffies-basedsched_clockwith the high-precision GIC counter.This series was tested on Mobileye SoCs with different cluster configurations:
* The EyeQ5 with two clusters but only one is equipped with VP (VP topology {2,2,2,2},{} total 8).
* The EyeQ6Lplus which is strictly single cluster (VP topology {4,4} total 8).
* The EyeQ6H with two identical clusters
(VP topology {4,4,4,4},{4,4,4,4} total 32).Although I am posting this as a single series since the patches are all related to the GIC, the changes to the irqchip driver and to the clocksource driver are independent from each other and can be merged in different trees. I can split the series in two if it helps reviewing/merging.
Signed-off-by Benoît Monin <[email protected]>---
Changes in v3:
- Rebased on v7.3-rc1.
- Simplifiedgic_next_event()as it is strictly per-CPU instead of fixing
unused code.
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- Four new patches described above fixing multi-cluster
handling in the GIC irqchip and clocksource drivers.
- In the last patch,gic_clocksource_promote()is now deferred via a
workqueue instead of a CPUHP ONLINE callback, to avoid a lockdep
issue onclocksource_mutex.
- Link to v1: https://patch.msgid.link/[email protected]To: Thomas Bogendoerfer <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Dragan Mladjenovic <[email protected]>
To: Chao-ying Fu <[email protected]>
To: Aleksandar Rikalo <[email protected]>
To: Paul Burton <[email protected]>
To: Daniel Lezcano <[email protected]>
To: Radu Rendec <[email protected]>
Cc: Thomas Petazzoni <[email protected]>
Cc: [email protected]
Cc: [email protected]---
Benoît Monin (5):
irqchip/mips-gic: Fix unbalancedcm_core_lockinfor_each_online_cpu_gic()irqchip/mips-gic: Fix recursive acquisition ofgic_lockingic_set_affinity()irqchip/mips-gic: Enable interrupt when moving affinity across clusters
clocksource: mips-gic-timer: Simplifygic_next_event()for per-cpu timer
clocksource: mips-gic-timer: Use local counter on synced multi-cluster systemsdrivers/clocksource/mips-gic-timer.c | 127 +++++++++++++++++++++++++++++++---- drivers/irqchip/irq-mips-gic.c | 51 ++++++++------ 2 files changed, 144 insertions(+), 34 deletions(-)---
base-commit:cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260717-sync-gic-counters-1cd6b40b968eBest regards,
Signature · 3 lines
Benoît Monin, Bootlin Embedded Linux and Kernel engineering https://bootlin.com