ipq806x: move to kernel 6.1 by default
[openwrt/openwrt.git] / target / linux / ipq806x / patches-5.15 / 114-01-devfreq-qcom-Add-L2-Krait-Cache-devfreq-scaling-driv.patch
1 From b044ae89862132a86fb511648e9c52ea3cdf8c30 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Wed, 5 Aug 2020 14:19:23 +0200
4 Subject: [PATCH 1/4] devfreq: qcom: Add L2 Krait Cache devfreq scaling driver
5
6 Qcom L2 Krait CPUs use the generic cpufreq-dt driver and doesn't actually
7 scale the Cache frequency when the CPU frequency is changed. This
8 devfreq driver register with the cpu notifier and scale the Cache
9 based on the max Freq across all core as the CPU cache is shared across
10 all of them. If provided this also scale the voltage of the regulator
11 attached to the CPU cache. The scaling logic is based on the CPU freq
12 and the 3 scaling interval are set by the device dts.
13
14 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
15 ---
16 drivers/devfreq/Kconfig | 11 ++
17 drivers/devfreq/Makefile | 1 +
18 drivers/devfreq/krait-cache-devfreq.c | 188 ++++++++++++++++++++++++++
19 3 files changed, 200 insertions(+)
20 create mode 100644 drivers/devfreq/krait-cache-devfreq.c
21
22 --- a/drivers/devfreq/Kconfig
23 +++ b/drivers/devfreq/Kconfig
24 @@ -132,6 +132,17 @@ config ARM_RK3399_DMC_DEVFREQ
25 It sets the frequency for the memory controller and reads the usage counts
26 from hardware.
27
28 +config ARM_KRAIT_CACHE_DEVFREQ
29 + tristate "Scaling support for Krait CPU Cache Devfreq"
30 + depends on ARCH_QCOM || COMPILE_TEST
31 + select DEVFREQ_GOV_PASSIVE
32 + help
33 + This adds the DEVFREQ driver for the Krait CPU L2 Cache shared by all cores.
34 +
35 + The driver register with the cpufreq notifier and find the right frequency
36 + based on the max frequency across all core and the range set in the device
37 + dts. If provided this scale also the regulator attached to the l2 cache.
38 +
39 source "drivers/devfreq/event/Kconfig"
40
41 endif # PM_DEVFREQ
42 --- a/drivers/devfreq/Makefile
43 +++ b/drivers/devfreq/Makefile
44 @@ -13,6 +13,7 @@ obj-$(CONFIG_ARM_IMX_BUS_DEVFREQ) += imx
45 obj-$(CONFIG_ARM_IMX8M_DDRC_DEVFREQ) += imx8m-ddrc.o
46 obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) += rk3399_dmc.o
47 obj-$(CONFIG_ARM_TEGRA_DEVFREQ) += tegra30-devfreq.o
48 +obj-$(CONFIG_ARM_KRAIT_CACHE_DEVFREQ) += krait-cache-devfreq.o
49
50 # DEVFREQ Event Drivers
51 obj-$(CONFIG_PM_DEVFREQ_EVENT) += event/
52 --- /dev/null
53 +++ b/drivers/devfreq/krait-cache-devfreq.c
54 @@ -0,0 +1,188 @@
55 +// SPDX-License-Identifier: GPL-2.0
56 +
57 +#include <linux/kernel.h>
58 +#include <linux/init.h>
59 +#include <linux/module.h>
60 +#include <linux/cpufreq.h>
61 +#include <linux/devfreq.h>
62 +#include <linux/of.h>
63 +#include <linux/platform_device.h>
64 +#include <linux/clk.h>
65 +#include <linux/slab.h>
66 +#include <linux/regulator/consumer.h>
67 +#include <linux/pm_opp.h>
68 +
69 +#include "governor.h"
70 +
71 +struct krait_cache_data {
72 + struct clk *clk;
73 + unsigned long idle_freq;
74 +};
75 +
76 +static int krait_cache_set_opp(struct dev_pm_set_opp_data *data)
77 +{
78 + unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
79 + struct dev_pm_opp_supply *supply = &data->new_opp.supplies[0];
80 + struct regulator *reg = data->regulators[0];
81 + struct krait_cache_data *kdata;
82 + struct clk *clk = data->clk;
83 + unsigned long idle_freq;
84 + int ret;
85 +
86 + kdata = dev_get_drvdata(data->dev);
87 + idle_freq = kdata->idle_freq;
88 +
89 + if (reg) {
90 + ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
91 + supply->u_volt,
92 + supply->u_volt_max);
93 + if (ret)
94 + goto exit;
95 + }
96 +
97 + /*
98 + * Set to idle bin if switching from normal to high bin
99 + * or vice versa. It has been notice that a bug is triggered
100 + * in cache scaling when more than one bin is scaled, to fix
101 + * this we first need to transition to the base rate and then
102 + * to target rate
103 + */
104 + if (likely(freq != idle_freq && old_freq != idle_freq)) {
105 + ret = clk_set_rate(clk, idle_freq);
106 + if (ret)
107 + goto exit;
108 + }
109 +
110 + ret = clk_set_rate(clk, freq);
111 + if (ret)
112 + goto exit;
113 +
114 +exit:
115 + return ret;
116 +};
117 +
118 +static int krait_cache_get_cur_freq(struct device *dev, unsigned long *freq)
119 +{
120 + struct krait_cache_data *data = dev_get_drvdata(dev);
121 +
122 + *freq = clk_get_rate(data->clk);
123 +
124 + return 0;
125 +};
126 +
127 +static int krait_cache_target(struct device *dev, unsigned long *freq,
128 + u32 flags)
129 +{
130 + struct dev_pm_opp *opp;
131 +
132 + opp = dev_pm_opp_find_freq_ceil(dev, freq);
133 + if (unlikely(IS_ERR(opp)))
134 + return PTR_ERR(opp);
135 +
136 + dev_pm_opp_put(opp);
137 +
138 + return dev_pm_opp_set_rate(dev, *freq);
139 +};
140 +
141 +static int krait_cache_get_dev_status(struct device *dev,
142 + struct devfreq_dev_status *stat)
143 +{
144 + struct krait_cache_data *data = dev_get_drvdata(dev);
145 +
146 + stat->busy_time = 0;
147 + stat->total_time = 0;
148 + stat->current_frequency = clk_get_rate(data->clk);
149 +
150 + return 0;
151 +};
152 +
153 +static struct devfreq_dev_profile krait_cache_devfreq_profile = {
154 + .target = krait_cache_target,
155 + .get_dev_status = krait_cache_get_dev_status,
156 + .get_cur_freq = krait_cache_get_cur_freq
157 +};
158 +
159 +static struct devfreq_passive_data devfreq_gov_data = {
160 + .parent_type = CPUFREQ_PARENT_DEV,
161 +};
162 +
163 +static int krait_cache_probe(struct platform_device *pdev)
164 +{
165 + struct device *dev = &pdev->dev;
166 + struct krait_cache_data *data;
167 + struct opp_table *table;
168 + struct devfreq *devfreq;
169 + struct dev_pm_opp *opp;
170 + struct clk *clk;
171 + int ret;
172 +
173 + krait_cache_devfreq_profile.freq_table = NULL;
174 +
175 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
176 + if (!data)
177 + return -ENOMEM;
178 +
179 + clk = devm_clk_get(dev, "l2");
180 + if (IS_ERR(clk))
181 + return PTR_ERR(clk);
182 +
183 + table = dev_pm_opp_set_regulators(dev, (const char *[]){ "l2" }, 1);
184 + if (IS_ERR(table)) {
185 + ret = PTR_ERR(table);
186 + dev_err_probe(dev, -EPROBE_DEFER, "failed to set regulators %d\n", ret);
187 + return ret;
188 + }
189 +
190 + ret = PTR_ERR_OR_ZERO(
191 + dev_pm_opp_register_set_opp_helper(dev, krait_cache_set_opp));
192 + if (ret)
193 + return ret;
194 +
195 + ret = dev_pm_opp_of_add_table(dev);
196 + if (ret) {
197 + dev_err(dev, "failed to parse L2 freq thresholds\n");
198 + return ret;
199 + }
200 +
201 + data->clk = clk;
202 + opp = dev_pm_opp_find_freq_ceil(dev, &data->idle_freq);
203 + dev_pm_opp_put(opp);
204 +
205 + dev_set_drvdata(dev, data);
206 +
207 + devfreq = devm_devfreq_add_device(&pdev->dev, &krait_cache_devfreq_profile,
208 + DEVFREQ_GOV_PASSIVE, &devfreq_gov_data);
209 + if (IS_ERR(devfreq)) {
210 + dev_pm_opp_remove_table(dev);
211 + dev_pm_opp_put_regulators(table);
212 + dev_pm_opp_unregister_set_opp_helper(table);
213 + }
214 +
215 + return PTR_ERR_OR_ZERO(devfreq);
216 +};
217 +
218 +static int krait_cache_remove(struct platform_device *pdev)
219 +{
220 + dev_pm_opp_remove_table(&pdev->dev);
221 +
222 + return 0;
223 +};
224 +
225 +static const struct of_device_id krait_cache_match_table[] = {
226 + { .compatible = "qcom,krait-cache" },
227 + {}
228 +};
229 +
230 +static struct platform_driver krait_cache_driver = {
231 + .probe = krait_cache_probe,
232 + .remove = krait_cache_remove,
233 + .driver = {
234 + .name = "krait-cache-scaling",
235 + .of_match_table = krait_cache_match_table,
236 + },
237 +};
238 +module_platform_driver(krait_cache_driver);
239 +
240 +MODULE_DESCRIPTION("Krait CPU Cache Scaling driver");
241 +MODULE_AUTHOR("Christian 'Ansuel' Marangi <ansuelsmth@gmail.com>");
242 +MODULE_LICENSE("GPL v2");