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
|
/*
* Copyright (c) 2013-2014, 2018-2019, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "devbw: " 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 <trace/events/power.h>
#include <linux/msm-bus.h>
#include <linux/msm-bus-board.h>
#include <linux/devfreq_boost.h>
/* Has to be ULL to prevent overflow where this macro is used. */
#define MBYTE (1ULL << 20)
#define MAX_PATHS 2
#define DBL_BUF 2
struct dev_data {
struct msm_bus_vectors vectors[MAX_PATHS * DBL_BUF];
struct msm_bus_paths bw_levels[DBL_BUF];
struct msm_bus_scale_pdata bw_data;
int num_paths;
u32 bus_client;
int cur_idx;
int cur_ab;
int cur_ib;
long gov_ab;
struct devfreq *df;
struct devfreq_dev_profile dp;
};
static int set_bw(struct device *dev, int new_ib, int new_ab)
{
struct dev_data *d = dev_get_drvdata(dev);
int i, ret;
if (d->cur_ib == new_ib && d->cur_ab == new_ab)
return 0;
i = (d->cur_idx + 1) % DBL_BUF;
d->bw_levels[i].vectors[0].ib = new_ib * MBYTE;
d->bw_levels[i].vectors[0].ab = new_ab / d->num_paths * MBYTE;
d->bw_levels[i].vectors[1].ib = new_ib * MBYTE;
d->bw_levels[i].vectors[1].ab = new_ab / d->num_paths * MBYTE;
dev_dbg(dev, "BW MBps: AB: %d IB: %d\n", new_ab, new_ib);
ret = msm_bus_scale_client_update_request(d->bus_client, i);
if (ret) {
dev_err(dev, "bandwidth request failed (%d)\n", ret);
} else {
d->cur_idx = i;
d->cur_ib = new_ib;
d->cur_ab = new_ab;
}
return ret;
}
static int devbw_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 devbw_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 PROP_OPERATING_POINTS_V2 "operating-points-v2"
static int add_opp_prop_from_child(struct device *dev,
struct device_node *of_child)
{
struct property *prop;
int len = 0, ret = 0;
void *value;
const void *p_val;
p_val = of_get_property(of_child, PROP_OPERATING_POINTS_V2, &len);
if (!p_val)
return -ENODEV;
value = devm_kzalloc(dev, len, GFP_KERNEL);
if (!value)
return -ENOMEM;
memcpy(value, p_val, len);
prop = devm_kzalloc(dev, sizeof(*prop), GFP_KERNEL);
if (!prop) {
devm_kfree(dev, value);
return -ENOMEM;
}
prop->name = "operating-points-v2";
prop->value = value;
prop->length = len;
ret = of_add_property(dev->of_node, prop);
if (ret) {
devm_kfree(dev, value);
devm_kfree(dev, prop);
dev_err(dev, "failed to add property: %d\n", ret);
}
return ret;
}
static int parse_child_nodes_for_opp(struct device *dev)
{
struct device_node *of_child;
int ddr_type_of = -1;
int ddr_type = of_fdt_get_ddrtype();
int ret = -EINVAL;
for_each_child_of_node(dev->of_node, of_child) {
ret = of_property_read_u32(of_child, "qcom,ddr-type",
&ddr_type_of);
if (!ret && (ddr_type == ddr_type_of)) {
dev_dbg(dev, "ddr-type = %d, is matching DT entry\n",
ddr_type_of);
ret = add_opp_prop_from_child(dev, of_child);
if (ret)
return ret;
return dev_pm_opp_of_add_table(dev);
}
ret = -ENODEV;
}
return ret;
}
#define PROP_PORTS "qcom,src-dst-ports"
#define PROP_ACTIVE "qcom,active-only"
int devfreq_add_devbw(struct device *dev)
{
struct dev_data *d;
struct devfreq_dev_profile *p;
u32 ports[MAX_PATHS * 2];
const char *gov_name;
int ret, len, i, num_paths;
d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
if (!d)
return -ENOMEM;
dev_set_drvdata(dev, d);
if (of_find_property(dev->of_node, PROP_PORTS, &len)) {
len /= sizeof(ports[0]);
if (len % 2 || len > ARRAY_SIZE(ports)) {
dev_err(dev, "Unexpected number of ports\n");
return -EINVAL;
}
ret = of_property_read_u32_array(dev->of_node, PROP_PORTS,
ports, len);
if (ret)
return ret;
num_paths = len / 2;
} else {
return -EINVAL;
}
d->bw_levels[0].vectors = &d->vectors[0];
d->bw_levels[1].vectors = &d->vectors[MAX_PATHS];
d->bw_data.usecase = d->bw_levels;
d->bw_data.num_usecases = ARRAY_SIZE(d->bw_levels);
d->bw_data.name = dev_name(dev);
d->bw_data.active_only = of_property_read_bool(dev->of_node,
PROP_ACTIVE);
for (i = 0; i < num_paths; i++) {
d->bw_levels[0].vectors[i].src = ports[2 * i];
d->bw_levels[0].vectors[i].dst = ports[2 * i + 1];
d->bw_levels[1].vectors[i].src = ports[2 * i];
d->bw_levels[1].vectors[i].dst = ports[2 * i + 1];
}
d->bw_levels[0].num_paths = num_paths;
d->bw_levels[1].num_paths = num_paths;
d->num_paths = num_paths;
p = &d->dp;
p->polling_ms = 200;
p->target = devbw_target;
p->get_dev_status = devbw_get_dev_status;
if (of_get_child_count(dev->of_node))
ret = parse_child_nodes_for_opp(dev);
else
ret = dev_pm_opp_of_add_table(dev);
if (ret)
dev_err(dev, "Couldn't parse OPP table:%d\n", ret);
d->bus_client = msm_bus_scale_register_client(&d->bw_data);
if (!d->bus_client) {
dev_err(dev, "Unable to register bus client\n");
return -ENODEV;
}
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)) {
msm_bus_scale_unregister_client(d->bus_client);
return PTR_ERR(d->df);
}
if (!strcmp(dev_name(dev), "soc:qcom,cpu-cpu-llcc-bw"))
devfreq_register_boost_device(DEVFREQ_MSM_CPUBW, d->df);
if (!strcmp(dev_name(dev), "soc:qcom,cpu-llcc-ddr-bw"))
devfreq_register_boost_device(DEVFREQ_MSM_LLCCBW, d->df);
return 0;
}
int devfreq_remove_devbw(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
msm_bus_scale_unregister_client(d->bus_client);
devfreq_remove_device(d->df);
return 0;
}
int devfreq_suspend_devbw(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
return devfreq_suspend_device(d->df);
}
int devfreq_resume_devbw(struct device *dev)
{
struct dev_data *d = dev_get_drvdata(dev);
return devfreq_resume_device(d->df);
}
static int devfreq_devbw_probe(struct platform_device *pdev)
{
return devfreq_add_devbw(&pdev->dev);
}
static int devfreq_devbw_remove(struct platform_device *pdev)
{
return devfreq_remove_devbw(&pdev->dev);
}
static const struct of_device_id devbw_match_table[] = {
{ .compatible = "qcom,devbw" },
{}
};
static struct platform_driver devbw_driver = {
.probe = devfreq_devbw_probe,
.remove = devfreq_devbw_remove,
.driver = {
.name = "devbw",
.of_match_table = devbw_match_table,
.suppress_bind_attrs = true,
},
};
module_platform_driver(devbw_driver);
MODULE_DESCRIPTION("Device DDR bandwidth voting driver MSM SoCs");
MODULE_LICENSE("GPL v2");
|