aboutsummaryrefslogtreecommitdiff
path: root/drivers/cpuidle/mtk_cpuidle_internal.c
blob: 90a70233ea5f3a685fd031b9d7c713866efed4f0 (plain) (blame)
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
#include <asm/uaccess.h>
#include <linux/cpu.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>

#include <mach/mt_clkmgr.h>
#include "mtk_cpuidle_internal.h"

/*Idle handler on/off*/
static int idle_switch[NR_TYPES] = {
    1,  //dpidle switch
    1,  //soidle switch
    1,  //slidle switch
    1,  //rgidle switch
};

static const char *idle_name[NR_TYPES] = {
    "dpidle",
    "soidle",
    "slidle",
    "rgidle",
};

const char *reason_name[NR_REASONS] = {
    "by_cpu",
    "by_clk",
    "by_tmr",
    "by_oth",
    "by_vtg",
};

#define INVALID_GRP_ID(grp) (grp < 0 || grp >= NR_GRPS)

unsigned int dpidle_condition_mask[NR_GRPS] = {
    0x0000008A, //INFRA:
//    0x37FA1FFD, //PERI0:
//  TODO: check uart1 CG on D-1 EVB ??
    0x37F21FFD, //PERI0:
    0x000FFFFF, //DISP0:
    0x0000003F, //DISP1:
    0x00000FE1, //IMAGE:
    0x00000001, //MFG:
    0x00000000, //AUDIO:
    0x00000001, //VDEC0:
    0x00000001, //VDEC1:
    0x00001111, //VENC:
};

static unsigned long    dpidle_cnt[NR_CPUS] = {0};
static unsigned long    dpidle_block_cnt[NR_REASONS] = {0};
unsigned int            dpidle_block_mask[NR_GRPS] = {0x0};
unsigned int            dpidle_time_critera = 26000;
unsigned int            dpidle_block_time_critera = 30000;//default 30sec
bool                    dpidle_by_pass_cg = false;

static unsigned long    soidle_cnt[NR_CPUS] = {0};

static unsigned long    slidle_cnt[NR_CPUS] = {0};

static unsigned long    rgidle_cnt[NR_CPUS] = {0};

void idle_cnt_inc(int idle_type, int cpu)
{
    switch (idle_type) {
    case IDLE_TYPE_DP:
        dpidle_cnt[cpu]++;
        break;
    case IDLE_TYPE_SO:
        soidle_cnt[cpu]++;
        break;
    case IDLE_TYPE_SL:
        slidle_cnt[cpu]++;
        break;
    case IDLE_TYPE_RG:
        rgidle_cnt[cpu]++;
        break;
    default:
        break;
    }
}
EXPORT_SYMBOL(idle_cnt_inc);

unsigned long idle_cnt_get(int idle_type, int cpu)
{
    unsigned long   ret = 0;

    switch (idle_type) {
    case IDLE_TYPE_DP:
        ret = dpidle_cnt[cpu];
        break;
    case IDLE_TYPE_SO:
        ret = soidle_cnt[cpu];
        break;
    case IDLE_TYPE_SL:
        ret = slidle_cnt[cpu];
        break;
    case IDLE_TYPE_RG:
        ret = rgidle_cnt[cpu];
        break;
    default:
        break;
    }

    return ret;
}
EXPORT_SYMBOL(idle_cnt_get);

void idle_block_cnt_inc(int idle_type, int reason)
{
    switch (idle_type) {
    case IDLE_TYPE_DP:
        dpidle_block_cnt[reason]++;
        break;
    case IDLE_TYPE_SO:
        /* TODO */
        break;
    case IDLE_TYPE_SL:
        /* TODO */
        break;
    case IDLE_TYPE_RG:
        /* TODO */
        break;
    default:
        break;
    }
}
EXPORT_SYMBOL(idle_block_cnt_inc);

unsigned long idle_block_cnt_get(int idle_type, int reason)
{
    unsigned long   ret = 0;

    switch (idle_type) {
    case IDLE_TYPE_DP:
        ret = dpidle_block_cnt[reason];
        break;
    case IDLE_TYPE_SO:
        /* TODO */
        break;
    case IDLE_TYPE_SL:
        /* TODO */
        break;
    case IDLE_TYPE_RG:
        /* TODO */
        break;
    default:
        break;
    }

    return ret;
}
EXPORT_SYMBOL(idle_block_cnt_get);

