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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/* Copyright (c) 2013-2017, 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.
*
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/msm_thermal_ioctl.h>
#include <linux/msm_thermal.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <linux/semaphore.h>
#include <linux/module.h>
struct msm_thermal_ioctl_dev {
struct semaphore sem;
struct cdev char_dev;
};
static int msm_thermal_major;
static struct class *thermal_class;
static struct msm_thermal_ioctl_dev *msm_thermal_dev;
static unsigned int freq_table_len[NR_CPUS], freq_table_set[NR_CPUS];
static unsigned int voltage_table_set[NR_CPUS];
static unsigned int *freq_table_ptr[NR_CPUS];
static uint32_t *voltage_table_ptr[NR_CPUS];
static DEFINE_MUTEX(ioctl_access_mutex);
static int msm_thermal_ioctl_open(struct inode *node, struct file *filep)
{
int ret = 0;
struct msm_thermal_ioctl_dev *dev;
dev = container_of(node->i_cdev, struct msm_thermal_ioctl_dev,
char_dev);
filep->private_data = dev;
return ret;
}
static int msm_thermal_ioctl_release(struct inode *node, struct file *filep)
{
pr_debug("%s: IOCTL: release\n", KBUILD_MODNAME);
return 0;
}
static long validate_and_copy(unsigned int *cmd, unsigned long *arg,
struct msm_thermal_ioctl *query)
{
long ret = 0, err_val = 0;
if ((_IOC_TYPE(*cmd) != MSM_THERMAL_MAGIC_NUM) ||
(_IOC_NR(*cmd) >= MSM_CMD_MAX_NR)) {
ret = -ENOTTY;
goto validate_exit;
}
if (_IOC_DIR(*cmd) & _IOC_READ) {
err_val = !access_ok(VERIFY_WRITE, (void __user *)*arg,
_IOC_SIZE(*cmd));
} else if (_IOC_DIR(*cmd) & _IOC_WRITE) {
err_val = !access_ok(VERIFY_READ, (void __user *)*arg,
_IOC_SIZE(*cmd));
}
if (err_val) {
ret = -EFAULT;
goto validate_exit;
}
if (copy_from_user(query, (void __user *)(*arg),
sizeof(struct msm_thermal_ioctl))) {
ret = -EACCES;
goto validate_exit;
}
if (query->size != sizeof(struct msm_thermal_ioctl)) {
pr_err("%s: Invalid input argument size\n", __func__);
ret = -EINVAL;
goto validate_exit;
}
switch (*cmd) {
case MSM_THERMAL_SET_CPU_MAX_FREQUENCY:
case MSM_THERMAL_SET_CPU_MIN_FREQUENCY:
if (query->cpu_freq.cpu_num >= num_possible_cpus()) {
pr_err("%s: Invalid CPU number: %u\n", __func__,
query->cpu_freq.cpu_num);
ret = -EINVAL;
goto validate_exit;
}
break;
default:
break;
}
validate_exit:
return ret;
}
static long msm_thermal_process_freq_table_req(struct msm_thermal_ioctl *query,
unsigned long *arg)
{
long ret = 0;
uint32_t table_idx, idx = 0, cluster_id = query->clock_freq.cluster_num;
struct clock_plan_arg *clock_freq = &(query->clock_freq);
if (cluster_id >= num_possible_cpus())
return -EINVAL;
if (!freq_table_len[cluster_id]) {
ret = msm_thermal_get_freq_plan_size(cluster_id,
&freq_table_len[cluster_id]);
if (ret) {
pr_err("%s: Cluster%d freq table length get err:%ld\n",
KBUILD_MODNAME, cluster_id, ret);
goto process_freq_exit;
}
if (!freq_table_len[cluster_id]) {
pr_err("%s: Cluster%d freq table empty\n",
KBUILD_MODNAME, cluster_id);
ret = -EAGAIN;
goto process_freq_exit;
}
freq_table_set[cluster_id] = freq_table_len[cluster_id]
/ MSM_IOCTL_FREQ_SIZE;
if (freq_table_len[cluster_id] % MSM_IOCTL_FREQ_SIZE)
freq_table_set[cluster_id]++;
if (!freq_table_ptr[cluster_id]) {
freq_table_ptr[cluster_id] = kzalloc(
sizeof(unsigned int) *
freq_table_len[cluster_id], GFP_KERNEL);
if (!freq_table_ptr[cluster_id]) {
pr_err("%s: memory alloc failed\n",
KBUILD_MODNAME);
freq_table_len[cluster_id] = 0;
ret = -ENOMEM;
goto process_freq_exit;
}
}
ret = msm_thermal_get_cluster_freq_plan(cluster_id,
freq_table_ptr[cluster_id]);
if (ret) {
pr_err("%s: Error getting frequency table. err:%ld\n",
KBUILD_MODNAME, ret);
freq_table_len[cluster_id] = 0;
freq_table_set[cluster_id] = 0;
kfree(freq_table_ptr[cluster_id]);
freq_table_ptr[cluster_id] = NULL;
goto process_freq_exit;
}
}
if (!clock_freq->freq_table_len) {
clock_freq->freq_table_len = freq_table_len[cluster_id];
goto copy_and_return;
}
if (clock_freq->set_idx >= freq_table_set[cluster_id]) {
pr_err("%s: Invalid freq table set%d for cluster%d\n",
KBUILD_MODNAME, clock_freq->set_idx,
cluster_id);
ret = -EINVAL;
goto process_freq_exit;
}
table_idx = MSM_IOCTL_FREQ_SIZE * clock_freq->set_idx;
for (; table_idx < freq_table_len[cluster_id]
&& idx < MSM_IOCTL_FREQ_SIZE; idx++, table_idx++) {
clock_freq->freq_table[idx] =
freq_table_ptr[cluster_id][table_idx];
}
clock_freq->freq_table_len = idx;
copy_and_return:
ret = copy_to_user((void __user *)(*arg), query,
sizeof(struct msm_thermal_ioctl));
if (ret) {
pr_err("%s: copy_to_user error:%ld.\n", KBUILD_MODNAME, ret);
goto process_freq_exit;
}
process_freq_exit:
return ret;
}
static long msm_thermal_process_voltage_table_req(
struct msm_thermal_ioctl *query,
unsigned long *arg)
{
long ret = 0;
uint32_t table_idx = 0, idx = 0;
uint32_t cluster_id = query->voltage.cluster_num;
struct voltage_plan_arg *voltage = &(query->voltage);
if (cluster_id >= num_possible_cpus())
return -EINVAL;
if (!voltage_table_ptr[cluster_id]) {
if (!freq_table_len[cluster_id]) {
ret = msm_thermal_get_freq_plan_size(cluster_id,
&freq_table_len[cluster_id]);
if (ret) {
pr_err(
"%s: Cluster%d freq table len err:%ld\n",
KBUILD_MODNAME, cluster_id, ret);
goto process_volt_exit;
}
if (!freq_table_len[cluster_id]) {
pr_err("%s: Cluster%d freq table empty\n",
KBUILD_MODNAME, cluster_id);
ret = -EAGAIN;
goto process_volt_exit;
}
}
voltage_table_ptr[cluster_id] = kzalloc(
sizeof(uint32_t) *
freq_table_len[cluster_id], GFP_KERNEL);
if (!voltage_table_ptr[cluster_id]) {
pr_err("%s: memory alloc failed\n",
KBUILD_MODNAME);
ret = -ENOMEM;
goto process_volt_exit;
}
ret = msm_thermal_get_cluster_voltage_plan(cluster_id,
voltage_table_ptr[cluster_id]);
if (ret) {
pr_err("%s: Error getting voltage table. err:%ld\n",
KBUILD_MODNAME, ret);
kfree(voltage_table_ptr[cluster_id]);
voltage_table_ptr[cluster_id] = NULL;
goto process_volt_exit;
}
}
if (!voltage->voltage_table_len) {
voltage->voltage_table_len = freq_table_len[cluster_id];
goto copy_and_return;
}
voltage_table_set[cluster_id] = freq_table_len[cluster_id]
/ MSM_IOCTL_FREQ_SIZE;
if (freq_table_len[cluster_id] % MSM_IOCTL_FREQ_SIZE)
voltage_table_set[cluster_id]++;
if (voltage->set_idx >= voltage_table_set[cluster_id]) {
pr_err("%s: Invalid voltage table set%d for cluster%d\n",
KBUILD_MODNAME, voltage->set_idx,
cluster_id);
ret = -EINVAL;
goto process_volt_exit;
}
table_idx = MSM_IOCTL_FREQ_SIZE * voltage->set_idx;
for (; table_idx < freq_table_len[cluster_id]
&& idx < MSM_IOCTL_FREQ_SIZE; idx++, table_idx++) {
voltage->voltage_table[idx] =
voltage_table_ptr[cluster_id][table_idx];
}
voltage->voltage_table_len = idx;
copy_and_return:
ret = copy_to_user((void __user *)(*arg), query,
sizeof(struct msm_thermal_ioctl));
if (ret) {
pr_err("%s: copy_to_user error:%ld.\n", KBUILD_MODNAME, ret);
goto process_volt_exit;
}
process_volt_exit:
return ret;
}
static long msm_thermal_ioctl_process(struct file *filep, unsigned int cmd,
unsigned long arg)
{
long ret = 0;
struct msm_thermal_ioctl query;
pr_debug("%s: IOCTL: processing cmd:%u\n", KBUILD_MODNAME, cmd);
ret = validate_and_copy(&cmd, &arg, &query);
if (ret)
return ret;
mutex_lock(&ioctl_access_mutex);
switch (cmd) {
case MSM_THERMAL_SET_CPU_MAX_FREQUENCY:
ret = msm_thermal_set_frequency(query.cpu_freq.cpu_num,
query.cpu_freq.freq_req, true);
break;
case MSM_THERMAL_SET_CPU_MIN_FREQUENCY:
ret = msm_thermal_set_frequency(query.cpu_freq.cpu_num,
query.cpu_freq.freq_req, false);
break;
case MSM_THERMAL_SET_CLUSTER_MAX_FREQUENCY:
ret = msm_thermal_set_cluster_freq(query.cpu_freq.cpu_num,
query.cpu_freq.freq_req, true);
break;
case MSM_THERMAL_SET_CLUSTER_MIN_FREQUENCY:
ret = msm_thermal_set_cluster_freq(query.cpu_freq.cpu_num,
query.cpu_freq.freq_req, false);
break;
case MSM_THERMAL_GET_CLUSTER_FREQUENCY_PLAN:
ret = msm_thermal_process_freq_table_req(&query, &arg);
break;
case MSM_THERMAL_GET_CLUSTER_VOLTAGE_PLAN:
ret = msm_thermal_process_voltage_table_req(&query, &arg);
break;
default:
ret = -ENOTTY;
goto process_exit;
}
process_exit:
mutex_unlock(&ioctl_access_mutex);
return ret;
}
#ifdef CONFIG_COMPAT
static long msm_thermal_compat_ioctl_process(struct file *filep,
unsigned int cmd, unsigned long arg)
{
arg = (unsigned long)compat_ptr(arg);
return msm_thermal_ioctl_process(filep, cmd, arg);
}
#endif /* CONFIG_COMPAT */
static const struct file_operations msm_thermal_fops = {
.owner = THIS_MODULE,
.open = msm_thermal_ioctl_open,
.unlocked_ioctl = msm_thermal_ioctl_process,
#ifdef CONFIG_COMPAT
.compat_ioctl = msm_thermal_compat_ioctl_process,
#endif /* CONFIG_COMPAT */
.release = msm_thermal_ioctl_release,
};
int msm_thermal_ioctl_init(void)
{
int ret = 0;
dev_t thermal_dev;
struct device *therm_device;
ret = alloc_chrdev_region(&thermal_dev, 0, 1,
MSM_THERMAL_IOCTL_NAME);
if (ret < 0) {
pr_err("%s: Error in allocating char device region. Err:%d\n",
KBUILD_MODNAME, ret);
goto ioctl_init_exit;
}
msm_thermal_major = MAJOR(thermal_dev);
thermal_class = class_create(THIS_MODULE, "msm_thermal");
if (IS_ERR(thermal_class)) {
pr_err("%s: Error in creating class\n",
KBUILD_MODNAME);
ret = PTR_ERR(thermal_class);
goto ioctl_class_fail;
}
therm_device = device_create(thermal_class, NULL, thermal_dev, NULL,
MSM_THERMAL_IOCTL_NAME);
if (IS_ERR(therm_device)) {
pr_err("%s: Error in creating character device\n",
KBUILD_MODNAME);
ret = PTR_ERR(therm_device);
goto ioctl_dev_fail;
}
msm_thermal_dev = kmalloc(sizeof(struct msm_thermal_ioctl_dev),
GFP_KERNEL);
if (!msm_thermal_dev) {
pr_err("%s: Error allocating memory\n",
KBUILD_MODNAME);
ret = -ENOMEM;
goto ioctl_clean_all;
}
memset(msm_thermal_dev, 0, sizeof(struct msm_thermal_ioctl_dev));
sema_init(&msm_thermal_dev->sem, 1);
cdev_init(&msm_thermal_dev->char_dev, &msm_thermal_fops);
ret = cdev_add(&msm_thermal_dev->char_dev, thermal_dev, 1);
if (ret < 0) {
pr_err("%s: Error in adding character device\n",
KBUILD_MODNAME);
goto ioctl_clean_all;
}
return ret;
ioctl_clean_all:
device_destroy(thermal_class, thermal_dev);
ioctl_dev_fail:
class_destroy(thermal_class);
ioctl_class_fail:
unregister_chrdev_region(thermal_dev, 1);
ioctl_init_exit:
return ret;
}
void msm_thermal_ioctl_cleanup(void)
{
uint32_t idx = 0;
dev_t thermal_dev = MKDEV(msm_thermal_major, 0);
if (!msm_thermal_dev) {
pr_err("%s: Thermal IOCTL cleanup already done\n",
KBUILD_MODNAME);
return;
}
for (; idx < num_possible_cpus(); idx++) {
kfree(freq_table_ptr[idx]);
kfree(voltage_table_ptr[idx]);
}
device_destroy(thermal_class, thermal_dev);
class_destroy(thermal_class);
cdev_del(&msm_thermal_dev->char_dev);
unregister_chrdev_region(thermal_dev, 1);
kfree(msm_thermal_dev);
msm_thermal_dev = NULL;
thermal_class = NULL;
}
|