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> 17d82b3518SMark Gross #include <linux/pm_qos_params.h> 184f86d3a8SLen Brown #include <linux/moduleparam.h> 194f86d3a8SLen Brown #include <linux/jiffies.h> 204f86d3a8SLen Brown 214f86d3a8SLen Brown #include <asm/io.h> 224f86d3a8SLen Brown #include <asm/uaccess.h> 234f86d3a8SLen Brown 244f86d3a8SLen Brown #define PROMOTION_COUNT 4 254f86d3a8SLen Brown #define DEMOTION_COUNT 1 264f86d3a8SLen Brown 274f86d3a8SLen Brown struct ladder_device_state { 284f86d3a8SLen Brown struct { 294f86d3a8SLen Brown u32 promotion_count; 304f86d3a8SLen Brown u32 demotion_count; 314f86d3a8SLen Brown u32 promotion_time; 324f86d3a8SLen Brown u32 demotion_time; 334f86d3a8SLen Brown } threshold; 344f86d3a8SLen Brown struct { 354f86d3a8SLen Brown int promotion_count; 364f86d3a8SLen Brown int demotion_count; 374f86d3a8SLen Brown } stats; 384f86d3a8SLen Brown }; 394f86d3a8SLen Brown 404f86d3a8SLen Brown struct ladder_device { 414f86d3a8SLen Brown struct ladder_device_state states[CPUIDLE_STATE_MAX]; 424f86d3a8SLen Brown int last_state_idx; 434f86d3a8SLen Brown }; 444f86d3a8SLen Brown 454f86d3a8SLen Brown static DEFINE_PER_CPU(struct ladder_device, ladder_devices); 464f86d3a8SLen Brown 474f86d3a8SLen Brown /** 484f86d3a8SLen Brown * ladder_do_selection - prepares private data for a state change 494f86d3a8SLen Brown * @ldev: the ladder device 504f86d3a8SLen Brown * @old_idx: the current state index 514f86d3a8SLen Brown * @new_idx: the new target state index 524f86d3a8SLen Brown */ 534f86d3a8SLen Brown static inline void ladder_do_selection(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; 584f86d3a8SLen Brown ldev->last_state_idx = new_idx; 594f86d3a8SLen Brown } 604f86d3a8SLen Brown 614f86d3a8SLen Brown /** 624f86d3a8SLen Brown * ladder_select_state - selects the next state to enter 63*46bcfad7SDeepthi Dharwar * @drv: cpuidle driver 644f86d3a8SLen Brown * @dev: the CPU 654f86d3a8SLen Brown */ 66*46bcfad7SDeepthi Dharwar static int ladder_select_state(struct cpuidle_driver *drv, 67*46bcfad7SDeepthi Dharwar struct cpuidle_device *dev) 684f86d3a8SLen Brown { 694f86d3a8SLen Brown struct ladder_device *ldev = &__get_cpu_var(ladder_devices); 704f86d3a8SLen Brown struct ladder_device_state *last_state; 714f86d3a8SLen Brown int last_residency, last_idx = ldev->last_state_idx; 72ed77134bSMark Gross int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); 734f86d3a8SLen Brown 74a2bd9202Svenkatesh.pallipadi@intel.com /* Special case when user has set very strict latency requirement */ 75a2bd9202Svenkatesh.pallipadi@intel.com if (unlikely(latency_req == 0)) { 76a2bd9202Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, 0); 77a2bd9202Svenkatesh.pallipadi@intel.com return 0; 78a2bd9202Svenkatesh.pallipadi@intel.com } 79a2bd9202Svenkatesh.pallipadi@intel.com 804f86d3a8SLen Brown last_state = &ldev->states[last_idx]; 814f86d3a8SLen Brown 82*46bcfad7SDeepthi Dharwar if (drv->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID) { 83*46bcfad7SDeepthi Dharwar last_residency = cpuidle_get_last_residency(dev) - \ 84*46bcfad7SDeepthi Dharwar drv->states[last_idx].exit_latency; 85*46bcfad7SDeepthi Dharwar } 864f86d3a8SLen Brown else 874f86d3a8SLen Brown last_residency = last_state->threshold.promotion_time + 1; 884f86d3a8SLen Brown 894f86d3a8SLen Brown /* consider promotion */ 90*46bcfad7SDeepthi Dharwar if (last_idx < drv->state_count - 1 && 914f86d3a8SLen Brown last_residency > last_state->threshold.promotion_time && 92*46bcfad7SDeepthi Dharwar drv->states[last_idx + 1].exit_latency <= latency_req) { 934f86d3a8SLen Brown last_state->stats.promotion_count++; 944f86d3a8SLen Brown last_state->stats.demotion_count = 0; 954f86d3a8SLen Brown if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { 964f86d3a8SLen Brown ladder_do_selection(ldev, last_idx, last_idx + 1); 974f86d3a8SLen Brown return last_idx + 1; 984f86d3a8SLen Brown } 994f86d3a8SLen Brown } 1004f86d3a8SLen Brown 1014f86d3a8SLen Brown /* consider demotion */ 102a2bd9202Svenkatesh.pallipadi@intel.com if (last_idx > CPUIDLE_DRIVER_STATE_START && 103*46bcfad7SDeepthi Dharwar drv->states[last_idx].exit_latency > latency_req) { 10406d9e908Svenkatesh.pallipadi@intel.com int i; 10506d9e908Svenkatesh.pallipadi@intel.com 10606d9e908Svenkatesh.pallipadi@intel.com for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) { 107*46bcfad7SDeepthi Dharwar if (drv->states[i].exit_latency <= latency_req) 10806d9e908Svenkatesh.pallipadi@intel.com break; 10906d9e908Svenkatesh.pallipadi@intel.com } 11006d9e908Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, i); 11106d9e908Svenkatesh.pallipadi@intel.com return i; 11206d9e908Svenkatesh.pallipadi@intel.com } 11306d9e908Svenkatesh.pallipadi@intel.com 11406d9e908Svenkatesh.pallipadi@intel.com if (last_idx > CPUIDLE_DRIVER_STATE_START && 1154f86d3a8SLen Brown last_residency < last_state->threshold.demotion_time) { 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) { 1194f86d3a8SLen Brown ladder_do_selection(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 130*46bcfad7SDeepthi Dharwar * @drv: cpuidle driver 1314f86d3a8SLen Brown * @dev: the CPU 1324f86d3a8SLen Brown */ 133*46bcfad7SDeepthi Dharwar static int ladder_enable_device(struct cpuidle_driver *drv, 134*46bcfad7SDeepthi Dharwar struct cpuidle_device *dev) 1354f86d3a8SLen Brown { 1364f86d3a8SLen Brown int i; 1374f86d3a8SLen Brown struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); 1384f86d3a8SLen Brown struct ladder_device_state *lstate; 1394f86d3a8SLen Brown struct cpuidle_state *state; 1404f86d3a8SLen Brown 141a2bd9202Svenkatesh.pallipadi@intel.com ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START; 1424f86d3a8SLen Brown 143*46bcfad7SDeepthi Dharwar for (i = 0; i < drv->state_count; i++) { 144*46bcfad7SDeepthi Dharwar state = &drv->states[i]; 1454f86d3a8SLen Brown lstate = &ldev->states[i]; 1464f86d3a8SLen Brown 1474f86d3a8SLen Brown lstate->stats.promotion_count = 0; 1484f86d3a8SLen Brown lstate->stats.demotion_count = 0; 1494f86d3a8SLen Brown 1504f86d3a8SLen Brown lstate->threshold.promotion_count = PROMOTION_COUNT; 1514f86d3a8SLen Brown lstate->threshold.demotion_count = DEMOTION_COUNT; 1524f86d3a8SLen Brown 153*46bcfad7SDeepthi Dharwar if (i < drv->state_count - 1) 1544f86d3a8SLen Brown lstate->threshold.promotion_time = state->exit_latency; 1554f86d3a8SLen Brown if (i > 0) 1564f86d3a8SLen Brown lstate->threshold.demotion_time = state->exit_latency; 1574f86d3a8SLen Brown } 1584f86d3a8SLen Brown 1594f86d3a8SLen Brown return 0; 1604f86d3a8SLen Brown } 1614f86d3a8SLen Brown 162e978aa7dSDeepthi Dharwar /** 163e978aa7dSDeepthi Dharwar * ladder_reflect - update the correct last_state_idx 164e978aa7dSDeepthi Dharwar * @dev: the CPU 165e978aa7dSDeepthi Dharwar * @index: the index of actual state entered 166e978aa7dSDeepthi Dharwar */ 167e978aa7dSDeepthi Dharwar static void ladder_reflect(struct cpuidle_device *dev, int index) 168e978aa7dSDeepthi Dharwar { 169e978aa7dSDeepthi Dharwar struct ladder_device *ldev = &__get_cpu_var(ladder_devices); 170e978aa7dSDeepthi Dharwar if (index > 0) 171e978aa7dSDeepthi Dharwar ldev->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 .owner = THIS_MODULE, 1814f86d3a8SLen Brown }; 1824f86d3a8SLen Brown 1834f86d3a8SLen Brown /** 1844f86d3a8SLen Brown * init_ladder - initializes the governor 1854f86d3a8SLen Brown */ 1864f86d3a8SLen Brown static int __init init_ladder(void) 1874f86d3a8SLen Brown { 1884f86d3a8SLen Brown return cpuidle_register_governor(&ladder_governor); 1894f86d3a8SLen Brown } 1904f86d3a8SLen Brown 1914f86d3a8SLen Brown /** 1924f86d3a8SLen Brown * exit_ladder - exits the governor 1934f86d3a8SLen Brown */ 1944f86d3a8SLen Brown static void __exit exit_ladder(void) 1954f86d3a8SLen Brown { 1964f86d3a8SLen Brown cpuidle_unregister_governor(&ladder_governor); 1974f86d3a8SLen Brown } 1984f86d3a8SLen Brown 1994f86d3a8SLen Brown MODULE_LICENSE("GPL"); 2004f86d3a8SLen Brown module_init(init_ladder); 2014f86d3a8SLen Brown module_exit(exit_ladder); 202