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 634f86d3a8SLen Brown * @dev: the CPU 644f86d3a8SLen Brown */ 654f86d3a8SLen Brown static int ladder_select_state(struct cpuidle_device *dev) 664f86d3a8SLen Brown { 674f86d3a8SLen Brown struct ladder_device *ldev = &__get_cpu_var(ladder_devices); 684f86d3a8SLen Brown struct ladder_device_state *last_state; 694f86d3a8SLen Brown int last_residency, last_idx = ldev->last_state_idx; 70ed77134bSMark Gross int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); 714f86d3a8SLen Brown 72a2bd9202Svenkatesh.pallipadi@intel.com /* Special case when user has set very strict latency requirement */ 73a2bd9202Svenkatesh.pallipadi@intel.com if (unlikely(latency_req == 0)) { 74a2bd9202Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, 0); 75a2bd9202Svenkatesh.pallipadi@intel.com return 0; 76a2bd9202Svenkatesh.pallipadi@intel.com } 77a2bd9202Svenkatesh.pallipadi@intel.com 784f86d3a8SLen Brown last_state = &ldev->states[last_idx]; 794f86d3a8SLen Brown 804f86d3a8SLen Brown if (dev->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID) 814f86d3a8SLen Brown last_residency = cpuidle_get_last_residency(dev) - dev->states[last_idx].exit_latency; 824f86d3a8SLen Brown else 834f86d3a8SLen Brown last_residency = last_state->threshold.promotion_time + 1; 844f86d3a8SLen Brown 854f86d3a8SLen Brown /* consider promotion */ 864f86d3a8SLen Brown if (last_idx < dev->state_count - 1 && 874f86d3a8SLen Brown last_residency > last_state->threshold.promotion_time && 88a2bd9202Svenkatesh.pallipadi@intel.com dev->states[last_idx + 1].exit_latency <= latency_req) { 894f86d3a8SLen Brown last_state->stats.promotion_count++; 904f86d3a8SLen Brown last_state->stats.demotion_count = 0; 914f86d3a8SLen Brown if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) { 924f86d3a8SLen Brown ladder_do_selection(ldev, last_idx, last_idx + 1); 934f86d3a8SLen Brown return last_idx + 1; 944f86d3a8SLen Brown } 954f86d3a8SLen Brown } 964f86d3a8SLen Brown 974f86d3a8SLen Brown /* consider demotion */ 98a2bd9202Svenkatesh.pallipadi@intel.com if (last_idx > CPUIDLE_DRIVER_STATE_START && 9906d9e908Svenkatesh.pallipadi@intel.com dev->states[last_idx].exit_latency > latency_req) { 10006d9e908Svenkatesh.pallipadi@intel.com int i; 10106d9e908Svenkatesh.pallipadi@intel.com 10206d9e908Svenkatesh.pallipadi@intel.com for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) { 10306d9e908Svenkatesh.pallipadi@intel.com if (dev->states[i].exit_latency <= latency_req) 10406d9e908Svenkatesh.pallipadi@intel.com break; 10506d9e908Svenkatesh.pallipadi@intel.com } 10606d9e908Svenkatesh.pallipadi@intel.com ladder_do_selection(ldev, last_idx, i); 10706d9e908Svenkatesh.pallipadi@intel.com return i; 10806d9e908Svenkatesh.pallipadi@intel.com } 10906d9e908Svenkatesh.pallipadi@intel.com 11006d9e908Svenkatesh.pallipadi@intel.com if (last_idx > CPUIDLE_DRIVER_STATE_START && 1114f86d3a8SLen Brown last_residency < last_state->threshold.demotion_time) { 1124f86d3a8SLen Brown last_state->stats.demotion_count++; 1134f86d3a8SLen Brown last_state->stats.promotion_count = 0; 1144f86d3a8SLen Brown if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) { 1154f86d3a8SLen Brown ladder_do_selection(ldev, last_idx, last_idx - 1); 1164f86d3a8SLen Brown return last_idx - 1; 1174f86d3a8SLen Brown } 1184f86d3a8SLen Brown } 1194f86d3a8SLen Brown 1204f86d3a8SLen Brown /* otherwise remain at the current state */ 1214f86d3a8SLen Brown return last_idx; 1224f86d3a8SLen Brown } 1234f86d3a8SLen Brown 1244f86d3a8SLen Brown /** 1254f86d3a8SLen Brown * ladder_enable_device - setup for the governor 1264f86d3a8SLen Brown * @dev: the CPU 1274f86d3a8SLen Brown */ 1284f86d3a8SLen Brown static int ladder_enable_device(struct cpuidle_device *dev) 1294f86d3a8SLen Brown { 1304f86d3a8SLen Brown int i; 1314f86d3a8SLen Brown struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu); 1324f86d3a8SLen Brown struct ladder_device_state *lstate; 1334f86d3a8SLen Brown struct cpuidle_state *state; 1344f86d3a8SLen Brown 135a2bd9202Svenkatesh.pallipadi@intel.com ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START; 1364f86d3a8SLen Brown 1374f86d3a8SLen Brown for (i = 0; i < dev->state_count; i++) { 1384f86d3a8SLen Brown state = &dev->states[i]; 1394f86d3a8SLen Brown lstate = &ldev->states[i]; 1404f86d3a8SLen Brown 1414f86d3a8SLen Brown lstate->stats.promotion_count = 0; 1424f86d3a8SLen Brown lstate->stats.demotion_count = 0; 1434f86d3a8SLen Brown 1444f86d3a8SLen Brown lstate->threshold.promotion_count = PROMOTION_COUNT; 1454f86d3a8SLen Brown lstate->threshold.demotion_count = DEMOTION_COUNT; 1464f86d3a8SLen Brown 1474f86d3a8SLen Brown if (i < dev->state_count - 1) 1484f86d3a8SLen Brown lstate->threshold.promotion_time = state->exit_latency; 1494f86d3a8SLen Brown if (i > 0) 1504f86d3a8SLen Brown lstate->threshold.demotion_time = state->exit_latency; 1514f86d3a8SLen Brown } 1524f86d3a8SLen Brown 1534f86d3a8SLen Brown return 0; 1544f86d3a8SLen Brown } 1554f86d3a8SLen Brown 156*e978aa7dSDeepthi Dharwar /** 157*e978aa7dSDeepthi Dharwar * ladder_reflect - update the correct last_state_idx 158*e978aa7dSDeepthi Dharwar * @dev: the CPU 159*e978aa7dSDeepthi Dharwar * @index: the index of actual state entered 160*e978aa7dSDeepthi Dharwar */ 161*e978aa7dSDeepthi Dharwar static void ladder_reflect(struct cpuidle_device *dev, int index) 162*e978aa7dSDeepthi Dharwar { 163*e978aa7dSDeepthi Dharwar struct ladder_device *ldev = &__get_cpu_var(ladder_devices); 164*e978aa7dSDeepthi Dharwar if (index > 0) 165*e978aa7dSDeepthi Dharwar ldev->last_state_idx = index; 166*e978aa7dSDeepthi Dharwar } 167*e978aa7dSDeepthi Dharwar 1684f86d3a8SLen Brown static struct cpuidle_governor ladder_governor = { 1694f86d3a8SLen Brown .name = "ladder", 1704f86d3a8SLen Brown .rating = 10, 1714f86d3a8SLen Brown .enable = ladder_enable_device, 1724f86d3a8SLen Brown .select = ladder_select_state, 173*e978aa7dSDeepthi Dharwar .reflect = ladder_reflect, 1744f86d3a8SLen Brown .owner = THIS_MODULE, 1754f86d3a8SLen Brown }; 1764f86d3a8SLen Brown 1774f86d3a8SLen Brown /** 1784f86d3a8SLen Brown * init_ladder - initializes the governor 1794f86d3a8SLen Brown */ 1804f86d3a8SLen Brown static int __init init_ladder(void) 1814f86d3a8SLen Brown { 1824f86d3a8SLen Brown return cpuidle_register_governor(&ladder_governor); 1834f86d3a8SLen Brown } 1844f86d3a8SLen Brown 1854f86d3a8SLen Brown /** 1864f86d3a8SLen Brown * exit_ladder - exits the governor 1874f86d3a8SLen Brown */ 1884f86d3a8SLen Brown static void __exit exit_ladder(void) 1894f86d3a8SLen Brown { 1904f86d3a8SLen Brown cpuidle_unregister_governor(&ladder_governor); 1914f86d3a8SLen Brown } 1924f86d3a8SLen Brown 1934f86d3a8SLen Brown MODULE_LICENSE("GPL"); 1944f86d3a8SLen Brown module_init(init_ladder); 1954f86d3a8SLen Brown module_exit(exit_ladder); 196