Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 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> |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 17 | #include <linux/pm_qos.h> |
Paul Gortmaker | 70e522a | 2011-08-29 17:52:39 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 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 | |
| 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; |
| 38 | }; |
| 39 | |
| 40 | struct ladder_device { |
| 41 | struct ladder_device_state states[CPUIDLE_STATE_MAX]; |
| 42 | int last_state_idx; |
| 43 | }; |
| 44 | |
| 45 | static 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 | */ |
| 53 | static 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 Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 63 | * @drv: cpuidle driver |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 64 | * @dev: the CPU |
| 65 | */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 66 | static int ladder_select_state(struct cpuidle_driver *drv, |
| 67 | struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 68 | { |
Christoph Lameter | 229b686 | 2014-08-17 12:30:30 -0500 | [diff] [blame] | 69 | struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 70 | struct ladder_device_state *last_state; |
| 71 | int last_residency, last_idx = ldev->last_state_idx; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 72 | int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 73 | |
venkatesh.pallipadi@intel.com | a2bd9202 | 2008-07-30 19:21:42 -0700 | [diff] [blame] | 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 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 80 | last_state = &ldev->states[last_idx]; |
| 81 | |
Len Brown | b73026b | 2014-12-16 01:52:07 -0500 | [diff] [blame] | 82 | last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 83 | |
| 84 | /* consider promotion */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 85 | if (last_idx < drv->state_count - 1 && |
Rafael J. Wysocki | 66804c1 | 2012-08-15 20:28:52 +0200 | [diff] [blame] | 86 | !drv->states[last_idx + 1].disabled && |
Carsten Emde | 62d6ae8 | 2012-07-19 20:34:10 +0000 | [diff] [blame] | 87 | !dev->states_usage[last_idx + 1].disable && |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 88 | last_residency > last_state->threshold.promotion_time && |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 89 | drv->states[last_idx + 1].exit_latency <= latency_req) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 90 | last_state->stats.promotion_count++; |
| 91 | last_state->stats.demotion_count = 0; |
| 92 | if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { |
| 93 | ladder_do_selection(ldev, last_idx, last_idx + 1); |
| 94 | return last_idx + 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* consider demotion */ |
venkatesh.pallipadi@intel.com | a2bd9202 | 2008-07-30 19:21:42 -0700 | [diff] [blame] | 99 | if (last_idx > CPUIDLE_DRIVER_STATE_START && |
Rafael J. Wysocki | 66804c1 | 2012-08-15 20:28:52 +0200 | [diff] [blame] | 100 | (drv->states[last_idx].disabled || |
| 101 | dev->states_usage[last_idx].disable || |
Carsten Emde | 62d6ae8 | 2012-07-19 20:34:10 +0000 | [diff] [blame] | 102 | drv->states[last_idx].exit_latency > latency_req)) { |
venkatesh.pallipadi@intel.com | 06d9e90 | 2008-07-30 19:21:44 -0700 | [diff] [blame] | 103 | int i; |
| 104 | |
| 105 | for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 106 | if (drv->states[i].exit_latency <= latency_req) |
venkatesh.pallipadi@intel.com | 06d9e90 | 2008-07-30 19:21:44 -0700 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | ladder_do_selection(ldev, last_idx, i); |
| 110 | return i; |
| 111 | } |
| 112 | |
| 113 | if (last_idx > CPUIDLE_DRIVER_STATE_START && |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 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; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* otherwise remain at the current state */ |
| 124 | return last_idx; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ladder_enable_device - setup for the governor |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 129 | * @drv: cpuidle driver |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 130 | * @dev: the CPU |
| 131 | */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 132 | static int ladder_enable_device(struct cpuidle_driver *drv, |
| 133 | struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 134 | { |
| 135 | int i; |
| 136 | struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); |
| 137 | struct ladder_device_state *lstate; |
| 138 | struct cpuidle_state *state; |
| 139 | |
venkatesh.pallipadi@intel.com | a2bd9202 | 2008-07-30 19:21:42 -0700 | [diff] [blame] | 140 | ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 141 | |
Mohammad Merajul Islam Molla | 1469244 | 2014-07-28 09:58:50 +0600 | [diff] [blame] | 142 | for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 143 | state = &drv->states[i]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 144 | lstate = &ldev->states[i]; |
| 145 | |
| 146 | lstate->stats.promotion_count = 0; |
| 147 | lstate->stats.demotion_count = 0; |
| 148 | |
| 149 | lstate->threshold.promotion_count = PROMOTION_COUNT; |
| 150 | lstate->threshold.demotion_count = DEMOTION_COUNT; |
| 151 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 152 | if (i < drv->state_count - 1) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 153 | lstate->threshold.promotion_time = state->exit_latency; |
Mohammad Merajul Islam Molla | 1469244 | 2014-07-28 09:58:50 +0600 | [diff] [blame] | 154 | if (i > CPUIDLE_DRIVER_STATE_START) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 155 | lstate->threshold.demotion_time = state->exit_latency; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 161 | /** |
| 162 | * ladder_reflect - update the correct last_state_idx |
| 163 | * @dev: the CPU |
| 164 | * @index: the index of actual state entered |
| 165 | */ |
| 166 | static void ladder_reflect(struct cpuidle_device *dev, int index) |
| 167 | { |
Christoph Lameter | 229b686 | 2014-08-17 12:30:30 -0500 | [diff] [blame] | 168 | struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 169 | if (index > 0) |
| 170 | ldev->last_state_idx = index; |
| 171 | } |
| 172 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 173 | static struct cpuidle_governor ladder_governor = { |
| 174 | .name = "ladder", |
| 175 | .rating = 10, |
| 176 | .enable = ladder_enable_device, |
| 177 | .select = ladder_select_state, |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 178 | .reflect = ladder_reflect, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 179 | .owner = THIS_MODULE, |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * init_ladder - initializes the governor |
| 184 | */ |
| 185 | static int __init init_ladder(void) |
| 186 | { |
| 187 | return cpuidle_register_governor(&ladder_governor); |
| 188 | } |
| 189 | |
Daniel Lezcano | 137b944 | 2013-06-12 15:08:48 +0200 | [diff] [blame] | 190 | postcore_initcall(init_ladder); |