cpuidle: Make ladder governor honor latency requirements fully
[linux-2.6/mini2440.git] / drivers / cpuidle / governors / ladder.c
bloba4bec3f919aa7cb98ddaf8f6f7121dc4d2f4781a
1 /*
2 * ladder.c - the residency ladder algorithm
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>
8 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9 * Shaohua Li <shaohua.li@intel.com>
10 * Adam Belay <abelay@novell.com>
12 * This code is licenced under the GPL.
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>
21 #include <asm/io.h>
22 #include <asm/uaccess.h>
24 #define PROMOTION_COUNT 4
25 #define DEMOTION_COUNT 1
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;
40 struct ladder_device {
41 struct ladder_device_state states[CPUIDLE_STATE_MAX];
42 int last_state_idx;
45 static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
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
53 static inline void ladder_do_selection(struct ladder_device *ldev,
54 int old_idx, int new_idx)
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;
61 /**
62 * ladder_select_state - selects the next state to enter
63 * @dev: the CPU
65 static int ladder_select_state(struct cpuidle_device *dev)
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 int latency_req = pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY);
72 if (unlikely(!ldev))
73 return 0;
75 /* Special case when user has set very strict latency requirement */
76 if (unlikely(latency_req == 0)) {
77 ladder_do_selection(ldev, last_idx, 0);
78 return 0;
81 last_state = &ldev->states[last_idx];
83 if (dev->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID)
84 last_residency = cpuidle_get_last_residency(dev) - dev->states[last_idx].exit_latency;
85 else
86 last_residency = last_state->threshold.promotion_time + 1;
88 /* consider promotion */
89 if (last_idx < dev->state_count - 1 &&
90 last_residency > last_state->threshold.promotion_time &&
91 dev->states[last_idx + 1].exit_latency <= latency_req) {
92 last_state->stats.promotion_count++;
93 last_state->stats.demotion_count = 0;
94 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
95 ladder_do_selection(ldev, last_idx, last_idx + 1);
96 return last_idx + 1;
100 /* consider demotion */
101 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
102 dev->states[last_idx].exit_latency > latency_req) {
103 int i;
105 for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) {
106 if (dev->states[i].exit_latency <= latency_req)
107 break;
109 ladder_do_selection(ldev, last_idx, i);
110 return i;
113 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
114 last_residency < last_state->threshold.demotion_time) {
115 last_state->stats.demotion_count++;
116 last_state->stats.promotion_count = 0;
117 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
118 ladder_do_selection(ldev, last_idx, last_idx - 1);
119 return last_idx - 1;
123 /* otherwise remain at the current state */
124 return last_idx;
128 * ladder_enable_device - setup for the governor
129 * @dev: the CPU
131 static int ladder_enable_device(struct cpuidle_device *dev)
133 int i;
134 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
135 struct ladder_device_state *lstate;
136 struct cpuidle_state *state;
138 ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START;
140 for (i = 0; i < dev->state_count; i++) {
141 state = &dev->states[i];
142 lstate = &ldev->states[i];
144 lstate->stats.promotion_count = 0;
145 lstate->stats.demotion_count = 0;
147 lstate->threshold.promotion_count = PROMOTION_COUNT;
148 lstate->threshold.demotion_count = DEMOTION_COUNT;
150 if (i < dev->state_count - 1)
151 lstate->threshold.promotion_time = state->exit_latency;
152 if (i > 0)
153 lstate->threshold.demotion_time = state->exit_latency;
156 return 0;
159 static struct cpuidle_governor ladder_governor = {
160 .name = "ladder",
161 .rating = 10,
162 .enable = ladder_enable_device,
163 .select = ladder_select_state,
164 .owner = THIS_MODULE,
168 * init_ladder - initializes the governor
170 static int __init init_ladder(void)
172 return cpuidle_register_governor(&ladder_governor);
176 * exit_ladder - exits the governor
178 static void __exit exit_ladder(void)
180 cpuidle_unregister_governor(&ladder_governor);
183 MODULE_LICENSE("GPL");
184 module_init(init_ladder);
185 module_exit(exit_ladder);