Linux clock framework development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jisheng Zhang" <jszhang@kernel.org>
Cc: devicetree@vger.kernel.org, linux-clk@vger.kernel.org,
	linux-gpio@vger.kernel.org, mfd@lists.linux.dev, robh@kernel.org,
	conor+dt@kernel.org, lee@kernel.org
Subject: Re: [PATCH v4 09/13] clk: berlin: add Synaptics clock controller helpers
Date: Fri, 04 Dec 2026 17:13:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-250282@kernel.org> (raw)
In-Reply-To: <20261204140732.31223-10-jszhang@kernel.org>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] clk: berlin: IRQ-unsafe spinlock in sl_sysmgr_gateclk_ops
- [Medium] clk: berlin: uninitialized struct clk_init_data in
  sl-sysmgr-clk.c
- [Medium] clk: berlin: sl_clk_determine_rate() ignores min_rate and
  max_rate
- [Medium] clk: berlin: sl_clk_get_parent() returns 0 on unsupported
  selector
- [Medium] clk: berlin: sl_sysmgr_clk_ops and gateclk ops omit .disable
  callback
- [Medium] clk: berlin: sl_sysmgr_clk_setup() ignores descriptor flags
- [Medium] clk: berlin: sl_clk_value() maps DIV_SEL 0, 6, 7 to 0 instead
  of 1
- [Low] clk: berlin: sl-sysmgr-clk.h is not self-contained and has
  unused items
--

commit 7ae4f8ba2e3b37f62f72f67fa5bfa73fb4501645
Author: Jisheng Zhang <jszhang@kernel.org>

clk: berlin: add Synaptics clock controller helpers

This commit adds the shared configurable divider/gate clock driver
(sl-clk.c) and the MCU system manager clock helper (sl-sysmgr-clk.c) for
Synaptics SL SoCs.

> diff --git a/drivers/clk/berlin/sl-clk.c b/drivers/clk/berlin/sl-clk.c
> new file mode 100644
> index 0000000000000..e212320a0637d
> --- /dev/null
> +++ b/drivers/clk/berlin/sl-clk.c
> @@ -0,0 +1,279 @@

[ ... ]

> +static unsigned int sl_clk_value(u32 val)
> +{
> +	static const u8 div_table[] = { 0, 2, 4, 6, 8, 12, 0, 0 };
> +
> +	if (val & SL_CLK_DIV3_SWITCH)
> +		return 3;
> +	if (!(val & SL_CLK_DIV_SWITCH))
> +		return 1;
> +
> +	return div_table[FIELD_GET(SL_CLK_DIV_SEL, val)];
> +}
> +
> +static unsigned long sl_clk_recalc_rate(struct clk_hw *hw,
> +					unsigned long parent_rate)
> +{
> +	struct sl_clk *clk = to_sl_clk(hw);
> +	unsigned int divisor = sl_clk_value(readl_relaxed(clk->reg));
> +
> +	if (WARN_ON_ONCE(!divisor))
> +		return 0;
> +
> +	return parent_rate / divisor;
> +}

[Severity: Medium]
Should div_table[] map SL_CLK_DIV_SEL encodings 0, 6, and 7 to 1 rather
than 0?

This register layout matches BERLIN2_SINGLE_DIV in berlin2-div.h, where
berlin2-div.c maps those encodings to divide-by-1 in clk_div[].  If reset
or firmware leaves SL_CLK_DIV_SWITCH set with SL_CLK_DIV_SEL in {0, 6, 7},
sl_clk_recalc_rate() triggers WARN_ON_ONCE(!divisor) and reports 0 Hz for
the clock and its children.

