blob: b6a09ea859b135c31e6dcffbad0e7a3d2ad66eee [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>
Jean Pihete8db0be2011-08-25 15:35:03 +020017#include <linux/pm_qos.h>
Paul Gortmaker70e522a2011-08-29 17:52:39 -040018#include <linux/module.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040019#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
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053063 * @drv: cpuidle driver
Len Brown4f86d3a2007-10-03 18:58:00 -040064 * @dev: the CPU
65 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053066static int ladder_select_state(struct cpuidle_driver *drv,
67 struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -040068{
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;
Mark Grossed771342010-05-06 01:59:26 +020072 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Len Brown4f86d3a2007-10-03 18:58:00 -040073
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -070074 /* 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
Len Brown4f86d3a2007-10-03 18:58:00 -040080 last_state = &ldev->states[last_idx];
81
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053082 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 }
Len Brown4f86d3a2007-10-03 18:58:00 -040086 else
87 last_residency = last_state->threshold.promotion_time + 1;
88
89 /* consider promotion */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053090 if (last_idx < drv->state_count - 1 &&
Len Brown4f86d3a2007-10-03 18:58:00 -040091 last_residency > last_state->threshold.promotion_time &&
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053092 drv->states[last_idx + 1].exit_latency <= latency_req) {
Len Brown4f86d3a2007-10-03 18:58:00 -040093 last_state->stats.promotion_count++;
94 last_state->stats.demotion_count = 0;
95 if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
96 ladder_do_selection(ldev, last_idx, last_idx + 1);
97 return last_idx + 1;
98 }
99 }
100
101 /* consider demotion */
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700102 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530103 drv->states[last_idx].exit_latency > latency_req) {
venkatesh.pallipadi@intel.com06d9e902008-07-30 19:21:44 -0700104 int i;
105
106 for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530107 if (drv->states[i].exit_latency <= latency_req)
venkatesh.pallipadi@intel.com06d9e902008-07-30 19:21:44 -0700108 break;
109 }
110 ladder_do_selection(ldev, last_idx, i);
111 return i;
112 }
113
114 if (last_idx > CPUIDLE_DRIVER_STATE_START &&
Len Brown4f86d3a2007-10-03 18:58:00 -0400115 last_residency < last_state->threshold.demotion_time) {
116 last_state->stats.demotion_count++;
117 last_state->stats.promotion_count = 0;
118 if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
119 ladder_do_selection(ldev, last_idx, last_idx - 1);
120 return last_idx - 1;
121 }
122 }
123
124 /* otherwise remain at the current state */
125 return last_idx;
126}
127
128/**
129 * ladder_enable_device - setup for the governor
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530130 * @drv: cpuidle driver
Len Brown4f86d3a2007-10-03 18:58:00 -0400131 * @dev: the CPU
132 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530133static int ladder_enable_device(struct cpuidle_driver *drv,
134 struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400135{
136 int i;
137 struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
138 struct ladder_device_state *lstate;
139 struct cpuidle_state *state;
140
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700141 ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -0400142
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530143 for (i = 0; i < drv->state_count; i++) {
144 state = &drv->states[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400145 lstate = &ldev->states[i];
146
147 lstate->stats.promotion_count = 0;
148 lstate->stats.demotion_count = 0;
149
150 lstate->threshold.promotion_count = PROMOTION_COUNT;
151 lstate->threshold.demotion_count = DEMOTION_COUNT;
152
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530153 if (i < drv->state_count - 1)
Len Brown4f86d3a2007-10-03 18:58:00 -0400154 lstate->threshold.promotion_time = state->exit_latency;
155 if (i > 0)
156 lstate->threshold.demotion_time = state->exit_latency;
157 }
158
159 return 0;
160}
161
Deepthi Dharware978aa72011-10-28 16:20:09 +0530162/**
163 * ladder_reflect - update the correct last_state_idx
164 * @dev: the CPU
165 * @index: the index of actual state entered
166 */
167static void ladder_reflect(struct cpuidle_device *dev, int index)
168{
169 struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
170 if (index > 0)
171 ldev->last_state_idx = index;
172}
173
Len Brown4f86d3a2007-10-03 18:58:00 -0400174static struct cpuidle_governor ladder_governor = {
175 .name = "ladder",
176 .rating = 10,
177 .enable = ladder_enable_device,
178 .select = ladder_select_state,
Deepthi Dharware978aa72011-10-28 16:20:09 +0530179 .reflect = ladder_reflect,
Len Brown4f86d3a2007-10-03 18:58:00 -0400180 .owner = THIS_MODULE,
181};
182
183/**
184 * init_ladder - initializes the governor
185 */
186static int __init init_ladder(void)
187{
188 return cpuidle_register_governor(&ladder_governor);
189}
190
191/**
192 * exit_ladder - exits the governor
193 */
194static void __exit exit_ladder(void)
195{
196 cpuidle_unregister_governor(&ladder_governor);
197}
198
199MODULE_LICENSE("GPL");
200module_init(init_ladder);
201module_exit(exit_ladder);