void idle_block_cnt_clr(int idle_type)
{
    unsigned long   ret = 0;

    switch (idle_type) {
    case IDLE_TYPE_DP:
        memset(dpidle_block_cnt, 0, sizeof(dpidle_block_cnt));
        break;
    case IDLE_TYPE_SO:
        /* TODO */
        break;
    case IDLE_TYPE_SL:
        /* TODO */
        break;
    case IDLE_TYPE_RG:
        /* TODO */
        break;
    default:
        break;
    }
}
EXPORT_SYMBOL(idle_block_cnt_clr);

int idle_switch_get(int idle_type)
{
    return (idle_type >= 0 && idle_type < NR_TYPES) ?
                idle_switch[idle_type] :
                0;
}
EXPORT_SYMBOL(idle_switch_get);

static DEFINE_MUTEX(dpidle_locked);

static void enable_dpidle_by_mask(int grp, unsigned int mask)
{
    mutex_lock(&dpidle_locked);
    dpidle_condition_mask[grp] &= ~mask;
    mutex_unlock(&dpidle_locked);
}

static void disable_dpidle_by_mask(int grp, unsigned int mask)
{
    mutex_lock(&dpidle_locked);
    dpidle_condition_mask[grp] |= mask;
    mutex_unlock(&dpidle_locked);
}

void enable_dpidle_by_bit(int id)
{
    int grp = id / 32;
    unsigned int mask = 1U << (id % 32);
    BUG_ON(INVALID_GRP_ID(grp));
    enable_dpidle_by_mask(grp, mask);
}
EXPORT_SYMBOL(enable_dpidle_by_bit);

void disable_dpidle_by_bit(int id)
{
    int grp = id / 32;
    unsigned int mask = 1U << (id % 32);
    BUG_ON(INVALID_GRP_ID(grp));
    disable_dpidle_by_mask(grp, mask);
}
EXPORT_SYMBOL(disable_dpidle_by_bit);

/***************************/
/* debugfs                 */
/***************************/
static char dbg_buf[2048] = {0};
static char cmd_buf[512] = {0};

/* idle_state */
static int _idle_state_open(struct seq_file *s, void *data)
{
    return 0;
}

static int idle_state_open(struct inode *inode, struct file *filp)
{
    return single_open(filp, _idle_state_open, inode->i_private);
}

static ssize_t idle_state_read(struct file *filp, 
                                 char __user *userbuf, 
                                 size_t count, 
                                 loff_t *f_pos)
{
    int len = 0;
    char *p = dbg_buf;
    int i;

    p += sprintf(p, "********** idle state dump **********\n");

    for (i = 0; i < nr_cpu_ids; i++) {
        p += sprintf(p, "soidle_cnt[%d]=%lu, dpidle_cnt[%d]=%lu, "
                "slidle_cnt[%d]=%lu, rgidle_cnt[%d]=%lu\n",
                i, soidle_cnt[i], i, dpidle_cnt[i],
                i, slidle_cnt[i], i, rgidle_cnt[i]);
    }

    p += sprintf(p, "\n********** variables dump **********\n");
    for (i = 0; i < NR_TYPES; i++) {
        p += sprintf(p, "%s_switch=%d, ", idle_name[i], idle_switch[i]);
    }
    p += sprintf(p, "\n");

    p += sprintf(p, "\n********** idle command help **********\n");
    p += sprintf(p, "status help:   cat /sys/power/idle_state\n");
    p += sprintf(p, "switch on/off: echo switch mask > /sys/power/idle_state\n");

    p += sprintf(p, "soidle help:   cat /sys/power/soidle_state\n");
    p += sprintf(p, "dpidle help:   cat /sys/power/dpidle_state\n");
    p += sprintf(p, "slidle help:   cat /sys/power/slidle_state\n");
    p += sprintf(p, "rgidle help:   cat /sys/power/rgidle_state\n");

    len = p - dbg_buf;

    return simple_read_from_buffer(userbuf, count, f_pos, dbg_buf, len);
}

static ssize_t idle_state_write(struct file *filp, 
                                  const char __user *userbuf, 
                                  size_t count, 
                                  loff_t *f_pos)
{
    char cmd[32];
    int idx;
    int param;

    count = min(count, sizeof(cmd_buf) - 1);

    if (copy_from_user(cmd_buf, userbuf, count)) {
        return -EFAULT;
    }
    cmd_buf[count] = '\0';

    if (sscanf(cmd_buf, "%s %x", cmd, &param) == 2) {
        if (!strcmp(cmd, "switch")) {
            for (idx = 0; idx < NR_TYPES; idx++) {
                idle_switch[idx] = (param & (1U << idx)) ? 1 : 0;
            }
        }
        return count;
    }

    return -EINVAL;
}

