Linux-libre 4.15.7-gnu
[librecmc/linux-libre.git] / 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.h>
18 #include <linux/jiffies.h>
19 #include <linux/tick.h>
20 #include <linux/cpu.h>
21
22 #include <asm/io.h>
23 #include <linux/uaccess.h>
24
25 #define PROMOTION_COUNT 4
26 #define DEMOTION_COUNT 1
27
28 struct ladder_device_state {
29         struct {
30                 u32 promotion_count;
31                 u32 demotion_count;
32                 u32 promotion_time;
33                 u32 demotion_time;
34         } threshold;
35         struct {
36                 int promotion_count;
37                 int demotion_count;
38         } stats;
39 };
40
41 struct ladder_device {
42         struct ladder_device_state states[CPUIDLE_STATE_MAX];
43         int last_state_idx;
44 };
45
46 static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
47
48 /**
49  * ladder_do_selection - prepares private data for a state change
50  * @ldev: the ladder device
51  * @old_idx: the current state index
52  * @new_idx: the new target state index
53  */
54 static inline void ladder_do_selection(struct ladder_device *ldev,
55                                        int old_idx, int new_idx)
56 {
57         ldev->states[old_idx].stats.promotion_count = 0;
58         ldev->states[old_idx].stats.demotion_count = 0;
59         ldev->last_state_idx = new_idx;
60 }
61
62 /**
63  * ladder_select_state - selects the next state to enter
64  * @drv: cpuidle driver
65  * @dev: the CPU
66  */
67 static int ladder_select_state(struct cpuidle_driver *drv,
68                                 struct cpuidle_device *dev)
69 {
70         struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
71         struct device *device = get_cpu_device(dev->cpu);
72         struct ladder_device_state *last_state;
73         int last_residency, last_idx = ldev->last_state_idx;
74         int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
75         int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
76         int resume_latency = dev_pm_qos_raw_read_value(device);
77
78         if (resume_latency < latency_req &&
79             resume_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
80                 latency_req = resume_latency;
81
82         /* Special case when user has set very strict latency requirement */
83         if (unlikely(latency_req == 0)) {
84                 ladder_do_selection(ldev, last_idx, 0);
85                 return 0;
86         }
87
88         last_state = &ldev->states[last_idx];
89
90         last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency;
91
92         /* consider promotion */
93         if (last_idx < drv->state_count - 1 &&
94             !drv->states[last_idx + 1].disabled &&
95             !dev->states_usage[last_idx + 1].disable &&
96             last_residency > last_state->threshold.promotion_time &&
97             drv->states[last_idx + 1].exit_latency <= latency_req) {
98                 last_state->stats.promotion_count++;
99                 last_state->stats.demotion_count = 0;
100                 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
101                         ladder_do_selection(ldev, last_idx, last_idx + 1);
102                         return last_idx + 1;
103                 }
104         }
105
106         /* consider demotion */
107         if (last_idx > first_idx &&
108             (drv->states[last_idx].disabled ||
109             dev->states_usage[last_idx].disable ||
110             drv->states[last_idx].exit_latency > latency_req)) {
111                 int i;
112
113                 for (i = last_idx - 1; i > first_idx; i--) {
114                         if (drv->states[i].exit_latency <= latency_req)
115                                 break;
116                 }
117                 ladder_do_selection(ldev, last_idx, i);
118                 return i;
119         }
120
121         if (last_idx > first_idx &&
122             last_residency < last_state->threshold.demotion_time) {
123                 last_state->stats.demotion_count++;
124                 last_state->stats.promotion_count = 0;
125                 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
126                         ladder_do_selection(ldev, last_idx, last_idx - 1);
127                         return last_idx - 1;
128                 }
129         }
130
131         /* otherwise remain at the current state */
132         return last_idx;
133 }
134
135 /**
136  * ladder_enable_device - setup for the governor
137  * @drv: cpuidle driver
138  * @dev: the CPU
139  */
140 static int ladder_enable_device(struct cpuidle_driver *drv,
141                                 struct cpuidle_device *dev)
142 {
143         int i;
144         int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
145         struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
146         struct ladder_device_state *lstate;
147         struct cpuidle_state *state;
148
149         ldev->last_state_idx = first_idx;
150
151         for (i = first_idx; i < drv->state_count; i++) {
152                 state = &drv->states[i];
153                 lstate = &ldev->states[i];
154
155                 lstate->stats.promotion_count = 0;
156                 lstate->stats.demotion_count = 0;
157
158                 lstate->threshold.promotion_count = PROMOTION_COUNT;
159                 lstate->threshold.demotion_count = DEMOTION_COUNT;
160
161                 if (i < drv->state_count - 1)
162                         lstate->threshold.promotion_time = state->exit_latency;
163                 if (i > first_idx)
164                         lstate->threshold.demotion_time = state->exit_latency;
165         }
166
167         return 0;
168 }
169
170 /**
171  * ladder_reflect - update the correct last_state_idx
172  * @dev: the CPU
173  * @index: the index of actual state entered
174  */
175 static void ladder_reflect(struct cpuidle_device *dev, int index)
176 {
177         struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
178         if (index > 0)
179                 ldev->last_state_idx = index;
180 }
181
182 static struct cpuidle_governor ladder_governor = {
183         .name =         "ladder",
184         .rating =       10,
185         .enable =       ladder_enable_device,
186         .select =       ladder_select_state,
187         .reflect =      ladder_reflect,
188 };
189
190 /**
191  * init_ladder - initializes the governor
192  */
193 static int __init init_ladder(void)
194 {
195         /*
196          * When NO_HZ is disabled, or when booting with nohz=off, the ladder
197          * governor is better so give it a higher rating than the menu
198          * governor.
199          */
200         if (!tick_nohz_enabled)
201                 ladder_governor.rating = 25;
202
203         return cpuidle_register_governor(&ladder_governor);
204 }
205
206 postcore_initcall(init_ladder);