14f86d3a8SLen Brown /* 24f86d3a8SLen Brown * ladder.c - the residency ladder algorithm 34f86d3a8SLen Brown * 44f86d3a8SLen Brown * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 54f86d3a8SLen Brown * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 64f86d3a8SLen Brown * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de> 74f86d3a8SLen Brown * 84f86d3a8SLen Brown * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> 94f86d3a8SLen Brown * Shaohua Li <shaohua.li@intel.com> 104f86d3a8SLen Brown * Adam Belay <abelay@novell.com> 114f86d3a8SLen Brown * 124f86d3a8SLen Brown * This code is licenced under the GPL. 134f86d3a8SLen Brown */ 144f86d3a8SLen Brown 154f86d3a8SLen Brown #include <linux/kernel.h> 164f86d3a8SLen Brown #include <linux/cpuidle.h> 174f86d3a8SLen Brown #include <linux/jiffies.h> 1866a5f6b6SJean Delvare #include <linux/tick.h> 194f86d3a8SLen Brown 204f86d3a8SLen Brown #include <asm/io.h> 217c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 224f86d3a8SLen Brown 234f86d3a8SLen Brown #define PROMOTION_COUNT 4 244f86d3a8SLen Brown #define DEMOTION_COUNT 1 254f86d3a8SLen Brown 264f86d3a8SLen Brown struct ladder_device_state { 274f86d3a8SLen Brown struct { 284f86d3a8SLen Brown u32 promotion_count; 294f86d3a8SLen Brown u32 demotion_count; 30c1d51f68SRafael J. Wysocki u64 promotion_time_ns; 31c1d51f68SRafael J. Wysocki u64 demotion_time_ns; 324f86d3a8SLen Brown } threshold; 334f86d3a8SLen Brown struct { 344f86d3a8SLen Brown int promotion_count; 354f86d3a8SLen Brown int demotion_count; 364f86d3a8SLen Brown } stats; 374f86d3a8SLen Brown }; 384f86d3a8SLen Brown 394f86d3a8SLen Brown struct ladder_device { 404f86d3a8SLen Brown struct ladder_device_state states[CPUIDLE_STATE_MAX]; 414f86d3a8SLen Brown }; 424f86d3a8SLen Brown 434f86d3a8SLen Brown static DEFINE_PER_CPU(struct ladder_device, ladder_devices); 444f86d3a8SLen Brown 454f86d3a8SLen Brown /** 464f86d3a8SLen Brown * ladder_do_selection - prepares private data for a state change 47*77445923SJeff Johnson * @dev: the CPU 484f86d3a8SLen Brown * @ldev: the ladder device 494f86d3a8SLen Brown * @old_idx: the current state index 504f86d3a8SLen Brown * @new_idx: the new target state index 514f86d3a8SLen Brown */ 527d4daeedSMarcelo Tosatti static inline void ladder_do_selection(struct cpuidle_device *dev, 537d4daeedSMarcelo Tosatti struct ladder_device *ldev, 544f86d3a8SLen Brown int old_idx, int new_idx) 554f86d3a8SLen Brown { 564f86d3a8SLen Brown ldev->states[old_idx].stats.promotion_count = 0; 574f86d3a8SLen Brown ldev->states[old_idx].stats.demotion_count = 0; 587d4daeedSMarcelo Tosatti dev->last_state_idx = new_idx; 594f86d3a8SLen Brown } 604f86d3a8SLen Brown 614f86d3a8SLen Brown /** 624f86d3a8SLen Brown * ladder_select_state - selects the next state to enter 6346bcfad7SDeepthi Dharwar * @drv: cpuidle driver 644f86d3a8SLen Brown * @dev: the CPU 6545f1ff59SRafael J. Wysocki * @dummy: not used 664f86d3a8SLen Brown */ 6746bcfad7SDeepthi Dharwar static int ladder_select_state(struct cpuidle_driver *drv, 6845f1ff59SRafael J. Wysocki struct cpuidle_device *dev, bool *dummy) 694f86d3a8SLen Brown { 70229b6863SChristoph Lameter struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); 714f86d3a8SLen Brown struct ladder_device_state *last_state; 72c1d51f68SRafael J. Wysocki int last_idx = dev->last_state_idx; 73dc2251bfSRafael J. Wysocki int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; 74c1d51f68SRafael J. Wysocki s64 latency_req = cpuidle_governor_latency_req(dev->cpu); 75c1d51f68SRafael J. Wysocki s64 last_residency; 764f86d3a8SLen Brown 77a2bd9202Svenkatesh.pallipadi@intel.com /* Special case when user has set very strict latency requirement */ 78a2bd9202Svenkatesh.pallipadi@intel.com if (unlikely(latency_req == 0)) { 797d4daeedSMarcelo Tosatti ladder_do_selection(dev, ldev, last_idx, 0); 80a2bd9202Svenkatesh.pallipadi@intel.com return 0; 81a2bd9202Svenkatesh.pallipadi@intel.com } 82a2bd9202Svenkatesh.pallipadi@intel.com 834f86d3a8SLen Brown last_state = &ldev->states[last_idx]; 844f86d3a8SLen Brown 85c1d51f68SRafael J. Wysocki last_residency = dev->last_residency_ns - drv->states[last_idx].exit_latency_ns; 864f86d3a8SLen Brown 874f86d3a8SLen Brown /* consider promotion */ 8846bcfad7SDeepthi Dharwar if (last_idx < drv->state_count - 1 && 8962d6ae88SCarsten Emde !dev->states_usage[last_idx + 1].disable && 90c1d51f68SRafael J. Wysocki last_residency > last_state->threshold.promotion_time_ns && 91c1d51f68SRafael J. Wysocki drv->states[last_idx + 1].exit_latency_ns <= latency_req) { 924f86d3a8SLen Brown last_state->stats.promotion_count++; 934f86d3a8SLen Brown last_state->stats.demotion_count = 0; 944f86d3a8SLen Brown if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { 957d4daeedSMarcelo Tosatti ladder_do_selection(dev, ldev, last_idx, last_idx + 1); 964f86d3a8SLen Brown return last_idx + 1; 974f86d3a8SLen Brown } 984f86d3a8SLen Brown } 994f86d3a8SLen Brown 1004f86d3a8SLen Brown /* consider demotion */ 101dc2251bfSRafael J. Wysocki if (last_idx > first_idx && 10299e98d3fSRafael J. Wysocki (dev->states_usage[last_idx].disable || 103c1d51f68SRafael J. Wysocki drv->states[last_idx].exit_latency_ns > latency_req)) { 10406d9e908Svenkatesh.pallipadi@intel.com int i; 10506d9e908Svenkatesh.pallipadi@intel.com 106dc2251bfSRafael J. Wysocki for (i = last_idx - 1; i > first_idx; i--) { 107c1d51f68SRafael J. Wysocki if (drv->states[i].exit_latency_ns <= latency_req) 10806d9e908Svenkatesh.pallipadi@intel.com break; 10906d9e908Svenkatesh.pallipadi@intel.com } 1107d4daeedSMarcelo Tosatti ladder_do_selection(dev, ldev, last_idx, i); 11106d9e908Svenkatesh.pallipadi@intel.com return i; 11206d9e908Svenkatesh.pallipadi@intel.com } 11306d9e908Svenkatesh.pallipadi@intel.com 114dc2251bfSRafael J. Wysocki if (last_idx > first_idx && 115c1d51f68SRafael J. Wysocki last_residency < last_state->threshold.demotion_time_ns) { 1164f86d3a8SLen Brown last_state->stats.demotion_count++; 1174f86d3a8SLen Brown last_state->stats.promotion_count = 0; 1184f86d3a8SLen Brown if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) { 1197d4daeedSMarcelo Tosatti ladder_do_selection(dev, ldev, last_idx, last_idx - 1); 1204f86d3a8SLen Brown return last_idx - 1; 1214f86d3a8SLen Brown } 1224f86d3a8SLen Brown } 1234f86d3a8SLen Brown 1244f86d3a8SLen Brown /* otherwise remain at the current state */ 1254f86d3a8SLen Brown return last_idx; 1264f86d3a8SLen Brown } 1274f86d3a8SLen Brown 1284f86d3a8SLen Brown /** 1294f86d3a8SLen Brown * ladder_enable_device - setup for the governor 13046bcfad7SDeepthi Dharwar * @drv: cpuidle driver 1314f86d3a8SLen Brown * @dev: the CPU 1324f86d3a8SLen Brown */ 13346bcfad7SDeepthi Dharwar static int ladder_enable_device(struct cpuidle_driver *drv, 13446bcfad7SDeepthi Dharwar struct cpuidle_device *dev) 1354f86d3a8SLen Brown { 1364f86d3a8SLen Brown int i; 137dc2251bfSRafael J. Wysocki int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; 1384f86d3a8SLen Brown struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); 1394f86d3a8SLen Brown struct ladder_device_state *lstate; 1404f86d3a8SLen Brown struct cpuidle_state *state; 1414f86d3a8SLen Brown 1427d4daeedSMarcelo Tosatti dev->last_state_idx = first_idx; 1434f86d3a8SLen Brown 144dc2251bfSRafael J. Wysocki for (i = first_idx; i < drv->state_count; i++) { 14546bcfad7SDeepthi Dharwar state = &drv->states[i]; 1464f86d3a8SLen Brown lstate = &ldev->states[i]; 1474f86d3a8SLen Brown 1484f86d3a8SLen Brown lstate->stats.promotion_count = 0; 1494f86d3a8SLen Brown lstate->stats.demotion_count = 0; 1504f86d3a8SLen Brown 1514f86d3a8SLen Brown lstate->threshold.promotion_count = PROMOTION_COUNT; 1524f86d3a8SLen Brown lstate->threshold.demotion_count = DEMOTION_COUNT; 1534f86d3a8SLen Brown 15446bcfad7SDeepthi Dharwar if (i < drv->state_count - 1) 155c1d51f68SRafael J. Wysocki lstate->threshold.promotion_time_ns = state->exit_latency_ns; 156dc2251bfSRafael J. Wysocki if (i > first_idx) 157c1d51f68SRafael J. Wysocki lstate->threshold.demotion_time_ns = state->exit_latency_ns; 1584f86d3a8SLen Brown } 1594f86d3a8SLen Brown 1604f86d3a8SLen Brown return 0; 1614f86d3a8SLen Brown } 1624f86d3a8SLen Brown 163e978aa7dSDeepthi Dharwar /** 164e978aa7dSDeepthi Dharwar * ladder_reflect - update the correct last_state_idx 165e978aa7dSDeepthi Dharwar * @dev: the CPU 166e978aa7dSDeepthi Dharwar * @index: the index of actual state entered 167e978aa7dSDeepthi Dharwar */ 168e978aa7dSDeepthi Dharwar static void ladder_reflect(struct cpuidle_device *dev, int index) 169e978aa7dSDeepthi Dharwar { 170e978aa7dSDeepthi Dharwar if (index > 0) 1717d4daeedSMarcelo Tosatti dev->last_state_idx = index; 172e978aa7dSDeepthi Dharwar } 173e978aa7dSDeepthi Dharwar 1744f86d3a8SLen Brown static struct cpuidle_governor ladder_governor = { 1754f86d3a8SLen Brown .name = "ladder", 1764f86d3a8SLen Brown .rating = 10, 1774f86d3a8SLen Brown .enable = ladder_enable_device, 1784f86d3a8SLen Brown .select = ladder_select_state, 179e978aa7dSDeepthi Dharwar .reflect = ladder_reflect, 1804f86d3a8SLen Brown }; 1814f86d3a8SLen Brown 1824f86d3a8SLen Brown /** 1834f86d3a8SLen Brown * init_ladder - initializes the governor 1844f86d3a8SLen Brown */ 1854f86d3a8SLen Brown static int __init init_ladder(void) 1864f86d3a8SLen Brown { 18766a5f6b6SJean Delvare /* 18866a5f6b6SJean Delvare * When NO_HZ is disabled, or when booting with nohz=off, the ladder 18966a5f6b6SJean Delvare * governor is better so give it a higher rating than the menu 19066a5f6b6SJean Delvare * governor. 19166a5f6b6SJean Delvare */ 19266a5f6b6SJean Delvare if (!tick_nohz_enabled) 19366a5f6b6SJean Delvare ladder_governor.rating = 25; 19466a5f6b6SJean Delvare 1954f86d3a8SLen Brown return cpuidle_register_governor(&ladder_governor); 1964f86d3a8SLen Brown } 1974f86d3a8SLen Brown 198137b944eSDaniel Lezcano postcore_initcall(init_ladder); 199