base-files: reduce IPv6 ULA prefix generation to a single call
[openwrt/staging/stintel.git] / target / linux / ipq806x / patches-6.1 / 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 @@ -151,6 +151,17 @@ config ARM_SUN8I_A33_MBUS_DEVFREQ
25 This adds the DEVFREQ driver for the MBUS controller in some
26 Allwinner sun8i (A33 through H3) and sun50i (A64 and H5) SoCs.
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 @@ -15,6 +15,7 @@ obj-$(CONFIG_ARM_MEDIATEK_CCI_DEVFREQ) +
45 obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) += rk3399_dmc.o
46 obj-$(CONFIG_ARM_SUN8I_A33_MBUS_DEVFREQ) += sun8i-a33-mbus.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,181 @@
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 + int token;
75 +};
76 +
77 +static int krait_cache_config_clk(struct device *dev, struct opp_table *opp_table,
78 + struct dev_pm_opp *old_opp, struct dev_pm_opp *opp,
79 + void *data, bool scaling_down)
80 +{
81 + struct krait_cache_data *kdata;
82 + unsigned long old_freq, freq;
83 + unsigned long idle_freq;
84 + struct clk *clk;
85 + int ret;
86 +
87 + kdata = dev_get_drvdata(dev);
88 + idle_freq = kdata->idle_freq;
89 + clk = kdata->clk;
90 +
91 + old_freq = dev_pm_opp_get_freq(old_opp);
92 + freq = dev_pm_opp_get_freq(opp);
93 +
94 + /*
95 + * Set to idle bin if switching from normal to high bin
96 + * or vice versa. It has been notice that a bug is triggered
97 + * in cache scaling when more than one bin is scaled, to fix
98 + * this we first need to transition to the base rate and then
99 + * to target rate
100 + */
101 + if (likely(freq != idle_freq && old_freq != idle_freq)) {
102 + ret = clk_set_rate(clk, idle_freq);
103 + if (ret)
104 + return ret;
105 + }
106 +
107 + return clk_set_rate(clk, freq);
108 +};
109 +
110 +static int krait_cache_get_cur_freq(struct device *dev, unsigned long *freq)
111 +{
112 + struct krait_cache_data *data = dev_get_drvdata(dev);
113 +
114 + *freq = clk_get_rate(data->clk);
115 +
116 + return 0;
117 +};
118 +
119 +static int krait_cache_target(struct device *dev, unsigned long *freq,
120 + u32 flags)
121 +{
122 + struct dev_pm_opp *opp;
123 +
124 + opp = dev_pm_opp_find_freq_ceil(dev, freq);
125 + if (unlikely(IS_ERR(opp)))
126 + return PTR_ERR(opp);
127 +
128 + dev_pm_opp_put(opp);
129 +
130 + return dev_pm_opp_set_rate(dev, *freq);
131 +};
132 +
133 +static int krait_cache_get_dev_status(struct device *dev,
134 + struct devfreq_dev_status *stat)
135 +{
136 + struct krait_cache_data *data = dev_get_drvdata(dev);
137 +
138 + stat->busy_time = 0;
139 + stat->total_time = 0;
140 + stat->current_frequency = clk_get_rate(data->clk);
141 +
142 + return 0;
143 +};
144 +
145 +static struct devfreq_dev_profile krait_cache_devfreq_profile = {
146 + .target = krait_cache_target,
147 + .get_dev_status = krait_cache_get_dev_status,
148 + .get_cur_freq = krait_cache_get_cur_freq
149 +};
150 +
151 +static struct devfreq_passive_data devfreq_gov_data = {
152 + .parent_type = CPUFREQ_PARENT_DEV,
153 +};
154 +
155 +static int krait_cache_probe(struct platform_device *pdev)
156 +{
157 + struct dev_pm_opp_config config = { };
158 + struct device *dev = &pdev->dev;
159 + struct krait_cache_data *data;
160 + struct devfreq *devfreq;
161 + struct dev_pm_opp *opp;
162 + struct clk *clk;
163 + int ret, token;
164 +
165 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
166 + if (!data)
167 + return -ENOMEM;
168 +
169 + clk = devm_clk_get(dev, "l2");
170 + if (IS_ERR(clk))
171 + return PTR_ERR(clk);
172 +
173 + config.regulator_names = (const char *[]){ "l2", NULL };
174 + config.clk_names = (const char *[]){ "l2", NULL };
175 + config.config_clks = krait_cache_config_clk;
176 +
177 + token = dev_pm_opp_set_config(dev, &config);
178 + if (token < 0)
179 + return token;
180 +
181 + ret = devm_pm_opp_of_add_table(dev);
182 + if (ret)
183 + goto free_opp;
184 +
185 + opp = dev_pm_opp_find_freq_ceil(dev, &data->idle_freq);
186 + if (IS_ERR(opp)) {
187 + ret = PTR_ERR(opp);
188 + goto free_opp;
189 + }
190 + dev_pm_opp_put(opp);
191 +
192 + data->token = token;
193 + data->clk = clk;
194 + dev_set_drvdata(dev, data);
195 + devfreq = devm_devfreq_add_device(dev, &krait_cache_devfreq_profile,
196 + DEVFREQ_GOV_PASSIVE, &devfreq_gov_data);
197 + if (IS_ERR(devfreq)) {
198 + ret = PTR_ERR(devfreq);
199 + goto free_opp;
200 + }
201 +
202 + return 0;
203 +
204 +free_opp:
205 + dev_pm_opp_clear_config(token);
206 + return ret;
207 +};
208 +
209 +static int krait_cache_remove(struct platform_device *pdev)
210 +{
211 + struct krait_cache_data *data = dev_get_drvdata(&pdev->dev);
212 +
213 + dev_pm_opp_clear_config(data->token);
214 +
215 + return 0;
216 +};
217 +
218 +static const struct of_device_id krait_cache_match_table[] = {
219 + { .compatible = "qcom,krait-cache" },
220 + {}
221 +};
222 +
223 +static struct platform_driver krait_cache_driver = {
224 + .probe = krait_cache_probe,
225 + .remove = krait_cache_remove,
226 + .driver = {
227 + .name = "krait-cache-scaling",
228 + .of_match_table = krait_cache_match_table,
229 + },
230 +};
231 +module_platform_driver(krait_cache_driver);
232 +
233 +MODULE_DESCRIPTION("Krait CPU Cache Scaling driver");
234 +MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
235 +MODULE_LICENSE("GPL v2");