> +
> +static unsigned int sl_clk_best(unsigned long parent_rate,
> +				unsigned long rate)
> +{
> +	unsigned long best_diff = ULONG_MAX;
> +	unsigned int best = 1, i;
> +
> +	for (i = 0; i < ARRAY_SIZE(sl_clk_values); i++) {
> +		unsigned long candidate = parent_rate / sl_clk_values[i];
> +		unsigned long diff = candidate > rate ? candidate - rate :
> +							     rate - candidate;
> +
> +		if (diff < best_diff) {
> +			best_diff = diff;
> +			best = sl_clk_values[i];
> +		}
> +	}
> +
> +	return best;
> +}
> +
> +static int sl_clk_determine_rate(struct clk_hw *hw,
> +				 struct clk_rate_request *req)
> +{
> +	struct clk_hw *parent = clk_hw_get_parent(hw);
> +
> +	if (!parent)
> +		return -EINVAL;
> +
> +	req->best_parent_hw = parent;
> +	req->best_parent_rate = clk_hw_get_rate(parent);
> +	req->rate = req->best_parent_rate /
> +		    sl_clk_best(req->best_parent_rate, req->rate);
> +
> +	return 0;
> +}

[Severity: Medium]
Does sl_clk_determine_rate() need to respect req->min_rate and
req->max_rate?

sl_clk_best() picks the closest divisor across sl_clk_values[] without
checking rate bounds.  For example, with a 1200 MHz parent and
req->max_rate = 360 MHz, it selects divisor 3 (400 MHz) instead of
divisor 4 (300 MHz), causing clk_calc_new_rates() to reject the rate and
fail clk_set_rate() with -EINVAL even though a valid in-range divisor
exists.

Filtering candidates to [req->min_rate, req->max_rate] and returning
-EINVAL if none fits avoids this.

[ ... ]

> +static u8 sl_clk_get_parent(struct clk_hw *hw)
> +{
> +	struct sl_clk *clk = to_sl_clk(hw);
> +	u32 val = readl_relaxed(clk->reg);
> +	u8 selector;
> +	unsigned int index;
> +
> +	if (!(val & SL_CLK_PLL_SWITCH))
> +		return 0;
> +
> +	selector = FIELD_GET(SL_CLK_PLL_SEL, val);
> +	index = selector + 1;
> +	if (index < clk_hw_get_num_parents(hw))
> +		return index;
> +
> +	dev_warn(clk->dev, "%s: unsupported parent selector %u\n",
> +		 clk_hw_get_name(hw), selector);
> +	return 0;
> +}

[Severity: Medium]
Should sl_clk_get_parent() return an out-of-range index instead of 0 when
hardware has an unsupported parent selector?

Returning 0 makes __clk_init_parent() attach the clock to parent 0 even
though SL_CLK_PLL_SWITCH is still set to the invalid selector.  Any later
clk_set_parent() targeting parent 0 then short-circuits in
clk_core_set_parent_nolock() because core->parent already matches, leaving
hardware stuck on the unsupported selector.

Returning index (or clk_hw_get_num_parents(hw)) lets
clk_core_get_parent_by_index() treat the clock as orphaned until
reparented.

[ ... ]

> diff --git a/drivers/clk/berlin/sl-sysmgr-clk.c b/drivers/clk/berlin/sl-sysmgr-clk.c
> new file mode 100644
> index 0000000000000..f3cf921fc3c1a
> --- /dev/null
> +++ b/drivers/clk/berlin/sl-sysmgr-clk.c
> @@ -0,0 +1,241 @@

[ ... ]

> +static DEFINE_SPINLOCK(sl_sysmgr_gateclk_lock);
> +
> +#define to_sl_sysmgr_gateclk(hw)	container_of(hw, struct sl_sysmgr_gateclk, hw)
> +#define to_sl_sysmgr_clk(hw)		container_of(hw, struct sl_sysmgr_clk, hw)
> +
> +static int sl_sysmgr_gateclk_enable(struct clk_hw *hw)
> +{
> +	u32 val;
> +	struct sl_sysmgr_gateclk *clk = to_sl_sysmgr_gateclk(hw);
> +
> +	guard(spinlock)(&sl_sysmgr_gateclk_lock);
> +
> +	val = readl_relaxed(clk->base);
> +	if (!(val & BIT(clk->bit_idx))) {
> +		val |= BIT(clk->bit_idx);
> +		writel_relaxed(val, clk->base);
> +	}
> +
> +	return 0;
> +}
> +
> +static int sl_sysmgr_gateclk_is_enabled(struct clk_hw *hw)
> +{
> +	u32 val;
> +	struct sl_sysmgr_gateclk *clk = to_sl_sysmgr_gateclk(hw);
> +
> +	guard(spinlock)(&sl_sysmgr_gateclk_lock);
> +
> +	val = readl_relaxed(clk->base);
> +	val &= BIT(clk->bit_idx);
> +
> +	return val ? 1 : 0;
> +}

