devfreq: Tegra devfreq fixes / improvements and Tegra30 support
cover devfreq: Tegra devfreq fixes / improvements and Tegra30 support
Hello,
I tried to utilize the Tegra devfreq driver on Tegra30 and found out that it doesn't work properly due to improper Hz<->kHz conversions made by the driver. After fixing that problem and doing some more testing I noticed that there are things that could be improved and in result here is this patchset that fixes the problems, makes some improvements and adds support for NVIDIA Tegra30 SoC's. This series was tested on Tegra30 and Tegra124 machines.
Dmitry Osipenko (8):
PM / devfreq: tegra: Fix kHz to Hz conversion
PM / devfreq: tegra: Replace readl-writel with relaxed versions PM / devfreq: tegra: Don't ignore clk errors
PM / devfreq: tegra: Don't release IRQ manually on driver removal PM / devfreq: tegra: Don't set EMC clock rate to maximum on probe PM / devfreq: tegra: Drop spinlock
PM / devfreq: tegra: Remove OPP entries on driver removal PM / devfreq: tegra: Support Tegra30
drivers/devfreq/Kconfig | 2 +- drivers/devfreq/tegra-devfreq.c | 66 ++++++++++++++------------------- 2 files changed, 29 insertions(+), 39 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/8 PM / devfreq: tegra: Fix kHz to Hz conversion
The kHz to Hz is incorrectly converted in a few places in the code, this results in a wrong frequency being calculated because devfreq core uses OPP frequencies that are given in Hz to clamp the rate, while tegra-devfreq gives to the core value in kHz and then it also expects to receive value in kHz from the core. In a result memory freq is always set to a value which is close to ULONG_MAX because of the bug. Hence the EMC frequency is always capped to the maximum and the driver doesn't do anything useful. Let's provide OPP with rates in kHz since this eliminates few multiplies and divisions in the code. This patch was tested on Tegra30 and Tegra124 SoC's, EMC frequency scaling works properly now.
--- drivers/devfreq/tegra-devfreq.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/devfreq/tegra-devfreq.c +5 -7
@@ -394,7 +394,7 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb, tegra = container_of(nb, struct tegra_devfreq, rate_change_nb); - tegra->cur_freq = data->new_rate /KHZ ; + tegra->cur_freq = data->new_rate;for (i =0 ; i <ARRAY_SIZE (tegra->devices); i++) { dev = &tegra->devices[i];
@@ -486,21 +486,19 @@ static int tegra_devfreq_target(struct device *dev, unsigned long *freq, {struct tegra_devfreq *tegra =dev_get_drvdata (dev);struct dev_pm_opp *opp; -unsigned long rate = *freq *KHZ ; +unsigned long rate; - opp =devfreq_recommended_opp (dev, &rate, flags); + opp =devfreq_recommended_opp (dev, freq, flags);if (IS_ERR (opp)) {dev_err (dev,"Failed to find opp for %lu KHz\n" , *freq);return PTR_ERR (opp); } - rate =dev_pm_opp_get_freq (opp); + rate =dev_pm_opp_get_freq (opp) *KHZ ;dev_pm_opp_put (opp);clk_set_min_rate (tegra->emc_clock, rate);clk_set_rate (tegra->emc_clock,0 ); - *freq = rate; -return 0 ; }
@@ -682,7 +680,7 @@ static int tegra_devfreq_probe(struct platform_device *pdev)for (rate =0 ; rate <= tegra->max_freq *KHZ ; rate++) { rate =clk_round_rate (tegra->emc_clock, rate); -dev_pm_opp_add (&pdev->dev, rate,0 ); +dev_pm_opp_add (&pdev->dev, rate /KHZ ,0 ); } irq =platform_get_irq (pdev,0 );
Signature · 1 line
2.21.0
PATCH 2/8 PM / devfreq: tegra: Replace readl-writel with relaxed versions
In reply to Dmitry OsipenkoThere is no need to insert memory barrier on each readl/writel invocation, hence use the relaxed versions.
--- drivers/devfreq/tegra-devfreq.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/devfreq/tegra-devfreq.c +4 -4
@@ -191,23 +191,23 @@ static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {static u32 actmon_readl (struct tegra_devfreq *tegra,u32 offset) { -return readl (tegra->regs + offset); +return readl_relaxed (tegra->regs + offset); }static void actmon_writel (struct tegra_devfreq *tegra,u32 val,u32 offset) { -writel (val, tegra->regs + offset); +writel_relaxed (val, tegra->regs + offset); }static u32 device_readl (struct tegra_devfreq_device *dev,u32 offset) { -return readl (dev->regs + offset); +return readl_relaxed (dev->regs + offset); }static void device_writel (struct tegra_devfreq_device *dev,u32 val,u32 offset) { -writel (val, dev->regs + offset); +writel_relaxed (val, dev->regs + offset); }static unsigned long do_percent (unsigned long val,unsigned int pct)
Signature · 1 line
2.21.0
PATCH 3/8 PM / devfreq: tegra: Don't ignore clk errors
In reply to Dmitry OsipenkoThe clk_set_min_rate() could fail and in this case clk_set_rate() sets rate to 0, which may drop EMC rate to minimum and make machine very difficult to use.
--- drivers/devfreq/tegra-devfreq.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/devfreq/tegra-devfreq.c +8 -2
@@ -487,6 +487,7 @@ static int tegra_devfreq_target(struct device *dev, unsigned long *freq,struct tegra_devfreq *tegra =dev_get_drvdata (dev);struct dev_pm_opp *opp;unsigned long rate; +int err; opp =devfreq_recommended_opp (dev, freq, flags);if (IS_ERR (opp)) {
@@ -496,8 +497,13 @@ static int tegra_devfreq_target(struct device *dev, unsigned long *freq, rate =dev_pm_opp_get_freq (opp) *KHZ ;dev_pm_opp_put (opp); -clk_set_min_rate (tegra->emc_clock, rate); -clk_set_rate (tegra->emc_clock,0 ); + err =clk_set_min_rate (tegra->emc_clock, rate); +if (err) +return err; + + err =clk_set_rate (tegra->emc_clock,0 ); +if (err) +return err;return 0 ; }
Signature · 1 line
2.21.0
PATCH 4/8 PM / devfreq: tegra: Don't release IRQ manually on driver removal
In reply to Dmitry OsipenkoThe IRQ releasing is handled by "managed resources", hence there is no need to release IRQ manually.
--- drivers/devfreq/tegra-devfreq.c | 3 --- 1 file changed, 3 deletions(-)
drivers/devfreq/tegra-devfreq.c +0 -3
@@ -717,7 +717,6 @@ static int tegra_devfreq_probe(struct platform_device *pdev)static int tegra_devfreq_remove (struct platform_device *pdev) {struct tegra_devfreq *tegra =platform_get_drvdata (pdev); -int irq =platform_get_irq (pdev,0 );u32 val;unsigned int i;
@@ -729,8 +728,6 @@ static int tegra_devfreq_remove(struct platform_device *pdev)actmon_write_barrier (tegra); -devm_free_irq (&pdev->dev, irq, tegra); -clk_notifier_unregister (tegra->emc_clock, &tegra->rate_change_nb);clk_disable_unprepare (tegra->clock);
Signature · 1 line
2.21.0
PATCH 5/8 PM / devfreq: tegra: Don't set EMC clock rate to maximum on probe
In reply to Dmitry OsipenkoThere is no real benefit from doing so, hence let's drop that rate setting for consistency.
--- drivers/devfreq/tegra-devfreq.c | 2 -- 1 file changed, 2 deletions(-)
drivers/devfreq/tegra-devfreq.c +0 -2
@@ -648,8 +648,6 @@ static int tegra_devfreq_probe(struct platform_device *pdev)return PTR_ERR (tegra->emc_clock); } -clk_set_rate (tegra->emc_clock,ULONG_MAX ); - tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb; err =clk_notifier_register (tegra->emc_clock, &tegra->rate_change_nb);if (err) {
Signature · 1 line
2.21.0
PATCH 6/8 PM / devfreq: tegra: Drop spinlock
In reply to Dmitry OsipenkoThere is no real need to use locking since we can simply read out the volatile variable value once and the watermark changing shouldn't clash with the changes made by the interrupt handler.
--- drivers/devfreq/tegra-devfreq.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/devfreq/tegra-devfreq.c +4 -17
@@ -144,7 +144,6 @@ static struct tegra_devfreq_device_config actmon_device_configs[] = {struct tegra_devfreq_device {const struct tegra_devfreq_device_config *config;void __iomem *regs; -spinlock_t lock;/* Average event count sampled in the last interrupt */ u32 avg_count;
@@ -250,11 +249,8 @@ static void actmon_write_barrier(struct tegra_devfreq *tegra)static void actmon_isr_device (struct tegra_devfreq *tegra,struct tegra_devfreq_device *dev) { -unsigned long flags;u32 intr_status, dev_ctrl; -spin_lock_irqsave (&dev->lock, flags); - dev->avg_count =device_readl (dev,ACTMON_DEV_AVG_COUNT );tegra_devfreq_update_avg_wmark (tegra, dev);
@@ -303,8 +299,6 @@ static void actmon_isr_device(struct tegra_devfreq *tegra,device_writel (dev,ACTMON_INTR_STATUS_CLEAR ,ACTMON_DEV_INTR_STATUS );actmon_write_barrier (tegra); - -spin_unlock_irqrestore (&dev->lock, flags); }static irqreturn_t actmon_isr (int irq,void *data)
@@ -349,24 +343,21 @@ static void actmon_update_target(struct tegra_devfreq *tegra,unsigned long cpu_freq =0 ;unsigned long static_cpu_emc_freq =0 ;unsigned int avg_sustain_coef; -unsigned long flags; +u32 avg_count;if (dev->config->avg_dependency_threshold) { cpu_freq =cpufreq_get (0 ); static_cpu_emc_freq =actmon_cpu_to_emc_rate (tegra, cpu_freq); } -spin_lock_irqsave (&dev->lock, flags); - - dev->target_freq = dev->avg_count /ACTMON_SAMPLING_PERIOD ; + avg_count =READ_ONCE (dev->avg_count); + dev->target_freq = avg_count /ACTMON_SAMPLING_PERIOD ; avg_sustain_coef =100 *100 / dev->config->boost_up_threshold; dev->target_freq =do_percent (dev->target_freq, avg_sustain_coef); dev->target_freq += dev->boost_freq; -if (dev->avg_count >= dev->config->avg_dependency_threshold) +if (avg_count >= dev->config->avg_dependency_threshold) dev->target_freq =max (dev->target_freq, static_cpu_emc_freq); - -spin_unlock_irqrestore (&dev->lock, flags); }static irqreturn_t actmon_thread_isr (int irq,void *data)
@@ -387,7 +378,6 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,struct tegra_devfreq *tegra;struct tegra_devfreq_device *dev;unsigned int i; -unsigned long flags;if (action !=POST_RATE_CHANGE )return NOTIFY_OK ;
@@ -399,9 +389,7 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,for (i =0 ; i <ARRAY_SIZE (tegra->devices); i++) { dev = &tegra->devices[i]; -spin_lock_irqsave (&dev->lock, flags);tegra_devfreq_update_wmark (tegra, dev); -spin_unlock_irqrestore (&dev->lock, flags); }actmon_write_barrier (tegra);
@@ -677,7 +665,6 @@ static int tegra_devfreq_probe(struct platform_device *pdev) dev = tegra->devices + i; dev->config = actmon_device_configs + i; dev->regs = tegra->regs + dev->config->offset; -spin_lock_init (&dev->lock);tegra_actmon_configure_device (tegra, dev); }
Signature · 1 line
2.21.0
PATCH 7/8 PM / devfreq: tegra: Remove OPP entries on driver removal
In reply to Dmitry OsipenkoThis fixes "_opp_is_duplicate" warning messages on driver's module reload.
--- drivers/devfreq/tegra-devfreq.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/devfreq/tegra-devfreq.c +7 -4
@@ -691,10 +691,10 @@ static int tegra_devfreq_probe(struct platform_device *pdev) } tegra_devfreq_profile.initial_freq =clk_get_rate (tegra->emc_clock); - tegra->devfreq =devm_devfreq_add_device (&pdev->dev, - &tegra_devfreq_profile, -"tegra_actmon" , -NULL ); + tegra->devfreq =devfreq_add_device (&pdev->dev, + &tegra_devfreq_profile, +"tegra_actmon" , +NULL );return 0 ; }
@@ -705,6 +705,9 @@ static int tegra_devfreq_remove(struct platform_device *pdev)u32 val;unsigned int i; +devfreq_remove_device (tegra->devfreq); +dev_pm_opp_remove_all_dynamic (&pdev->dev); +for (i =0 ; i <ARRAY_SIZE (actmon_device_configs); i++) { val =device_readl (&tegra->devices[i],ACTMON_DEV_CTRL ); val &= ~ACTMON_DEV_CTRL_ENB ;
Signature · 1 line
2.21.0
PATCH 8/8 PM / devfreq: tegra: Support Tegra30
In reply to Dmitry OsipenkoThe devfreq driver can be used on Tegra30 without any code change and it works perfectly fine, the default Tegra124 parameters are good enough for Tegra30.
--- drivers/devfreq/Kconfig | 2 +- drivers/devfreq/tegra-devfreq.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/devfreq/Kconfig +1 -1
@@ -93,7 +93,7 @@ config ARM_EXYNOS_BUS_DEVFREQ config ARM_TEGRA_DEVFREQ tristate "Tegra DEVFREQ Driver" - depends on ARCH_TEGRA_124_SOC + depends on ARCH_TEGRA select DEVFREQ_GOV_SIMPLE_ONDEMAND select PM_OPP help
drivers/devfreq/tegra-devfreq.c +1 -0
@@ -724,6 +724,7 @@ static int tegra_devfreq_remove(struct platform_device *pdev) }static const struct of_device_id tegra_devfreq_of_match[] = { + { .compatible ="nvidia,tegra30-actmon" }, { .compatible ="nvidia,tegra124-actmon" }, { }, };
Signature · 1 line
2.21.0
Re: [PATCH v1 1/8] PM / devfreq: tegra: Fix kHz to Hz conversion
In reply to a message not in the archive12.04.2019 4:34, Sasha Levin пишет:
Quoted 59 lines
Hi,
[This is an automated email]
This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: all
The bot has tested the following trees: v5.0.7, v4.19.34, v4.14.111, v4.9.168, v4.4.178, v3.18.138.
v5.0.7: Build OK!
v4.19.34: Build OK!
v4.14.111: Build OK!
v4.9.168: Failed to apply! Possible dependencies:0f0fe7e01327 ("PM / OPP: Manage supply's voltage/current in a separate structure") 37a73ec0c9bb ("PM / OPP: Add per OPP table mutex") 63a69ea4b88f ("PM / OPP: Rename _allocate_opp() to _opp_allocate()") 7034764a1e4a ("PM / OPP: Add 'struct kref' to struct dev_pm_opp") 8a31d9d94297 ("PM / OPP: Update OPP users to put reference") 947355850fcb ("PM / OPP: Separate out _generic_set_opp()") 969fceb3c7e6 ("PM / OPP: Add light weight _opp_free() routine") ce31781a7574 ("PM / OPP: Pass struct dev_pm_opp_supply to _set_opp_voltage()") dfbe4678d709 ("PM / OPP: Add infrastructure to manage multiple regulators")v4.4.178: Failed to apply! Possible dependencies:
01fb4d3c39d3 ("PM / OPP: Parse 'opp-<prop>-<name>' bindings") 0c717d0f9cb4 ("PM / OPP: Initialize regulator pointer to an error value") 2c2709dc6921 ("PM / OPP: Rename structures for clarity") 50f8cfbd5897 ("PM / OPP: Parse clock-latency and voltage-tolerance for v1 bindings") 655c9df96175 ("PM / OPP: Introduce dev_pm_opp_get_max_volt_latency()") 7de36b0aa51a ("PM / OPP: Parse 'opp-supported-hw' binding") 8a31d9d94297 ("PM / OPP: Update OPP users to put reference") 9f8ea969d5cf ("PM / OPP: get/put regulators from OPP core") d54974c2513f ("PM / OPP: Manage device clk") dc4e7b1fa20a ("PM / OPP: Add missing doc comments") deaa51465105 ("PM / OPP: Add debugfs support")v3.18.138: Failed to apply! Possible dependencies:
07cce74a7b25("PM / OPP: handle allocation ofdevice_oppin a separate routine")0fe30da2cb43("PM / OPP: fix warning inof_free_opp_table()")11573e9132ae("PM / devfreq: tegra: Update to v5 of the submitted patches")129eec55df6a("PM / OPP Introduce APIs to remove OPPs")
29df0ee1b14a("PM / OPP: reusefind_device_opp()instead of duplicating code")2c2709dc6921("PM / OPP: Rename structures for clarity")
327854c87117("PM / OPP: Ensure consistent naming of static functions")38393409da34("PM / OPP mark OPPs as 'static' or 'dynamic'")
6234f38016ad("PM / devfreq: tegra: add devfreq driver for Tegra Activity Monitor")737002b5de3d("PM / OPP: Relocate few routines")
86453b473b1f("PM / OPP: Staticize__dev_pm_opp_remove()")
87b4115db023("PM / OPP: Protect updates tolist_devwith mutex")8a31d9d94297("PM / OPP: Update OPP users to put reference")
8d4d4e98acd6("PM / OPP: Add helpers for initializing CPU OPPs")984f16c8490c("PM / OPP: Update kernel documentation")
a7470db6fec4("PM / OPP don't match for existing OPPs when list is empty")b02ded246d01("PM / OPP: add some lockdep annotations")
b4037aaa584b("PM / OPP replacekfree_rcu()withcall_srcu()inopp_set_availability()")cd1a068a52ee("PM / OPP rename 'head' as 'rcu_head' or 'srcu_head' based on its type")f59d3ee8480d("PM / OPP: Move cpu specific code toopp/cpu.c")
Quoted 1 line
How should we proceed with this patch?
Don't backport to v4.9 and older, it's not a critical fix at all.
Re: [PATCH v1 1/8] PM / devfreq: tegra: Fix kHz to Hz conversion
In reply to Dmitry Osipenko12.04.2019 1:29, Dmitry Osipenko пишет:
Quoted 23 lines
The kHz to Hz is incorrectly converted in a few places in the code, this results in a wrong frequency being calculated because devfreq core uses OPP frequencies that are given in Hz to clamp the rate, while tegra-devfreq gives to the core value in kHz and then it also expects to receive value in kHz from the core. In a result memory freq is always set to a value which is close to
ULONG_MAXbecause of the bug. Hence the EMC frequency is always capped to the maximum and the driver doesn't do anything useful. Let's provide OPP with rates in kHz since this eliminates few multiplies and divisions in the code. This patch was tested on Tegra30 and Tegra124 SoC's, EMC frequency scaling works properly now.Cc <[email protected]>Tested-by Steev Klimaszewski <[email protected]>Signed-off-by Dmitry Osipenko <[email protected]>--- drivers/devfreq/tegra-devfreq.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)drivers/devfreq/tegra-devfreq.c +0
-0
drivers/devfreq/tegra-devfreq.c static int tegra_actmon_rate_notify_cb(struct notifier_block *nb, · line 397
Quoted 5 lines
drivers/devfreq/tegra-devfreq.c +1
-1@@ -394,7 +394,7 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb, tegra = container_of(nb, struct tegra_devfreq, rate_change_nb); - tegra->cur_freq = data->new_rate /KHZ ; + tegra->cur_freq = data->new_rate;
This was a last-minute change and it is incorrect. The cur_freq should be kept in kHz, I'll fix it up in v2 and re-test everything properly once again.