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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
*/
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/devfreq.h>
#include <linux/pm_opp.h>
#define INIT_HZ 300000000UL
#define XO_HZ 19200000UL
#define FTBL_MAX_ENTRIES 40U
#define FTBL_ROW_SIZE 4
#define SRC_MASK GENMASK(31, 30)
#define SRC_SHIFT 30
#define MULT_MASK GENMASK(7, 0)
struct devfreq_qcom_fw {
void __iomem *perf_base;
void __iomem *pstate_base;
struct devfreq_dev_profile dp;
struct list_head voters;
struct list_head voter;
unsigned int index;
};
static DEFINE_SPINLOCK(voter_lock);
static unsigned int ftbl_row_size = FTBL_ROW_SIZE;
static struct device *dev_node;
static int devfreq_qcom_fw_target(struct device *dev, unsigned long *freq,
u32 flags)
{
struct devfreq_qcom_fw *d = dev_get_drvdata(dev), *pd, *v;
struct devfreq_dev_profile *p = &d->dp;
unsigned int index;
unsigned long lflags;
struct dev_pm_opp *opp;
void __iomem *perf_base = d->perf_base;
opp = devfreq_recommended_opp(dev, freq, flags);
if (!IS_ERR(opp))
dev_pm_opp_put(opp);
else
return PTR_ERR(opp);
for (index = 0; index < p->max_state; index++)
if (p->freq_table[index] == *freq)
break;
if (index >= p->max_state) {
dev_err(dev, "Unable to find index for freq (%lu)!\n", *freq);
return -EINVAL;
}
d->index = index;
spin_lock_irqsave(&voter_lock, lflags);
/* Voter */
if (!perf_base) {
pd = dev_get_drvdata(dev->parent);
list_for_each_entry(v, &pd->voters, voter)
index = max(index, v->index);
perf_base = pd->perf_base;
}
writel_relaxed(index, perf_base);
spin_unlock_irqrestore(&voter_lock, lflags);
return 0;
}
static int devfreq_panic_callback(struct notifier_block *nfb,
unsigned long event, void *unused)
{
struct devfreq_qcom_fw *d = dev_get_drvdata(dev_node);
void __iomem *base;
pr_err("%s: L3-DOMAIN\n", __func__);
base = d->perf_base;
pr_err("%25s: 0x%.8x\n", "PERF_STATE_DESIRED", readl_relaxed(base));
base = d->pstate_base;
pr_err("%25s: 0x%.8x\n", "PSTATE_STATUS", readl_relaxed(base));
return NOTIFY_OK;
}
static struct notifier_block devfreq_panic_notifier = {
.notifier_call = devfreq_panic_callback,
.priority = 1,
};
static int devfreq_qcom_fw_get_cur_freq(struct device *dev,
unsigned long *freq)
{
struct devfreq_qcom_fw *d = dev_get_drvdata(dev);
struct devfreq_dev_profile *p = &d->dp;
unsigned int index;
/* Voter */
if (!d->perf_base) {
index = d->index;
} else {
index = readl_relaxed(d->perf_base);
index = min(index, p->max_state - 1);
}
*freq = p->freq_table[index];
return 0;
}
static int devfreq_qcom_populate_opp(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
u32 data, src, mult, i;
unsigned long freq, prev_freq = 0;
struct resource *res;
void __iomem *ftbl_base;
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ftbl-base");
if (!res) {
dev_err(dev, "Unable to find ftbl-base!\n");
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(pdev->dev.of_node, "qcom,ftbl-row-size",
&ftbl_row_size);
for (i = 0; i < FTBL_MAX_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 with the same core counts means
* end of table.
*/
if (i > 0 && prev_freq == freq)
break;
dev_pm_opp_add(&pdev->dev, freq, 0);
prev_freq = freq;
}
devm_iounmap(dev, ftbl_base);
return 0;
}
static int devfreq_qcom_init_hw(struct platform_device *pdev)
{
struct devfreq_qcom_fw *d;
struct resource *res;
struct device *dev = &pdev->dev;
int ret = 0;
void __iomem *en_base;
d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
if (!d)
return -ENOMEM;
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "en-base");
if (!res) {
dev_err(dev, "Unable to find en-base!\n");
return -EINVAL;
}
en_base = devm_ioremap(dev, res->start, resource_size(res));
if (!en_base) {
dev_err(dev, "Unable to map en-base\n");
return -ENOMEM;
}
/* Firmware should be enabled state to proceed */
if (!(readl_relaxed(en_base) & 1)) {
dev_err(dev, "Firmware not enabled\n");
return -ENODEV;
}
devm_iounmap(dev, en_base);
ret = devfreq_qcom_populate_opp(pdev);
if (ret) {
dev_err(dev, "Failed to read FTBL\n");
return ret;
}
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "perf-base");
if (!res) {
dev_err(dev, "Unable to find perf-base!\n");
ret = -EINVAL;
goto out;
}
d->perf_base = devm_ioremap(dev, res->start, resource_size(res));
if (!d->perf_base) {
dev_err(dev, "Unable to map perf-base\n");
ret = -ENOMEM;
goto out;
}
if (of_property_read_bool(dev->of_node,
"qcom,support-panic-notifier")) {
res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
"pstate-base");
if (!res) {
dev_err(dev, "Unable to find pstate-base!\n");
ret = -EINVAL;
goto out;
}
d->pstate_base = devm_ioremap(dev, res->start,
resource_size(res));
if (!d->pstate_base) {
dev_err(dev, "Unable to map pstate-base\n");
ret = -ENOMEM;
goto out;
}
atomic_notifier_chain_register(&panic_notifier_list,
&devfreq_panic_notifier);
dev_node = dev;
}
INIT_LIST_HEAD(&d->voters);
dev_set_drvdata(dev, d);
out:
if (ret)
dev_pm_opp_remove_table(dev);
return ret;
}
static int devfreq_qcom_copy_opp(struct device *src_dev, struct device *dst_dev)
{
unsigned long freq;
int i, cnt, ret = 0;
struct dev_pm_opp *opp;
if (!src_dev)
return -ENODEV;
cnt = dev_pm_opp_get_opp_count(src_dev);
if (!cnt)
return -EINVAL;
for (i = 0, freq = 0; i < cnt; i++, freq++) {
opp = dev_pm_opp_find_freq_ceil(src_dev, &freq);
if (IS_ERR(opp)) {
ret = -EINVAL;
break;
}
dev_pm_opp_put(opp);
ret = dev_pm_opp_add(dst_dev, freq, 0);
if (ret)
break;
}
if (ret)
dev_pm_opp_remove_table(dst_dev);
return ret;
}
static int devfreq_qcom_init_voter(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device *par_dev = dev->parent;
struct devfreq_qcom_fw *d, *pd = dev_get_drvdata(par_dev);
int ret = 0;
d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
if (!d)
return -ENOMEM;
ret = devfreq_qcom_copy_opp(dev->parent, dev);
if (ret) {
dev_err(dev, "Failed to copy parent OPPs\n");
return ret;
}
list_add(&d->voter, &pd->voters);
dev_set_drvdata(dev, d);
return 0;
}
static int devfreq_qcom_fw_driver_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
int ret = 0;
struct devfreq_qcom_fw *d;
struct devfreq_dev_profile *p;
struct devfreq *df;
if (!of_device_get_match_data(dev))
ret = devfreq_qcom_init_voter(pdev);
else
ret = devfreq_qcom_init_hw(pdev);
if (ret) {
dev_err(dev, "Unable to probe device!\n");
return ret;
}
/*
* If device has voter children, do no register directly with devfreq
*/
if (of_get_available_child_count(dev->of_node)) {
of_platform_populate(dev->of_node, NULL, NULL, dev);
dev_info(dev, "Devfreq QCOM Firmware parent dev inited.\n");
return 0;
}
d = dev_get_drvdata(dev);
p = &d->dp;
p->polling_ms = 50;
p->target = devfreq_qcom_fw_target;
p->get_cur_freq = devfreq_qcom_fw_get_cur_freq;
df = devm_devfreq_add_device(dev, p, "performance", NULL);
if (IS_ERR(df)) {
dev_err(dev, "Unable to register Devfreq QCOM Firmware dev!\n");
return PTR_ERR(df);
}
dev_info(dev, "Devfreq QCOM Firmware dev registered.\n");
return 0;
}
static const struct of_device_id match_table[] = {
{ .compatible = "qcom,devfreq-fw", .data = (void *) 1 },
{ .compatible = "qcom,devfreq-fw-voter", .data = (void *) 0 },
{}
};
static struct platform_driver devfreq_qcom_fw_driver = {
.probe = devfreq_qcom_fw_driver_probe,
.driver = {
.name = "devfreq-qcom-fw",
.of_match_table = match_table,
},
};
static int __init devfreq_qcom_fw_init(void)
{
return platform_driver_register(&devfreq_qcom_fw_driver);
}
subsys_initcall(devfreq_qcom_fw_init);
static void __exit devfreq_qcom_fw_exit(void)
{
platform_driver_unregister(&devfreq_qcom_fw_driver);
}
module_exit(devfreq_qcom_fw_exit);
MODULE_DESCRIPTION("Devfreq QCOM Firmware");
MODULE_LICENSE("GPL v2");
|