[Severity: High]
Can acquiring sl_sysmgr_gateclk_lock without disabling interrupts cause a
hardirq deadlock?

clk_core_enable() calls sl_sysmgr_gateclk_enable() while holding CCF's
hardirq-safe enable_lock, whereas clk_summary_show_one() calls
sl_sysmgr_gateclk_is_enabled() in process context with interrupts enabled
and without enable_lock:

CPU0                              CPU1
----                              ----
lock(&sl_sysmgr_gateclk_lock)
                                  lock(&enable_lock)
                                  lock(&sl_sysmgr_gateclk_lock)
<hardirq>
  lock(&enable_lock)

 *** DEADLOCK ***

CPU0: clk_summary_show_one() -> clk_core_is_enabled() ->
      sl_sysmgr_gateclk_is_enabled()
      <hardirq> clk_enable() -> clk_core_enable_lock()
CPU1: clk_enable() -> clk_core_enable_lock() ->
      sl_sysmgr_gateclk_enable()

A hardirq enabling a SysMgr gate clock on the same CPU also self-deadlocks
on sl_sysmgr_gateclk_lock.  Since sl_sysmgr_gateclk_is_enabled() only
performs a single 32-bit readl_relaxed(), dropping the lock there and
using guard(spinlock_irqsave) on the write path avoids both the deadlock
and unnecessary global contention.

> +
> +static const struct clk_ops sl_sysmgr_gateclk_ops = {
> +	.enable		= sl_sysmgr_gateclk_enable,
> +	.is_enabled	= sl_sysmgr_gateclk_is_enabled,
> +};

[Severity: Medium]
Why do sl_sysmgr_gateclk_ops and sl_sysmgr_clk_ops omit .disable?

Without .disable, clk_disable() and clk_disable_unused() cannot gate
these clocks off in hardware once enabled.  In addition,
clk_is_enabled_when_prepared() checks !(ops->enable && ops->disable) and
falsely returns true, causing pm_clk_acquire() to misclassify these
clocks as enabled when prepared.

Separately, sl_sysmgr_gateclk duplicates devm_clk_hw_register_gate(), and
struct sl_sysmgr_gateclk_desc duplicates struct sl_gateclk_desc in
sl-clk.h.

