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> 17e8db0be1SJean Pihet #include <linux/pm_qos.h> 184f86d3a8SLen Brown #include <linux/jiffies.h> 1966a5f6b6SJean Delvare #include <linux/tick.h> 20c523c68dSRamesh Thomas #include <linux/cpu.h> 214f86d3a8SLen Brown 224f86d3a8SLen Brown #include <asm/io.h> 237c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 244f86d3a8SLen Brown 254f86d3a8SLen Brown #define PROMOTION_COUNT 4 264f86d3a8SLen Brown #define DEMOTION_COUNT 1 274f86d3a8SLen Brown 284f86d3a8SLen Brown struct ladder_device_state { 294f86d3a8SLen Brown struct { 304f86d3a8SLen Brown u32 promotion_count; 314f86d3a8SLen Brown u32 demotion_count; 324f86d3a8SLen Brown u32 promotion_time; 334f86d3a8SLen Brown u32 demotion_time; 344f86d3a8SLen Brown } threshold; 354f86d3a8SLen Brown struct { 364f86d3a8SLen Brown int promotion_count; 374f86d3a8SLen Brown int demotion_count; 384f86d3a8SLen Brown } stats; 394f86d3a8SLen Brown }; 404f86d3a8SLen Brown 414f86d3a8SLen Brown struct ladder_device { 424f86d3a8SLen Brown struct ladder_device_state states[CPUIDLE_STATE_MAX]; 434f86d3a8SLen Brown int last_state_idx; 444f86d3a8SLen Brown }; 454f86d3a8SLen Brown 464f86d3a8SLen Brown static DEFINE_PER_CPU(struct ladder_device, ladder_devices); 474f86d3a8SLen Brown 484f86d3a8SLen Brown /** 494f86d3a8SLen Brown * ladder_do_selection - prepares private data for a state change 504f86d3a8SLen Brown * @ldev: the ladder device 514f86d3a8SLen Brown * @old_idx: the current state index 524f86d3a8SLen Brown * @new_idx: the new target state index 534f86d3a8SLen Brown */ 544f86d3a8SLen Brown static inline void ladder_do_selection(struct ladder_device *ldev, 554f86d3a8SLen Brown int old_idx, int new_idx) 564f86d3a8SLen Brown { 574f86d3a8SLen Brown ldev->states[old_idx].stats.promotion_count = 0; 584f86d3a8SLen Brown ldev->states[old_idx].stats.demotion_count = 0; 594f86d3a8SLen Brown ldev->last_state_idx = new_idx; 604f86d3a8SLen Brown } 614f86d3a8SLen Brown 624f86d3a8SLen Brown /** 634f86d3a8SLen Brown * ladder_select_state - selects the next state to enter 6446bcfad7SDeepthi Dharwar * @drv: cpuidle driver 654f86d3a8SLen Brown * @dev: the CPU 66*45f1ff59SRafael J. Wysocki * @dummy: not used 674f86d3a8SLen Brown */ 6846bcfad7SDeepthi Dharwar static int ladder_select_state(struct cpuidle_driver *drv, 69*45f1ff59SRafael J. Wysocki struct cpuidle_device *dev, bool *dummy) 704f86d3a8SLen Brown { 71229b6863SChristoph Lameter struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); 72c523c68dSRamesh Thomas struct device *device = get_cpu_device(dev->cpu); 734f86d3a8SLen Brown struct ladder_device_state *last_state; 744f86d3a8SLen Brown int last_residency, last_idx = ldev->last_state_idx; 75dc2251bfSRafael J. Wysocki int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; 76ed77134bSMark Gross int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); 77c523c68dSRamesh Thomas int resume_latency = dev_pm_qos_raw_read_value(device); 78c523c68dSRamesh Thomas 79c523c68dSRamesh Thomas if (resume_latency < latency_req && 80c523c68dSRamesh Thomas resume_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT) 81c523c68dSRamesh Thomas latency_req = resume_latency; 824f86d3a8SLen Brown 83a2bd9202Svenkatesh.pallipadi@intel.com /* Special case when user has set very strict latency requirement */ 84a2bd9202Svenkatesh.pallipadi@intel.com if (unlikely(latency_req == 0)) { 85a2bd9202Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, 0); 86a2bd9202Svenkatesh.pallipadi@intel.com return 0; 87a2bd9202Svenkatesh.pallipadi@intel.com } 88a2bd9202Svenkatesh.pallipadi@intel.com 894f86d3a8SLen Brown last_state = &ldev->states[last_idx]; 904f86d3a8SLen Brown 91b73026b9SLen Brown last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency; 924f86d3a8SLen Brown 934f86d3a8SLen Brown /* consider promotion */ 9446bcfad7SDeepthi Dharwar if (last_idx < drv->state_count - 1 && 9566804c13SRafael J. Wysocki !drv->states[last_idx + 1].disabled && 9662d6ae88SCarsten Emde !dev->states_usage[last_idx + 1].disable && 974f86d3a8SLen Brown last_residency > last_state->threshold.promotion_time && 9846bcfad7SDeepthi Dharwar drv->states[last_idx + 1].exit_latency <= latency_req) { 994f86d3a8SLen Brown last_state->stats.promotion_count++; 1004f86d3a8SLen Brown last_state->stats.demotion_count = 0; 1014f86d3a8SLen Brown if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { 1024f86d3a8SLen Brown ladder_do_selection(ldev, last_idx, last_idx + 1); 1034f86d3a8SLen Brown return last_idx + 1; 1044f86d3a8SLen Brown } 1054f86d3a8SLen Brown } 1064f86d3a8SLen Brown 1074f86d3a8SLen Brown /* consider demotion */ 108dc2251bfSRafael J. Wysocki if (last_idx > first_idx && 10966804c13SRafael J. Wysocki (drv->states[last_idx].disabled || 11066804c13SRafael J. Wysocki dev->states_usage[last_idx].disable || 11162d6ae88SCarsten Emde drv->states[last_idx].exit_latency > latency_req)) { 11206d9e908Svenkatesh.pallipadi@intel.com int i; 11306d9e908Svenkatesh.pallipadi@intel.com 114dc2251bfSRafael J. Wysocki for (i = last_idx - 1; i > first_idx; i--) { 11546bcfad7SDeepthi Dharwar if (drv->states[i].exit_latency <= latency_req) 11606d9e908Svenkatesh.pallipadi@intel.com break; 11706d9e908Svenkatesh.pallipadi@intel.com } 11806d9e908Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, i); 11906d9e908Svenkatesh.pallipadi@intel.com return i; 12006d9e908Svenkatesh.pallipadi@intel.com } 12106d9e908Svenkatesh.pallipadi@intel.com 122dc2251bfSRafael J. Wysocki if (last_idx > first_idx && 1234f86d3a8SLen Brown last_residency < last_state->threshold.demotion_time) { 1244f86d3a8SLen Brown last_state->stats.demotion_count++; 1254f86d3a8SLen Brown last_state->stats.promotion_count = 0; 1264f86d3a8SLen Brown if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) { 1274f86d3a8SLen Brown ladder_do_selection(ldev, last_idx, last_idx - 1); 1284f86d3a8SLen Brown return last_idx - 1; 1294f86d3a8SLen Brown } 1304f86d3a8SLen Brown } 1314f86d3a8SLen Brown 1324f86d3a8SLen Brown /* otherwise remain at the current state */ 1334f86d3a8SLen Brown return last_idx; 1344f86d3a8SLen Brown } 1354f86d3a8SLen Brown 1364f86d3a8SLen Brown /** 1374f86d3a8SLen Brown * ladder_enable_device - setup for the governor 13846bcfad7SDeepthi Dharwar * @drv: cpuidle driver 1394f86d3a8SLen Brown * @dev: the CPU 1404f86d3a8SLen Brown */ 14146bcfad7SDeepthi Dharwar static int ladder_enable_device(struct cpuidle_driver *drv, 14246bcfad7SDeepthi Dharwar struct cpuidle_device *dev) 1434f86d3a8SLen Brown { 1444f86d3a8SLen Brown int i; 145dc2251bfSRafael J. Wysocki int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0; 1464f86d3a8SLen Brown struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); 1474f86d3a8SLen Brown struct ladder_device_state *lstate; 1484f86d3a8SLen Brown struct cpuidle_state *state; 1494f86d3a8SLen Brown 150dc2251bfSRafael J. Wysocki ldev->last_state_idx = first_idx; 1514f86d3a8SLen Brown 152dc2251bfSRafael J. Wysocki for (i = first_idx; i < drv->state_count; i++) { 15346bcfad7SDeepthi Dharwar state = &drv->states[i]; 1544f86d3a8SLen Brown lstate = &ldev->states[i]; 1554f86d3a8SLen Brown 1564f86d3a8SLen Brown lstate->stats.promotion_count = 0; 1574f86d3a8SLen Brown lstate->stats.demotion_count = 0; 1584f86d3a8SLen Brown 1594f86d3a8SLen Brown lstate->threshold.promotion_count = PROMOTION_COUNT; 1604f86d3a8SLen Brown lstate->threshold.demotion_count = DEMOTION_COUNT; 1614f86d3a8SLen Brown 16246bcfad7SDeepthi Dharwar if (i < drv->state_count - 1) 1634f86d3a8SLen Brown lstate->threshold.promotion_time = state->exit_latency; 164dc2251bfSRafael J. Wysocki if (i > first_idx) 1654f86d3a8SLen Brown lstate->threshold.demotion_time = state->exit_latency; 1664f86d3a8SLen Brown } 1674f86d3a8SLen Brown 1684f86d3a8SLen Brown return 0; 1694f86d3a8SLen Brown } 1704f86d3a8SLen Brown 171e978aa7dSDeepthi Dharwar /** 172e978aa7dSDeepthi Dharwar * ladder_reflect - update the correct last_state_idx 173e978aa7dSDeepthi Dharwar * @dev: the CPU 174e978aa7dSDeepthi Dharwar * @index: the index of actual state entered 175e978aa7dSDeepthi Dharwar */ 176e978aa7dSDeepthi Dharwar static void ladder_reflect(struct cpuidle_device *dev, int index) 177e978aa7dSDeepthi Dharwar { 178229b6863SChristoph Lameter struct ladder_device *ldev = this_cpu_ptr(&ladder_devices); 179e978aa7dSDeepthi Dharwar if (index > 0) 180e978aa7dSDeepthi Dharwar ldev->last_state_idx = index; 181e978aa7dSDeepthi Dharwar } 182e978aa7dSDeepthi Dharwar 1834f86d3a8SLen Brown static struct cpuidle_governor ladder_governor = { 1844f86d3a8SLen Brown .name = "ladder", 1854f86d3a8SLen Brown .rating = 10, 1864f86d3a8SLen Brown .enable = ladder_enable_device, 1874f86d3a8SLen Brown .select = ladder_select_state, 188e978aa7dSDeepthi Dharwar .reflect = ladder_reflect, 1894f86d3a8SLen Brown }; 1904f86d3a8SLen Brown 1914f86d3a8SLen Brown /** 1924f86d3a8SLen Brown * init_ladder - initializes the governor 1934f86d3a8SLen Brown */ 1944f86d3a8SLen Brown static int __init init_ladder(void) 1954f86d3a8SLen Brown { 19666a5f6b6SJean Delvare /* 19766a5f6b6SJean Delvare * When NO_HZ is disabled, or when booting with nohz=off, the ladder 19866a5f6b6SJean Delvare * governor is better so give it a higher rating than the menu 19966a5f6b6SJean Delvare * governor. 20066a5f6b6SJean Delvare */ 20166a5f6b6SJean Delvare if (!tick_nohz_enabled) 20266a5f6b6SJean Delvare ladder_governor.rating = 25; 20366a5f6b6SJean Delvare 2044f86d3a8SLen Brown return cpuidle_register_governor(&ladder_governor); 2054f86d3a8SLen Brown } 2064f86d3a8SLen Brown 207137b944eSDaniel Lezcano postcore_initcall(init_ladder); 208