1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2013-2014, 2018-2020, The Linux Foundation. All rights reserved.
*/
#define pr_fmt(fmt) "devfreq-icc: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/ktime.h>
#include <linux/time.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/devfreq.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_fdt.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <trace/events/power.h>
#include <linux/platform_device.h>
#include <linux/interconnect.h>
#include <soc/qcom/devfreq_icc.h>
/* Has to be ULL to prevent overflow where this macro is used. */
#define MBYTE (1ULL << 20)
#define HZ_TO_MBPS(hz, w) (mult_frac(w, hz, MBYTE))
#define MBPS_TO_HZ(mbps, w) (mult_frac(mbps, MBYTE, w))
enum dev_type {
STD_MBPS_DEV,
L3_HZ_DEV,
L3_MBPS_DEV,
NUM_DEV_TYPES
};
struct devfreq_icc_spec {
enum dev_type type;
};
struct dev_data {
struct icc_path *icc_path;
u32 cur_ab;
u32 cur_ib;
unsigned long gov_ab;
const struct devfreq_icc_spec *spec;
unsigned int width;
struct devfreq *df;
struct devfreq_dev_profile dp;
};
#define MAX_L3_ENTRIES 40U
static unsigned long l3_freqs[MAX_L3_ENTRIES];
static DEFINE_MUTEX(l3_freqs_lock);
static bool use_cached_l3_freqs;
static int set_bw(struct device *dev, u32 new_ib, u32 new_ab)
{
struct dev_data *d = dev_get_drvdata(dev);
int ret;
u32 icc_ib = new_ib, icc_ab = new_ab;
if (d->cur_ib == new_ib && d->cur_ab == new_ab)
return 0;
if (d->spec->type == L3_MBPS_DEV) {
icc_ib = MBPS_TO_HZ(new_ib, d->width);
icc_ab = MBPS_TO_HZ(new_ab, d->width);
} else if (d->spec->type == STD_MBPS_DEV) {
icc_ib = Bps_to_icc(new_ib * MBYTE);
icc_ab = Bps_to_icc(new_ab * MBYTE);
}
dev_dbg(dev, "ICC BW: AB: %u IB: %u\n", icc_ab, icc_ib);
ret = icc_set_bw(d->icc_path, icc_ab, icc_ib);
if (ret < 0) {
dev_err(dev, "icc set bandwidth request failed (%d)\n", ret);
} else {
d->cur_ib = new_ib;
d->cur_ab = new_ab;
}
return ret;
}
static int icc_target(struct device *dev, unsigned long *freq, u32 flags)
{
struct dev_data *d = dev_get_drvdata(dev);
struct dev_pm_opp *opp;
opp = devfreq_recommended_opp(dev, freq, flags);
if (!IS_ERR(opp))
dev_pm_opp_put(opp);
return set_bw(dev, *freq, d->gov_ab);
}
static int icc_get_dev_status(struct device *dev,
struct devfreq_dev_status *stat)
{
struct dev_data *d = dev_get_drvdata(dev);
stat->private_data = &d->gov_ab;
return 0;
}
#define INIT_HZ 300000000UL
#define XO_HZ 19200000UL
#define FTBL_ROW_SIZE 4
#define SRC_MASK GENMASK(31, 30)
#define SRC_SHIFT 30
#define MULT_MASK GENMASK(7, 0)
static int populate_l3_opp_table(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
int idx, ret;
u32 data, src, mult, i;
unsigned long freq, prev_freq = 0;
struct resource res;
void __iomem *ftbl_base;
unsigned int ftbl_row_size = FTBL_ROW_SIZE;
idx = of_property_match_string(dev->of_node, "reg-names", "ftbl-base");
if (idx < 0) {
dev_err(dev, "Unable to find ftbl-base: %d\n", idx);
return -EINVAL;
}
ret = of_address_to_resource(dev->of_node, idx, &res);
if (ret < 0) {
dev_err(dev, "Unable to get resource from address: %d\n", ret);
return -EINVAL;
}
ftbl_base = devm_ioremap(dev, res.start, resource_size(&res));
if (!ftbl_base) {
dev_err(dev, "Unable to map ftbl-base!\n");
return -ENOMEM;
}
of_property_read_u32(dev->of_node, "qcom,ftbl-row-size",
&ftbl_row_size);
for (i = 0; i < MAX_L3_ENTRIES; i++) {
data = readl_relaxed(ftbl_base + i * ftbl_row_size);
src = ((data & SRC_MASK) >> SRC_SHIFT);
mult = (data & MULT_MASK);
freq = src ? XO_HZ * mult : INIT_HZ;
/* Two of the same frequencies means end of table */
if (i > 0 && prev_freq == freq)
break;
if (d->spec->type == L3_MBPS_DEV)
dev_pm_opp_add(dev, HZ_TO_MBPS(freq, d->width), 0);
else
dev_pm_opp_add(dev, freq, 0);
l3_freqs[i] = freq;
prev_freq = freq;
}
devm_iounmap(dev, ftbl_base);
use_cached_l3_freqs = true;
return 0;
}
static int copy_l3_opp_table(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
int idx;
for (idx = 0; idx < MAX_L3_ENTRIES; idx++) {
if (!l3_freqs[idx])
break;
if (d->spec->type == L3_MBPS_DEV)
dev_pm_opp_add(dev,
HZ_TO_MBPS(l3_freqs[idx], d->width), 0);
else
dev_pm_opp_add(dev, l3_freqs[idx], 0);
}
if (!idx) {
dev_err(dev, "No L3 frequencies copied for device!\n");
return -EINVAL;
}
return 0;
}
#define PROP_ACTIVE "qcom,active-only"
#define ACTIVE_ONLY_TAG 0x3
int devfreq_add_icc(struct device *dev)
{
struct dev_data *d;
struct devfreq_dev_profile *p;
const char *gov_name;
int ret;
struct opp_table *opp_table;
u32 version;
d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
if (!d)
return -ENOMEM;
dev_set_drvdata(dev, d);
d->spec = of_device_get_match_data(dev);
if (!d->spec) {
dev_err(dev, "Unknown device type!\n");
return -ENODEV;
}
p = &d->dp;
p->polling_ms = 500;
p->target = icc_target;
p->get_dev_status = icc_get_dev_status;
if (of_device_is_compatible(dev->of_node, "qcom,devfreq-icc-ddr")) {
version = (1 << of_fdt_get_ddrtype());
opp_table = dev_pm_opp_set_supported_hw(dev, &version, 1);
if (IS_ERR(opp_table)) {
dev_err(dev, "Failed to set supported hardware\n");
return PTR_ERR(opp_table);
}
}
if (d->spec->type == L3_MBPS_DEV) {
ret = of_property_read_u32(dev->of_node, "qcom,bus-width",
&d->width);
if (ret < 0 || !d->width) {
dev_err(dev, "Missing or invalid bus-width: %d\n", ret);
return -EINVAL;
}
}
if (d->spec->type == L3_HZ_DEV || d->spec->type == L3_MBPS_DEV) {
mutex_lock(&l3_freqs_lock);
if (use_cached_l3_freqs) {
mutex_unlock(&l3_freqs_lock);
ret = copy_l3_opp_table(dev);
} else {
ret = populate_l3_opp_table(dev);
mutex_unlock(&l3_freqs_lock);
}
} else {
ret = dev_pm_opp_of_add_table(dev);
}
if (ret < 0)
dev_err(dev, "Couldn't parse OPP table:%d\n", ret);
d->icc_path = of_icc_get(dev, NULL);
if (IS_ERR(d->icc_path)) {
ret = PTR_ERR(d->icc_path);
if (ret != -EPROBE_DEFER)
dev_err(dev, "Unable to register icc path: %d\n", ret);
return ret;
}
if (of_property_read_bool(dev->of_node, PROP_ACTIVE))
icc_set_tag(d->icc_path, ACTIVE_ONLY_TAG);
if (of_property_read_string(dev->of_node, "governor", &gov_name))
gov_name = "performance";
d->df = devfreq_add_device(dev, p, gov_name, NULL);
if (IS_ERR(d->df)) {
icc_put(d->icc_path);
return PTR_ERR(d->df);
}
return 0;
}
int devfreq_remove_icc(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
icc_put(d->icc_path);
devfreq_remove_device(d->df);
return 0;
}
int devfreq_suspend_icc(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
return devfreq_suspend_device(d->df);
}
int devfreq_resume_icc(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
return devfreq_resume_device(d->df);
}
static int devfreq_icc_probe(struct platform_device *pdev)
{
return devfreq_add_icc(&pdev->dev);
}
static int devfreq_icc_remove(struct platform_device *pdev)
{
return devfreq_remove_icc(&pdev->dev);
}
static const struct devfreq_icc_spec spec[] = {
[0] = { STD_MBPS_DEV },
[1] = { L3_HZ_DEV },
[2] = { L3_MBPS_DEV },
};
static const struct of_device_id devfreq_icc_match_table[] = {
{ .compatible = "qcom,devfreq-icc-l3bw", .data = &spec[2] },
{ .compatible = "qcom,devfreq-icc-l3", .data = &spec[1] },
{ .compatible = "qcom,devfreq-icc-llcc", .data = &spec[0] },
{ .compatible = "qcom,devfreq-icc-ddr", .data = &spec[0] },
{ .compatible = "qcom,devfreq-icc", .data = &spec[0] },
{}
};
static struct platform_driver devfreq_icc_driver = {
.probe = devfreq_icc_probe,
.remove = devfreq_icc_remove,
.driver = {
.name = "devfreq-icc",
.of_match_table = devfreq_icc_match_table,
.suppress_bind_attrs = true,
},
};
static int __init devfreq_icc_init(void)
{
int ret;
ret = platform_driver_register(&devfreq_icc_driver);
if (ret)
pr_err("devfreq_icc register failed %d\n", ret);
return ret;
}
late_initcall(devfreq_icc_init);
static __exit void devfreq_icc_exit(void)
{
platform_driver_unregister(&devfreq_icc_driver);
}
module_exit(devfreq_icc_exit);
MODULE_DESCRIPTION("Device DDR bandwidth voting driver MSM SoCs");
MODULE_LICENSE("GPL v2");
|