static const struct file_operations idle_state_fops = {
    .open = idle_state_open,
    .read = idle_state_read,
    .write = idle_state_write,
    .llseek = seq_lseek,
    .release = single_release,
};
/* dpidle_state */
static int _dpidle_state_open(struct seq_file *s, void *data)
{
    return 0;
}

static int dpidle_state_open(struct inode *inode, struct file *filp)
{
    return single_open(filp, _dpidle_state_open, inode->i_private);
}

static ssize_t dpidle_state_read(struct file *filp, char __user *userbuf, size_t count, loff_t *f_pos)
{
    int len = 0;
    char *p = dbg_buf;
    int i;
    ssize_t retval = 0;

    p += sprintf(p, "*********** deep idle state ************\n");
    p += sprintf(p, "dpidle_time_critera=%u\n", dpidle_time_critera);

    for (i = 0; i < NR_REASONS; i++) {
        p += sprintf(p, "[%d]dpidle_block_cnt[%s]=%lu\n", i, reason_name[i],
                dpidle_block_cnt[i]);
    }

    p += sprintf(p, "\n");

    for (i = 0; i < NR_GRPS; i++) {
        p += sprintf(p, "[%02d]dpidle_condition_mask[%-8s]=0x%08x\t\t"
                "dpidle_block_mask[%-8s]=0x%08x\n", i,
                grp_get_name(i), dpidle_condition_mask[i],
                grp_get_name(i), dpidle_block_mask[i]);
    }

    p += sprintf(p, "dpidle_bypass_cg=%u\n", dpidle_by_pass_cg);

    p += sprintf(p, "\n*********** dpidle command help  ************\n");
    p += sprintf(p, "dpidle help:   cat /sys/power/dpidle_state\n");
    p += sprintf(p, "switch on/off: echo [dpidle] 1/0 > /sys/power/dpidle_state\n");
    p += sprintf(p, "cpupdn on/off: echo cpupdn 1/0 > /sys/power/dpidle_state\n");
    p += sprintf(p, "en_dp_by_bit:  echo enable id > /sys/power/dpidle_state\n");
    p += sprintf(p, "dis_dp_by_bit: echo disable id > /sys/power/dpidle_state\n");
    p += sprintf(p, "modify tm_cri: echo time value(dec) > /sys/power/dpidle_state\n");
    p += sprintf(p, "bypass cg:     echo bypass 1/0 > /sys/power/dpidle_state\n");

    len = p - dbg_buf;

    return simple_read_from_buffer(userbuf, count, f_pos, dbg_buf, len);
}

static ssize_t dpidle_state_write(struct file *filp, 
                                  const char __user *userbuf, 
                                  size_t count, 
                                  loff_t *f_pos)
{
    char cmd[32];
    int param;

    count = min(count, sizeof(cmd_buf) - 1);

    if (copy_from_user(cmd_buf, userbuf, count)) {
        return -EFAULT;
    }
    cmd_buf[count] = '\0';

    if (sscanf(cmd_buf, "%s %d", cmd, &param) == 2) {
        if (!strcmp(cmd, "dpidle")) {
            idle_switch[IDLE_TYPE_DP] = param;
        } else if (!strcmp(cmd, "enable")) {
            enable_dpidle_by_bit(param);
        } else if (!strcmp(cmd, "disable")) {
            disable_dpidle_by_bit(param);
        } else if (!strcmp(cmd, "time")) {
            dpidle_time_critera = param;
        }else if (!strcmp(cmd, "bypass")) {
            dpidle_by_pass_cg = param;
            printk(KERN_WARNING"bypass = %d\n", dpidle_by_pass_cg);
        }
        return count;
    } else if (sscanf(cmd_buf, "%d", &param) == 1) {
        idle_switch[IDLE_TYPE_DP] = param;
        return count;
    }

    return -EINVAL;
}

static const struct file_operations dpidle_state_fops = {
    .open = dpidle_state_open,
    .read = dpidle_state_read,
    .write = dpidle_state_write,
    .llseek = seq_lseek,
    .release = single_release,
};

static struct dentry *root_entry;

int mtk_cpuidle_debugfs_init(void)
{
    /* TODO: check if debugfs_create_file() failed */
    /* Initialize debugfs */
    root_entry = debugfs_create_dir("cpuidle", NULL);
    if (!root_entry) {
        printk(KERN_WARNING"Can not create debugfs `dpidle_state`\n");
        return 1;
    }

    debugfs_create_file("idle_state", 0644, root_entry, NULL, &idle_state_fops);
    debugfs_create_file("dpidle_state", 0644, root_entry, NULL, &dpidle_state_fops);

    return 0;
}