From nobody Thu Oct 2 12:06:47 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B10F4337E9A; Wed, 17 Sep 2025 12:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758111865; cv=pass; b=oyNu3aTaL4ngcCkEWapK7I3x8inlhNIEioBsOEDeZjDbOA1biIZHPysXn3klqb+nKqkfKRBiqJ5HUCINbJdvJ7hFfeMeOR/uTSg7o+ilq0LR2PCnxbCeWvZufm5of5Kvr/6bGopfMvcHm2aTQBYHILFz+hWcAn58tXnIiCnswig= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758111865; c=relaxed/simple; bh=NzkgHQgcYDUrCVVa26xpYjN8qVsMz4h9FTH4FPTxOaI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UR9Lnt2d52IHoaBL2xFyPGIvC4jsnvinp+Zzh2QNoR2LKUPArw58qeGu7hI1so3KN1axXkiLv+iBV828WWKEhzKaiTnKokRdrtwq8fApJDrgDQBQC6j3zuZ10r1Ii7hgxp3EzgvolTyzhKgjWF5QdKL76eU6ZrxzInzBa2la7Ho= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=YShtF36k; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="YShtF36k" ARC-Seal: i=1; a=rsa-sha256; t=1758111834; cv=none; d=zohomail.com; s=zohoarc; b=NKLexEr7AJLZ0/b8937VhVppN9TmQHQVr6fM0zJzzlgAjq8LZZXPp5T6zO+LDX5Qn/p/sJkMCs8Ax5iavyvVkDnc6P2Er0t44C+6TAJhicCidb48jD0rrYkWEU20dnGW6en/CMsDORVREq0ynGcnEtAIioaAHJAYiyyJCitAgQc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1758111834; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=YgQ1u8TXIPwB5gikKQCGFASO6bmBVfR87cbZTdgE0KE=; b=RpVOTNHwAKPwamnIRJ6C13mFP2PJZUI7NOsKPN1UDYHD7T2N9E5n3IywkwR14SJhUnuIGRaHvKZ3oU4SrmqjnhxMPljVIKNdC60dztjvG3RcFw7YoKirf1RUe5Mz2SQ1ynJy7KwrnIzrLVWJOj8VZGcupCQwkmaG9+6XOyrmbwQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1758111834; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=YgQ1u8TXIPwB5gikKQCGFASO6bmBVfR87cbZTdgE0KE=; b=YShtF36kV71aKi9+JCwVxqWAipFlmF4bBznV97zLh5XAJak0XOdfvXqbLm2scdL7 5l7hBDx0vDqbXEeQuhlKLgONSC6qsL9UWWVOxmgUzfriwynmku9NuQoNi02zNaWRH5X gB4dCAfu5ohTadrV2IWZwb7lWFlU9HbKrTOdWsCE= Received: by mx.zohomail.com with SMTPS id 1758111833136461.8065563339446; Wed, 17 Sep 2025 05:23:53 -0700 (PDT) From: Nicolas Frattaroli Date: Wed, 17 Sep 2025 14:22:39 +0200 Subject: [PATCH v3 08/10] drm/panthor: devfreq: add pluggable devfreq providers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250917-mt8196-gpufreq-v3-8-c4ede4b4399e@collabora.com> References: <20250917-mt8196-gpufreq-v3-0-c4ede4b4399e@collabora.com> In-Reply-To: <20250917-mt8196-gpufreq-v3-0-c4ede4b4399e@collabora.com> To: AngeloGioacchino Del Regno , Boris Brezillon , Steven Price , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matthias Brugger , MyungJoo Ham , Kyungmin Park , Chanwoo Choi , Jassi Brar , Kees Cook , "Gustavo A. R. Silva" , Chia-I Wu , Chen-Yu Tsai Cc: kernel@collabora.com, dri-devel@lists.freedesktop.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-pm@vger.kernel.org, linux-hardening@vger.kernel.org, Nicolas Frattaroli X-Mailer: b4 0.14.2 On some devices, devfreq is not controlled directly by DT OPPs and the common clock framework, but through an external devfreq driver. To permit this type of usage, add the concept of devfreq providers. Devfreq providers for panthor register themselves with panthor as a provider. panthor then gets whatever device is pointed at on its performance-domains property, finds the registered devfreq provider for it, and uses its registered devfreq ops. It wraps those operations by passing the provider ops the provider's struct device, as opposed to the panthor device. Providers can choose to omit overloading some operations. In that case, panthor's own implementation is used. The only exception is the exit operation, which panthor does not use, and the wrapper only gets registered if a provider needs it. Should the probe order work out such that panthor probes before the devfreq provider is finished probing and registering itself, then we just defer the probe after adding a device link. Signed-off-by: Nicolas Frattaroli --- drivers/gpu/drm/panthor/panthor_devfreq.c | 190 ++++++++++++++++++++++++++= ---- drivers/gpu/drm/panthor/panthor_devfreq.h | 47 ++++++++ 2 files changed, 212 insertions(+), 25 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_devfreq.c b/drivers/gpu/drm/pa= nthor/panthor_devfreq.c index 118da7cbb3c809e4aabfef7d20914e61c2b62555..1f10724e2f08df09b52fa1a27ff= e9cfd49994b09 100644 --- a/drivers/gpu/drm/panthor/panthor_devfreq.c +++ b/drivers/gpu/drm/panthor/panthor_devfreq.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include =20 @@ -34,6 +35,12 @@ struct panthor_devfreq { /** @last_busy_state: True if the GPU was busy last time we updated the s= tate. */ bool last_busy_state; =20 + /** + * @panthor_devfreq_provider: the used performance-domain controller + * through which devfreq callbacks are passed onto, or NULL if none. + */ + struct panthor_devfreq_provider *provider; + /** * @lock: Lock used to protect busy_time, idle_time, time_last_update and * last_busy_state. @@ -44,6 +51,19 @@ struct panthor_devfreq { spinlock_t lock; }; =20 +static LIST_HEAD(panthor_devfreq_providers); +static DEFINE_MUTEX(panthor_devfreq_providers_lock); + +int panthor_devfreq_register_provider(struct panthor_devfreq_provider *pro= v) +{ + guard(mutex)(&panthor_devfreq_providers_lock); + + list_add(&prov->node, &panthor_devfreq_providers); + + return 0; +} +EXPORT_SYMBOL(panthor_devfreq_register_provider); + static void panthor_devfreq_update_utilization(struct panthor_devfreq *pde= vfreq) { ktime_t now, last; @@ -59,12 +79,26 @@ static void panthor_devfreq_update_utilization(struct p= anthor_devfreq *pdevfreq) pdevfreq->time_last_update =3D now; } =20 +static void panthor_devfreq_exit(struct device *dev) +{ + struct panthor_device *ptdev =3D dev_get_drvdata(dev); + struct panthor_devfreq_provider *provider =3D ptdev->devfreq->provider; + + if (provider && provider->exit) + provider->exit(provider->dev); +} + static int panthor_devfreq_target(struct device *dev, unsigned long *freq, u32 flags) { + struct panthor_device *ptdev =3D dev_get_drvdata(dev); + struct panthor_devfreq_provider *provider =3D ptdev->devfreq->provider; struct dev_pm_opp *opp; int err; =20 + if (provider && provider->target) + return provider->target(provider->dev, freq, flags); + opp =3D devfreq_recommended_opp(dev, freq, flags); if (IS_ERR(opp)) return PTR_ERR(opp); @@ -87,6 +121,7 @@ static int panthor_devfreq_get_dev_status(struct device = *dev, { struct panthor_device *ptdev =3D dev_get_drvdata(dev); struct panthor_devfreq *pdevfreq =3D ptdev->devfreq; + struct panthor_devfreq_provider *provider =3D pdevfreq->provider; struct devfreq_dev_profile *p =3D pdevfreq->devfreq->profile; unsigned long irqflags; int ret; @@ -97,32 +132,42 @@ static int panthor_devfreq_get_dev_status(struct devic= e *dev, =20 spin_lock_irqsave(&pdevfreq->lock, irqflags); =20 - panthor_devfreq_update_utilization(pdevfreq); + if (provider && provider->get_dev_status) { + ret =3D provider->get_dev_status(provider->dev, status); + } else { + panthor_devfreq_update_utilization(pdevfreq); =20 - status->total_time =3D ktime_to_ns(ktime_add(pdevfreq->busy_time, - pdevfreq->idle_time)); + status->total_time =3D ktime_to_ns(ktime_add(pdevfreq->busy_time, + pdevfreq->idle_time)); =20 - status->busy_time =3D ktime_to_ns(pdevfreq->busy_time); + status->busy_time =3D ktime_to_ns(pdevfreq->busy_time); =20 - panthor_devfreq_reset(pdevfreq); + panthor_devfreq_reset(pdevfreq); + } =20 spin_unlock_irqrestore(&pdevfreq->lock, irqflags); =20 - drm_dbg(&ptdev->base, "busy %lu total %lu %lu %% freq %lu MHz\n", - status->busy_time, status->total_time, - status->busy_time / (status->total_time / 100), - status->current_frequency / 1000 / 1000); + if (!ret) + drm_dbg(&ptdev->base, "busy %lu total %lu %lu %% freq %lu MHz\n", + status->busy_time, status->total_time, + status->busy_time / (status->total_time / 100), + status->current_frequency / 1000 / 1000); =20 - return 0; + return ret; } =20 static int panthor_devfreq_get_cur_freq(struct device *dev, unsigned long = *freq) { struct panthor_device *ptdev =3D dev_get_drvdata(dev); + struct panthor_devfreq_provider *provider =3D ptdev->devfreq->provider; + int ret =3D 0; =20 - *freq =3D clk_get_rate(ptdev->clks.core); + if (provider && provider->get_cur_freq) + ret =3D provider->get_cur_freq(provider->dev, freq); + else + *freq =3D clk_get_rate(ptdev->clks.core); =20 - return 0; + return ret; } =20 static struct devfreq_dev_profile panthor_devfreq_profile =3D { @@ -133,7 +178,51 @@ static struct devfreq_dev_profile panthor_devfreq_prof= ile =3D { .get_cur_freq =3D panthor_devfreq_get_cur_freq, }; =20 -int panthor_devfreq_init(struct panthor_device *ptdev) +static int panthor_devfreq_use_provider(struct panthor_device *ptdev, + struct panthor_devfreq_provider *provider) +{ + struct devfreq_dev_profile *p =3D &panthor_devfreq_profile; + struct device *dev =3D ptdev->base.dev; + unsigned int i; + int ret =3D 0; + + ptdev->devfreq->provider =3D provider; + + if (provider->exit) + p->exit =3D panthor_devfreq_exit; + + for (i =3D 0; i < provider->num_opps; i++) { + ret =3D dev_pm_opp_add_dynamic(dev, provider->opp_table[i]); + if (ret) { + DRM_DEV_ERROR(dev, "Couldn't add OPP %u: %pe\n", i, ERR_PTR(ret)); + return ret; + } + } + + p->max_state =3D provider->num_opps; + + if (provider->num_opps) + ptdev->fast_rate =3D provider->opp_table[provider->num_opps - 1]->freq; + + return ret; +} + +static int panthor_devfreq_init_provider(struct panthor_device *ptdev, + struct device *provider_dev) +{ + struct panthor_devfreq_provider *prov; + + guard(mutex)(&panthor_devfreq_providers_lock); + + list_for_each_entry(prov, &panthor_devfreq_providers, node) { + if (prov->dev =3D=3D provider_dev) + return panthor_devfreq_use_provider(ptdev, prov); + } + + return -EPROBE_DEFER; +} + +static int panthor_devfreq_init_of(struct panthor_device *ptdev) { /* There's actually 2 regulators (mali and sram), but the OPP core only * supports one. @@ -142,20 +231,12 @@ int panthor_devfreq_init(struct panthor_device *ptdev) * the coupling logic deal with voltage updates. */ static const char * const reg_names[] =3D { "mali", NULL }; - struct thermal_cooling_device *cooling; struct device *dev =3D ptdev->base.dev; - struct panthor_devfreq *pdevfreq; struct dev_pm_opp *opp; unsigned long cur_freq; unsigned long freq =3D ULONG_MAX; int ret; =20 - pdevfreq =3D drmm_kzalloc(&ptdev->base, sizeof(*ptdev->devfreq), GFP_KERN= EL); - if (!pdevfreq) - return -ENOMEM; - - ptdev->devfreq =3D pdevfreq; - ret =3D devm_pm_opp_set_regulators(dev, reg_names); if (ret) { if (ret !=3D -EPROBE_DEFER) @@ -168,10 +249,6 @@ int panthor_devfreq_init(struct panthor_device *ptdev) if (ret) return ret; =20 - spin_lock_init(&pdevfreq->lock); - - panthor_devfreq_reset(pdevfreq); - cur_freq =3D clk_get_rate(ptdev->clks.core); =20 /* Regulator coupling only takes care of synchronizing/balancing voltage @@ -229,6 +306,61 @@ int panthor_devfreq_init(struct panthor_device *ptdev) =20 dev_pm_opp_put(opp); =20 + return 0; +} + +static int panthor_devfreq_init_platform(struct panthor_device *ptdev) +{ + struct device_node *pcnode; + struct platform_device *pdev; + struct device_link *link; + int ret; + + pcnode =3D of_parse_phandle(ptdev->base.dev->of_node, + "performance-domains", 0); + if (!pcnode) + return -EINVAL; + + pdev =3D of_find_device_by_node(pcnode); + of_node_put(pcnode); + if (!pdev) + return -ENODEV; + + link =3D device_link_add(ptdev->base.dev, &pdev->dev, + DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); + if (!link) { + dev_err(ptdev->base.dev, "failed to add device link\n"); + return -ENODEV; + } + + ret =3D panthor_devfreq_init_provider(ptdev, &pdev->dev); + if (ret) + return dev_err_probe(ptdev->base.dev, ret, + "failed to initialize devfreq provider\n"); + + DRM_DEV_INFO(ptdev->base.dev, "initialized devfreq provider %s\n", + dev_name(&pdev->dev)); + + return 0; +} + +int panthor_devfreq_init(struct panthor_device *ptdev) +{ + struct thermal_cooling_device *cooling; + struct device *dev =3D ptdev->base.dev; + struct panthor_devfreq *pdevfreq; + int ret; + + pdevfreq =3D drmm_kzalloc(&ptdev->base, sizeof(*ptdev->devfreq), GFP_KERN= EL); + if (!pdevfreq) + return -ENOMEM; + + ptdev->devfreq =3D pdevfreq; + + spin_lock_init(&pdevfreq->lock); + + panthor_devfreq_reset(pdevfreq); + /* * Setup default thresholds for the simple_ondemand governor. * The values are chosen based on experiments. @@ -236,6 +368,14 @@ int panthor_devfreq_init(struct panthor_device *ptdev) pdevfreq->gov_data.upthreshold =3D 45; pdevfreq->gov_data.downdifferential =3D 5; =20 + if (!of_property_present(dev->of_node, "performance-domains")) + ret =3D panthor_devfreq_init_of(ptdev); + else + ret =3D panthor_devfreq_init_platform(ptdev); + + if (ret) + return ret; + pdevfreq->devfreq =3D devm_devfreq_add_device(dev, &panthor_devfreq_profi= le, DEVFREQ_GOV_SIMPLE_ONDEMAND, &pdevfreq->gov_data); diff --git a/drivers/gpu/drm/panthor/panthor_devfreq.h b/drivers/gpu/drm/pa= nthor/panthor_devfreq.h index f8e29e02f66cb3281ed4bb4c75cda9bd4df82b92..777045d406242f46dea376bccb9= 99bfc11f92a7a 100644 --- a/drivers/gpu/drm/panthor/panthor_devfreq.h +++ b/drivers/gpu/drm/panthor/panthor_devfreq.h @@ -10,6 +10,51 @@ struct thermal_cooling_device; struct panthor_device; struct panthor_devfreq; =20 +struct panthor_devfreq_provider { + /** @dev: device pointer to the provider device */ + struct device *dev; + + /** + * @opp_table: table of unique OPPs, sorted from lowest frequency to + * highest. + */ + struct dev_pm_opp_data **opp_table; + + /** @num_opps: number of entries in @opp_table. */ + unsigned int num_opps; + + /** + * @target: &struct devfreq_dev_profile "target" callback to wrap, + * optional. Falls back to panthor internal implementation if absent. + * Passes @dev as the first parameter, not panthor's device. + */ + int (*target)(struct device *dev, unsigned long *freq, u32 flags); + + /** + * @get_dev_status: &struct devfreq_dev_profile "get_dev_status" callback + * to wrap, optional. Falls back to panthor internal implementation if + * absent. Passes @dev as the first parameter, not panthor's device. + */ + int (*get_dev_status)(struct device *dev, + struct devfreq_dev_status *stat); + + /** + * @get_cur_freq: &struct devfreq_dev_profile "get_dev_status" callback + * to wrap, optional. Falls back to panthor internal implementation if + * absent. Passes @dev as the first parameter, not panthor's device. + */ + int (*get_cur_freq)(struct device *dev, unsigned long *freq); + + /** + * @exit: &struct devfreq_dev_profile "exit" callback to wrap, optional. + * Falls back to not registering one an exit function at all if absent. + * Passes @dev as the first parameter, not panthor's device. + */ + void (*exit)(struct device *dev); + + struct list_head node; +}; + int panthor_devfreq_init(struct panthor_device *ptdev); =20 void panthor_devfreq_resume(struct panthor_device *ptdev); @@ -20,4 +65,6 @@ void panthor_devfreq_record_idle(struct panthor_device *p= tdev); =20 unsigned long panthor_devfreq_get_freq(struct panthor_device *ptdev); =20 +int panthor_devfreq_register_provider(struct panthor_devfreq_provider *pro= v); + #endif /* __PANTHOR_DEVFREQ_H__ */ --=20 2.51.0