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> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 17 | #include <linux/jiffies.h> |
Jean Delvare | 66a5f6b | 2016-01-11 17:41:53 +0100 | [diff] [blame] | 18 | #include <linux/tick.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 19 | |
| 20 | #include <asm/io.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 22 | |
| 23 | #define PROMOTION_COUNT 4 |
| 24 | #define DEMOTION_COUNT 1 |
| 25 | |
| 26 | struct ladder_device_state { |
| 27 | struct { |
| 28 | u32 promotion_count; |
| 29 | u32 demotion_count; |
| 30 | u32 promotion_time; |
| 31 | u32 demotion_time; |
| 32 | } threshold; |
| 33 | struct { |
| 34 | int promotion_count; |
| 35 | int demotion_count; |
| 36 | } stats; |
| 37 | }; |
| 38 | |
| 39 | struct ladder_device { |
| 40 | struct ladder_device_state states[CPUIDLE_STATE_MAX]; |
| 41 | int last_state_idx; |
| 42 | }; |
| 43 | |
| 44 | static DEFINE_PER_CPU(struct ladder_device, ladder_devices); |
| 45 | |
| 46 | /** |
| 47 | * ladder_do_selection - prepares private data for a state change |
| 48 | * @ldev: the ladder device |
| 49 | * @old_idx: the current state index |
| 50 | * @new_idx: the new target state index |
| 51 | */ |
| 52 | static inline void ladder_do_selection(struct ladder_device *ldev, |
| 53 | int old_idx, int new_idx) |
| 54 | { |
| 55 | ldev->states[old_idx].stats.promotion_count = 0; |
| 56 | ldev->states[old_idx].stats.demotion_count = 0; |
| 57 | ldev->last_state_idx = new_idx; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * ladder_select_state - selects the next state to enter |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 62 | * @drv: cpuidle driver |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 63 | * @dev: the CPU |
Rafael J. Wysocki | 45f1ff5 | 2018-03-22 17:50:49 +0100 | [diff] [blame] | 64 | * @dummy: not used |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 65 | */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 66 | static int ladder_select_state(struct cpuidle_driver *drv, |
Rafael J. Wysocki | 45f1ff5 | 2018-03-22 17:50:49 +0100 | [diff] [blame] | 67 | struct cpuidle_device *dev, bool *dummy) |
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; |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 72 | int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; |
Rafael J. Wysocki | 0fc784f | 2018-05-30 13:43:01 +0200 | [diff] [blame] | 73 | int latency_req = cpuidle_governor_latency_req(dev->cpu); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 74 | |
venkatesh.pallipadi@intel.com | a2bd9202 | 2008-07-30 19:21:42 -0700 | [diff] [blame] | 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; |
| 79 | } |
| 80 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 81 | last_state = &ldev->states[last_idx]; |
| 82 | |
Len Brown | b73026b | 2014-12-16 01:52:07 -0500 | [diff] [blame] | 83 | 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] | 84 | |
| 85 | /* consider promotion */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 86 | if (last_idx < drv->state_count - 1 && |
Rafael J. Wysocki | 66804c1 | 2012-08-15 20:28:52 +0200 | [diff] [blame] | 87 | !drv->states[last_idx + 1].disabled && |
Carsten Emde | 62d6ae8 | 2012-07-19 20:34:10 +0000 | [diff] [blame] | 88 | !dev->states_usage[last_idx + 1].disable && |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 89 | last_residency > last_state->threshold.promotion_time && |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 90 | drv->states[last_idx + 1].exit_latency <= latency_req) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 91 | last_state->stats.promotion_count++; |
| 92 | last_state->stats.demotion_count = 0; |
| 93 | if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { |
| 94 | ladder_do_selection(ldev, last_idx, last_idx + 1); |
| 95 | return last_idx + 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* consider demotion */ |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 100 | if (last_idx > first_idx && |
Rafael J. Wysocki | 66804c1 | 2012-08-15 20:28:52 +0200 | [diff] [blame] | 101 | (drv->states[last_idx].disabled || |
| 102 | dev->states_usage[last_idx].disable || |
Carsten Emde | 62d6ae8 | 2012-07-19 20:34:10 +0000 | [diff] [blame] | 103 | drv->states[last_idx].exit_latency > latency_req)) { |
venkatesh.pallipadi@intel.com | 06d9e90 | 2008-07-30 19:21:44 -0700 | [diff] [blame] | 104 | int i; |
| 105 | |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 106 | for (i = last_idx - 1; i > first_idx; i--) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 107 | if (drv->states[i].exit_latency <= latency_req) |
venkatesh.pallipadi@intel.com | 06d9e90 | 2008-07-30 19:21:44 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | ladder_do_selection(ldev, last_idx, i); |
| 111 | return i; |
| 112 | } |
| 113 | |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 114 | if (last_idx > first_idx && |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 115 | 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 Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 130 | * @drv: cpuidle driver |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 131 | * @dev: the CPU |
| 132 | */ |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 133 | static int ladder_enable_device(struct cpuidle_driver *drv, |
| 134 | struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 135 | { |
| 136 | int i; |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 137 | int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 138 | struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); |
| 139 | struct ladder_device_state *lstate; |
| 140 | struct cpuidle_state *state; |
| 141 | |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 142 | ldev->last_state_idx = first_idx; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 143 | |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 144 | for (i = first_idx; i < drv->state_count; i++) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 145 | state = &drv->states[i]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 146 | lstate = &ldev->states[i]; |
| 147 | |
| 148 | lstate->stats.promotion_count = 0; |
| 149 | lstate->stats.demotion_count = 0; |
| 150 | |
| 151 | lstate->threshold.promotion_count = PROMOTION_COUNT; |
| 152 | lstate->threshold.demotion_count = DEMOTION_COUNT; |
| 153 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 154 | if (i < drv->state_count - 1) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 155 | lstate->threshold.promotion_time = state->exit_latency; |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 156 | if (i > first_idx) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 157 | lstate->threshold.demotion_time = state->exit_latency; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 163 | /** |
| 164 | * ladder_reflect - update the correct last_state_idx |
| 165 | * @dev: the CPU |
| 166 | * @index: the index of actual state entered |
| 167 | */ |
| 168 | static void ladder_reflect(struct cpuidle_device *dev, int index) |
| 169 | { |
Christoph Lameter | 229b686 | 2014-08-17 12:30:30 -0500 | [diff] [blame] | 170 | struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 171 | if (index > 0) |
| 172 | ldev->last_state_idx = index; |
| 173 | } |
| 174 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 175 | static struct cpuidle_governor ladder_governor = { |
| 176 | .name = "ladder", |
| 177 | .rating = 10, |
| 178 | .enable = ladder_enable_device, |
| 179 | .select = ladder_select_state, |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 180 | .reflect = ladder_reflect, |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * init_ladder - initializes the governor |
| 185 | */ |
| 186 | static int __init init_ladder(void) |
| 187 | { |
Jean Delvare | 66a5f6b | 2016-01-11 17:41:53 +0100 | [diff] [blame] | 188 | /* |
| 189 | * When NO_HZ is disabled, or when booting with nohz=off, the ladder |
| 190 | * governor is better so give it a higher rating than the menu |
| 191 | * governor. |
| 192 | */ |
| 193 | if (!tick_nohz_enabled) |
| 194 | ladder_governor.rating = 25; |
| 195 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 196 | return cpuidle_register_governor(&ladder_governor); |
| 197 | } |
| 198 | |
Daniel Lezcano | 137b944 | 2013-06-12 15:08:48 +0200 | [diff] [blame] | 199 | postcore_initcall(init_ladder); |