> +
> +static int
> +sl_sysmgr_gateclk_register(struct device *dev, struct sl_sysmgr_gateclk *gclk,
> +			   const char *name, const char *parent_names,
> +			   u8 bit_idx, void __iomem *base)
> +{
> +	struct clk_init_data init;
> +
> +	init.name = name;
> +	init.ops = &sl_sysmgr_gateclk_ops;
> +	init.parent_names = &parent_names;
> +	init.num_parents = 1;
> +	init.flags = 0;

[Severity: Medium]
Does leaving init uninitialized here and in sl_sysmgr_clk_register() pass
uninitialized stack memory into clk_core_populate_parent_map()?

Neither function initializes init.parent_data or init.parent_hws, which
clk_core_populate_parent_map() reads unconditionally during clock
registration, triggering KMSAN warnings.  Zero-initializing with
struct clk_init_data init = {}; (as sl_clk_register() does) avoids this.

[ ... ]

> +	for (i = 0; i < gclk_n; i++) {
> +		ret = sl_sysmgr_gateclk_register(&pdev->dev, &priv->gclks[i],
> +						 gdescs[i].name, gdescs[i].parent_name,
> +						 gdescs[i].bit_idx, base + gclk_offset);
> +		if (ret)
> +			return ret;
> +
> +		id = gdescs[i].id;
> +		clk_data->hws[id] = &priv->gclks[i].hw;
> +	}
> +
> +	for (i = 0; i < clk_n; i++) {
> +		ret = sl_sysmgr_clk_register(&pdev->dev, &priv->clks[i],
> +					     descs[i].name, descs[i].parent_name,
> +					     descs[i].div_bits, base + descs[i].offset);

[Severity: Medium]
Should sl_sysmgr_clk_setup() pass gdescs[i].flags and descs[i].flags to
the registration helpers?

Both struct sl_sysmgr_gateclk_desc and struct sl_sysmgr_clk_desc expose a
flags field, but sl_sysmgr_gateclk_register() and sl_sysmgr_clk_register()
hardcode init.flags = 0, silently discarding flags such as
CLK_IS_CRITICAL or CLK_IGNORE_UNUSED.

[ ... ]

> diff --git a/drivers/clk/berlin/sl-sysmgr-clk.h b/drivers/clk/berlin/sl-sysmgr-clk.h
> new file mode 100644
> index 0000000000000..5ccf388bcccbd
> --- /dev/null
> +++ b/drivers/clk/berlin/sl-sysmgr-clk.h
> @@ -0,0 +1,44 @@

[ ... ]

> +#ifndef __SL_SYSMGR_CLK_H
> +#define __SL_SYSMGR_CLK_H
> +
> +struct sl_sysmgr_clk_desc {
> +	const char	*name;
> +	const char	*parent_name;
> +	u32		id;
> +	u32		offset;
> +	u8		div_bits;
> +	unsigned long	flags;
> +};

[ ... ]

> +int sl_sysmgr_clk_setup(struct platform_device *pdev,

[Severity: Low]
Can sl-sysmgr-clk.h be included standalone?  It uses u32, u8, and struct
platform_device without including linux/types.h or forward-declaring
struct platform_device, which fails to build if included before those
headers.

In sl-sysmgr-clk.c, linux/version.h and DIV_CHNG_EN are also unused.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204140732.31223-1-jszhang@kernel.org?part=9

  reply	other threads:[~2026-12-04 17:13 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 14:07 [PATCH v4 00/13] arm64: add Synaptics SL2610 SoCs and RDK boards Jisheng Zhang
2026-12-04 14:07 ` [PATCH v4 01/13] arm64: kconfig: let ARCH_BERLIN cover Synaptics arm64 SoCs Jisheng Zhang
2026-12-04 14:07 ` [PATCH v4 02/13] pinctrl: berlin: use u16 instead of u8 for the offset Jisheng Zhang
2026-12-04 14:07 ` [PATCH v4 03/13] pinctrl: berlin: enable module build support Jisheng Zhang
2026-12-04 15:03   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 04/13] pinctrl: berlin: add optional pinconf support Jisheng Zhang
2026-12-04 15:22   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 05/13] dt-bindings: soc: synaptics: add SL2610 system controllers Jisheng Zhang
2026-12-04 15:41   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 06/13] mfd: synaptics: add SL2610 system controller support Jisheng Zhang
2026-12-04 15:58   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 07/13] reset: add Synaptics SL2610 reset " Jisheng Zhang
2026-12-04 16:24   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 08/13] pinctrl: berlin: add Synaptics SL2610 pin " Jisheng Zhang
2026-12-04 16:51   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 09/13] clk: berlin: add Synaptics clock controller helpers Jisheng Zhang
2026-12-04 17:13   ` sashiko-bot [this message]
2026-12-04 14:07 ` [PATCH v4 10/13] clk: berlin: add Synaptics SL2610 clocks and PLLs Jisheng Zhang
2026-12-04 17:39   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 11/13] dt-bindings: arm: berlin: add Synaptics SL2610 SoCs and RDK boards Jisheng Zhang
2026-12-04 14:07 ` [PATCH v4 12/13] arm64: dts: synaptics: add SL2610 family SoCs Jisheng Zhang
2026-12-04 18:05   ` sashiko-bot
2026-12-04 14:07 ` [PATCH v4 13/13] arm64: dts: synaptics: add SL2610 family RDK boards Jisheng Zhang
2026-12-04 18:19   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250282@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jszhang@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox