blob: 6a686a76711f7b7f7db1dffa705b767c251cb2fe [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
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>
Mark Grossd82b3512008-02-04 22:30:08 -080017#include <linux/pm_qos_params.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040018#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
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
63 * @dev: the CPU
64 */
65static 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;
Mark Grossed771342010-05-06 01:59:26 +020070 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Len Brown4f86d3a2007-10-03 18:58:00 -040071
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -070072 /* Special case when user has set very strict latency requirement */
73 if (unlikely(latency_req == 0)) {
74 ladder_do_selection(ldev, last_idx, 0);
75 return 0;
76 }
77
Len Brown4f86d3a2007-10-03 18:58:00 -040078 last_state = &ldev->states[last_idx];
79
80 if (dev->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID)
81 last_residency = cpuidle_get_last_residency(dev) - dev->states[last_idx].exit_latency;
82 else
83 last_residency = last_state->threshold.promotion_time + 1;
84
85 /* consider promotion */
86 if (last_idx < dev->state_count - 1 &&
87 last_residency > last_state->threshold.promotion_time &&
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -070088 dev->states[last_idx + 1].exit_latency <= latency_req) {
Len Brown4f86d3a2007-10-03 18:58:00 -040089 last_state->stats.promotion_count++;
90 last_state->stats.demotion_count = 0;
91 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
92 ladder_do_selection(ldev, last_idx, last_idx + 1);
93 return last_idx + 1;
94 }
95 }
96
97 /* consider demotion */
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -070098 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
venkatesh.pallipadi@intel.com06d9e902008-07-30 19:21:44 -070099 dev->states[last_idx].exit_latency > latency_req) {
100 int i;
101
102 for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) {
103 if (dev->states[i].exit_latency <= latency_req)
104 break;
105 }
106 ladder_do_selection(ldev, last_idx, i);
107 return i;
108 }
109
110 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
Len Brown4f86d3a2007-10-03 18:58:00 -0400111 last_residency < last_state->threshold.demotion_time) {
112 last_state->stats.demotion_count++;
113 last_state->stats.promotion_count = 0;
114 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
115 ladder_do_selection(ldev, last_idx, last_idx - 1);
116 return last_idx - 1;
117 }
118 }
119
120 /* otherwise remain at the current state */
121 return last_idx;
122}
123
124/**
125 * ladder_enable_device - setup for the governor
126 * @dev: the CPU
127 */
128static int ladder_enable_device(struct cpuidle_device *dev)
129{
130 int i;
131 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
132 struct ladder_device_state *lstate;
133 struct cpuidle_state *state;
134
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700135 ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -0400136
137 for (i = 0; i < dev->state_count; i++) {
138 state = &dev->states[i];
139 lstate = &ldev->states[i];
140
141 lstate->stats.promotion_count = 0;
142 lstate->stats.demotion_count = 0;
143
144 lstate->threshold.promotion_count = PROMOTION_COUNT;
145 lstate->threshold.demotion_count = DEMOTION_COUNT;
146
147 if (i < dev->state_count - 1)
148 lstate->threshold.promotion_time = state->exit_latency;
149 if (i > 0)
150 lstate->threshold.demotion_time = state->exit_latency;
151 }
152
153 return 0;
154}
155
Deepthi Dharware978aa72011-10-28 16:20:09 +0530156/**
157 * ladder_reflect - update the correct last_state_idx
158 * @dev: the CPU
159 * @index: the index of actual state entered
160 */
161static void ladder_reflect(struct cpuidle_device *dev, int index)
162{
163 struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
164 if (index > 0)
165 ldev->last_state_idx = index;
166}
167
Len Brown4f86d3a2007-10-03 18:58:00 -0400168static struct cpuidle_governor ladder_governor = {
169 .name = "ladder",
170 .rating = 10,
171 .enable = ladder_enable_device,
172 .select = ladder_select_state,
Deepthi Dharware978aa72011-10-28 16:20:09 +0530173 .reflect = ladder_reflect,
Len Brown4f86d3a2007-10-03 18:58:00 -0400174 .owner = THIS_MODULE,
175};
176
177/**
178 * init_ladder - initializes the governor
179 */
180static int __init init_ladder(void)
181{
182 return cpuidle_register_governor(&ladder_governor);
183}
184
185/**
186 * exit_ladder - exits the governor
187 */
188static void __exit exit_ladder(void)
189{
190 cpuidle_unregister_governor(&ladder_governor);
191}
192
193MODULE_LICENSE("GPL");
194module_init(init_ladder);
195module_exit(exit_ladder);