Merge branch 'upstream-davem' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / drivers / cpuidle / governors / ladder.c
1 /*
2  * ladder.c - the residency ladder algorithm
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
7  *
8  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9  *               Shaohua Li <shaohua.li@intel.com>
10  *               Adam Belay <abelay@novell.com>
11  *
12  * This code is licenced under the GPL.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/cpuidle.h>
17 #include <linux/pm_qos_params.h>
18 #include <linux/moduleparam.h>
19 #include <linux/jiffies.h>
20
21 #include <asm/io.h>
22 #include <asm/uaccess.h>
23
24 #define PROMOTION_COUNT 4
25 #define DEMOTION_COUNT 1
26
27 struct ladder_device_state {
28         struct {
29                 u32 promotion_count;
30                 u32 demotion_count;
31                 u32 promotion_time;
32                 u32 demotion_time;
33         } threshold;
34         struct {
35                 int promotion_count;
36                 int demotion_count;
37         } stats;
38 };
39
40 struct ladder_device {
41         struct ladder_device_state states[CPUIDLE_STATE_MAX];
42         int last_state_idx;
43 };
44
45 static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
46
47 /**
48  * ladder_do_selection - prepares private data for a state change
49  * @ldev: the ladder device
50  * @old_idx: the current state index
51  * @new_idx: the new target state index
52  */
53 static inline void ladder_do_selection(struct ladder_device *ldev,
54                                        int old_idx, int new_idx)
55 {
56         ldev->states[old_idx].stats.promotion_count = 0;
57         ldev->states[old_idx].stats.demotion_count = 0;
58         ldev->last_state_idx = new_idx;
59 }
60
61 /**
62  * ladder_select_state - selects the next state to enter
63  * @dev: the CPU
64  */
65 static int ladder_select_state(struct cpuidle_device *dev)
66 {
67         struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
68         struct ladder_device_state *last_state;
69         int last_residency, last_idx = ldev->last_state_idx;
70
71         if (unlikely(!ldev))
72                 return 0;
73
74         last_state = &ldev->states[last_idx];
75
76         if (dev->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID)
77                 last_residency = cpuidle_get_last_residency(dev) - dev->states[last_idx].exit_latency;
78         else
79                 last_residency = last_state->threshold.promotion_time + 1;
80
81         /* consider promotion */
82         if (last_idx < dev->state_count - 1 &&
83             last_residency > last_state->threshold.promotion_time &&
84             dev->states[last_idx + 1].exit_latency <=
85                         pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
86                 last_state->stats.promotion_count++;
87                 last_state->stats.demotion_count = 0;
88                 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
89                         ladder_do_selection(ldev, last_idx, last_idx + 1);
90                         return last_idx + 1;
91                 }
92         }
93
94         /* consider demotion */
95         if (last_idx > 0 &&
96             last_residency < last_state->threshold.demotion_time) {
97                 last_state->stats.demotion_count++;
98                 last_state->stats.promotion_count = 0;
99                 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
100                         ladder_do_selection(ldev, last_idx, last_idx - 1);
101                         return last_idx - 1;
102                 }
103         }
104
105         /* otherwise remain at the current state */
106         return last_idx;
107 }
108
109 /**
110  * ladder_enable_device - setup for the governor
111  * @dev: the CPU
112  */
113 static int ladder_enable_device(struct cpuidle_device *dev)
114 {
115         int i;
116         struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
117         struct ladder_device_state *lstate;
118         struct cpuidle_state *state;
119
120         ldev->last_state_idx = 0;
121
122         for (i = 0; i < dev->state_count; i++) {
123                 state = &dev->states[i];
124                 lstate = &ldev->states[i];
125
126                 lstate->stats.promotion_count = 0;
127                 lstate->stats.demotion_count = 0;
128
129                 lstate->threshold.promotion_count = PROMOTION_COUNT;
130                 lstate->threshold.demotion_count = DEMOTION_COUNT;
131
132                 if (i < dev->state_count - 1)
133                         lstate->threshold.promotion_time = state->exit_latency;
134                 if (i > 0)
135                         lstate->threshold.demotion_time = state->exit_latency;
136         }
137
138         return 0;
139 }
140
141 static struct cpuidle_governor ladder_governor = {
142         .name =         "ladder",
143         .rating =       10,
144         .enable =       ladder_enable_device,
145         .select =       ladder_select_state,
146         .owner =        THIS_MODULE,
147 };
148
149 /**
150  * init_ladder - initializes the governor
151  */
152 static int __init init_ladder(void)
153 {
154         return cpuidle_register_governor(&ladder_governor);
155 }
156
157 /**
158  * exit_ladder - exits the governor
159  */
160 static void __exit exit_ladder(void)
161 {
162         cpuidle_unregister_governor(&ladder_governor);
163 }
164
165 MODULE_LICENSE("GPL");
166 module_init(init_ladder);
167 module_exit(exit_ladder);