* Re: Re: Re: Re: Re: Re: [PATCH v3 2/3] clk: eswin: Add eic7700 HSP clock driver
[not found] ` <afPc66GcXiSzQ6uN@redhat.com>
@ 2026-05-09 2:53 ` Xuyang Dong
0 siblings, 0 replies; only message in thread
From: Xuyang Dong @ 2026-05-09 2:53 UTC (permalink / raw)
To: Brian Masney
Cc: Stephen Boyd, mturquette, robh, krzk+dt, conor+dt, linux-clk,
devicetree, linux-kernel, p.zabel, huangyifeng, benoit.monin,
ningyu, linmin, pinkesh.vaghela
>
> On Thu, Apr 30, 2026 at 01:58:58PM +0800, Xuyang Dong wrote:
> > > On Wed, Apr 29, 2026 at 05:38:51PM +0800, Xuyang Dong wrote:
> > > > > >
> > > > > > The common gate API, the HSP private API, and the reset driver all access
> > > > > > the same register space.
> > > > > > Therefore, they need to be protected by the same data->lock.
> > > > > >
> > > > >
> > > > > If everything is accessing registers through regmap why aren't we using
> > > > > the builtin lock with struct regmap_config::use_raw_spinlock? I don't
> > > > > understand why we're rolling our own here.
> > > >
> > > > Hi Stephen,
> > > >
> > > > In the HSP clock driver and reset driver, there are three components that
> > > > access the HSP register space: a common gate clock, a custom gate clock
> > > > (i.e., 0x800), and a reset.
> > > >
> > > > 1. The common gate uses eswin_clk_register_gate() to register a gate clock
> > > > via devm_clk_hw_register_gate_parent_data(). It accesses the register
> > > > using clk_gate_endisable().
> > > >
> > > > static void clk_gate_endisable(struct clk_hw *hw, int enable)
> > > > {
> > > > struct clk_gate *gate = to_clk_gate(hw);
> > > > unsigned long flags;
> > > >
> > > > if (gate->lock)
> > > > spin_lock_irqsave(gate->lock, flags);
> > > > else
> > > > __acquire(gate->lock);
> > > > ...
> > > > if (gate->lock)
> > > > spin_unlock_irqrestore(gate->lock, flags);
> > > > else
> > > > __release(gate->lock);
> > > > }
> > > >
> > > > The gate->lock in use is the data->lock passed in from the clock driver.
> > > >
> > > > 2. The custom gate uses hsp_clk_register_gate() to register a gate clock.
> > > > It accesses the register using hsp_clk_gate_endisable().
> > > >
> > > > static void hsp_clk_gate_endisable(struct clk_hw *hw, int enable)
> > > > {
> > > > struct eic7700_hsp_clk_gate *gate = to_gate_clk(hw);
> > > >
> > > > guard(spinlock_irqsave)(gate->lock);
> > > > ...
> > > > }
> > > >
> > > > The gate->lock in use is the same data->lock passed in from the clock
> > > > driver.
> > > >
> > > > 3. The reset uses eic7700_hsp_reset_assert() and
> > > > eic7700_hsp_reset_deassert(), which call regmap_assign_bits() to access
> > > > the register.
> > > >
> > > > All three methods access the same register space; therefore, they must be
> > > > protected by the same lock (data->lock).
> > > >
> > > > That's why we introduced eic7700_hsp_regmap_lock/unlock for
> > > > eic7700_hsp_regmap_config.
> > > > eic7700_hsp_regmap_config = {
> > > > .lock = eic7700_hsp_regmap_lock,
> > > > .unlock = eic7700_hsp_regmap_unlock,
> > > > .lock_arg = lock_ctx,
> > > > };
> > > >
> > > > The 'lock_ctx->lock' in eic7700_hsp_regmap_lock/unlock is the 'data->lock'.
> > > > static void eic7700_hsp_regmap_lock(void *arg)
> > > > __acquires(lock_ctx->lock)
> > > > {
> > > > struct eic7700_hsp_regmap_lock *const lock_ctx = arg;
> > > > unsigned long flags;
> > > >
> > > > spin_lock_irqsave(lock_ctx->lock, flags);
> > > > lock_ctx->flags = flags;
> > > > }
> > > >
> > > > The similar approach can be found in clk-imx8ulp-sim-lpav.c.
> > > >
> > > > The annotations what we mentioned previously is the above
> > > > "__acquires(lock_ctx->lock)".
> > >
> > > I see what Stephen is saying. Take a look at __regmap_init() in
> > > drivers/base/regmap/regmap.c. If the lock/unlock ops are not specified,
> > > then the final else will automatically setup locking. By default, it'll
> > > use a mutex, but there is the ability to use a spinlock.
> > >
> > > So you can drop the lock/unlock ops from the driver, and add to the ops:
> > >
> > > fast_io: 1,
> > > use_raw_spinlock: 1,
> > >
> > > Given the critcal nature of clks, I agree with Stephen that a raw
> > > spinlock should be used here.
> > >
> >
> > Hi Stephen and Brian,
> >
> > In the HSP clock driver, hsp_clk_gate_endisable() only accesses the
> > registers at 0x800/0x900, and reset accesses the same registers as well,
> > which leads to concurrent RMW (read-modify-write) races.
> >
> > There are two approaches to solve these races.
> >
> > The first method is the current implementation. All three functions
> > (clk_gate_endisable(), hsp_clk_gate_endisable(),
> > and eic7700_hsp_reset_assert()) use data->lock to prevent concurrent
> > RMW races.
> >
> > The second method is as Stephen said. If I understand correctly, it is to
> > change the register read/write operations in hsp_clk_gate_endisable() to
> > use the regmap API and use the same lock (map->raw_spinlock) as reset.
> >
> > Is the second approach preferable?
>
> Use the same regmap everywhere. Also you don't have to explicitly define
> the raw spinlock in your driver since the regmap API will create a raw
> spinlock for you if you use the fast_io / use_raw_spinlock options I
> described above.
>
Hi Brian,
Thank you for your time and comments. Sorry for the late reply.
This will be fixed in the next version.
Best regards,
Xuyang Dong
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-05-09 2:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260423091114.2326-1-dongxuyang@eswincomputing.com>
[not found] ` <aeo8nn-eigzlojWx@redhat.com>
[not found] ` <4e5c887.5a31.19dbf179fb6.Coremail.dongxuyang@eswincomputing.com>
[not found] ` <CABx5tqK7p_XJHfXZ70gXhR88PzAteV7cVSFPoRzccgmjanADMw@mail.gmail.com>
[not found] ` <177733570840.5403.12558106273673899411@lazor>
[not found] ` <7a76d8cb.5bab.19dd3645d4e.Coremail.dongxuyang@eswincomputing.com>
[not found] ` <177742748214.5403.15526965667317467444@localhost.localdomain>
[not found] ` <4257942f.5c6d.19dd89b06f8.Coremail.dongxuyang@eswincomputing.com>
[not found] ` <afINjhKluCxeb9LK@redhat.com>
[not found] ` <1f0a2d11.5cd2.19ddcf8114a.Coremail.dongxuyang@eswincomputing.com>
[not found] ` <afPc66GcXiSzQ6uN@redhat.com>
2026-05-09 2:53 ` Re: Re: Re: Re: Re: [PATCH v3 2/3] clk: eswin: Add eic7700 HSP clock driver Xuyang Dong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox