Honor state disabling in the cpuidle ladder governor
[deliverable/linux.git] / drivers / cpuidle / governors / ladder.c
CommitLineData
4f86d3a8
LB
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>
e8db0be1 17#include <linux/pm_qos.h>
70e522a0 18#include <linux/module.h>
4f86d3a8
LB
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
27struct 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
40struct ladder_device {
41 struct ladder_device_state states[CPUIDLE_STATE_MAX];
42 int last_state_idx;
43};
44
45static 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 */
53static 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
46bcfad7 63 * @drv: cpuidle driver
4f86d3a8
LB
64 * @dev: the CPU
65 */
46bcfad7
DD
66static int ladder_select_state(struct cpuidle_driver *drv,
67 struct cpuidle_device *dev)
4f86d3a8
LB
68{
69 struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
70 struct ladder_device_state *last_state;
71 int last_residency, last_idx = ldev->last_state_idx;
ed77134b 72 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
4f86d3a8 73
a2bd9202 74 /* Special case when user has set very strict latency requirement */
75 if (unlikely(latency_req == 0)) {
76 ladder_do_selection(ldev, last_idx, 0);
77 return 0;
78 }
79
4f86d3a8
LB
80 last_state = &ldev->states[last_idx];
81
46bcfad7
DD
82 if (drv->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID) {
83 last_residency = cpuidle_get_last_residency(dev) - \
84 drv->states[last_idx].exit_latency;
85 }
4f86d3a8
LB
86 else
87 last_residency = last_state->threshold.promotion_time + 1;
88
89 /* consider promotion */
46bcfad7 90 if (last_idx < drv->state_count - 1 &&
62d6ae88 91 !dev->states_usage[last_idx + 1].disable &&
4f86d3a8 92 last_residency > last_state->threshold.promotion_time &&
46bcfad7 93 drv->states[last_idx + 1].exit_latency <= latency_req) {
4f86d3a8
LB
94 last_state->stats.promotion_count++;
95 last_state->stats.demotion_count = 0;
96 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
97 ladder_do_selection(ldev, last_idx, last_idx + 1);
98 return last_idx + 1;
99 }
100 }
101
102 /* consider demotion */
06d9e908 103 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
62d6ae88
CE
104 (dev->states_usage[last_idx].disable ||
105 drv->states[last_idx].exit_latency > latency_req)) {
06d9e908 106 int i;
107
108 for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) {
46bcfad7 109 if (drv->states[i].exit_latency <= latency_req)
06d9e908 110 break;
111 }
112 ladder_do_selection(ldev, last_idx, i);
113 return i;
114 }
115
a2bd9202 116 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
4f86d3a8
LB
117 last_residency < last_state->threshold.demotion_time) {
118 last_state->stats.demotion_count++;
119 last_state->stats.promotion_count = 0;
120 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
121 ladder_do_selection(ldev, last_idx, last_idx - 1);
122 return last_idx - 1;
123 }
124 }
125
126 /* otherwise remain at the current state */
127 return last_idx;
128}
129
130/**
131 * ladder_enable_device - setup for the governor
46bcfad7 132 * @drv: cpuidle driver
4f86d3a8
LB
133 * @dev: the CPU
134 */
46bcfad7
DD
135static int ladder_enable_device(struct cpuidle_driver *drv,
136 struct cpuidle_device *dev)
4f86d3a8
LB
137{
138 int i;
139 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
140 struct ladder_device_state *lstate;
141 struct cpuidle_state *state;
142
a2bd9202 143 ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START;
4f86d3a8 144
46bcfad7
DD
145 for (i = 0; i < drv->state_count; i++) {
146 state = &drv->states[i];
4f86d3a8
LB
147 lstate = &ldev->states[i];
148
149 lstate->stats.promotion_count = 0;
150 lstate->stats.demotion_count = 0;
151
152 lstate->threshold.promotion_count = PROMOTION_COUNT;
153 lstate->threshold.demotion_count = DEMOTION_COUNT;
154
46bcfad7 155 if (i < drv->state_count - 1)
4f86d3a8
LB
156 lstate->threshold.promotion_time = state->exit_latency;
157 if (i > 0)
158 lstate->threshold.demotion_time = state->exit_latency;
159 }
160
161 return 0;
162}
163
e978aa7d
DD
164/**
165 * ladder_reflect - update the correct last_state_idx
166 * @dev: the CPU
167 * @index: the index of actual state entered
168 */
169static void ladder_reflect(struct cpuidle_device *dev, int index)
170{
171 struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
172 if (index > 0)
173 ldev->last_state_idx = index;
174}
175
4f86d3a8
LB
176static struct cpuidle_governor ladder_governor = {
177 .name = "ladder",
178 .rating = 10,
179 .enable = ladder_enable_device,
180 .select = ladder_select_state,
e978aa7d 181 .reflect = ladder_reflect,
4f86d3a8
LB
182 .owner = THIS_MODULE,
183};
184
185/**
186 * init_ladder - initializes the governor
187 */
188static int __init init_ladder(void)
189{
190 return cpuidle_register_governor(&ladder_governor);
191}
192
193/**
194 * exit_ladder - exits the governor
195 */
196static void __exit exit_ladder(void)
197{
198 cpuidle_unregister_governor(&ladder_governor);
199}
200
201MODULE_LICENSE("GPL");
202module_init(init_ladder);
203module_exit(exit_ladder);
This page took 0.422648 seconds and 5 git commands to generate.