xref: /linux/drivers/cpuidle/governors/ladder.c (revision dc2251bf98c66db3f4e055b751968f0871037ae4)
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>
204f86d3a8SLen Brown 
214f86d3a8SLen Brown #include <asm/io.h>
227c0f6ba6SLinus Torvalds #include <linux/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
6346bcfad7SDeepthi Dharwar  * @drv: cpuidle driver
644f86d3a8SLen Brown  * @dev: the CPU
654f86d3a8SLen Brown  */
6646bcfad7SDeepthi Dharwar static int ladder_select_state(struct cpuidle_driver *drv,
6746bcfad7SDeepthi Dharwar 				struct cpuidle_device *dev)
684f86d3a8SLen Brown {
69229b6863SChristoph Lameter 	struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
704f86d3a8SLen Brown 	struct ladder_device_state *last_state;
714f86d3a8SLen Brown 	int last_residency, last_idx = ldev->last_state_idx;
72*dc2251bfSRafael J. Wysocki 	int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
73ed77134bSMark Gross 	int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
744f86d3a8SLen Brown 
75a2bd9202Svenkatesh.pallipadi@intel.com 	/* Special case when user has set very strict latency requirement */
76a2bd9202Svenkatesh.pallipadi@intel.com 	if (unlikely(latency_req == 0)) {
77a2bd9202Svenkatesh.pallipadi@intel.com 		ladder_do_selection(ldev, last_idx, 0);
78a2bd9202Svenkatesh.pallipadi@intel.com 		return 0;
79a2bd9202Svenkatesh.pallipadi@intel.com 	}
80a2bd9202Svenkatesh.pallipadi@intel.com 
814f86d3a8SLen Brown 	last_state = &ldev->states[last_idx];
824f86d3a8SLen Brown 
83b73026b9SLen Brown 	last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency;
844f86d3a8SLen Brown 
854f86d3a8SLen Brown 	/* consider promotion */
8646bcfad7SDeepthi Dharwar 	if (last_idx < drv->state_count - 1 &&
8766804c13SRafael J. Wysocki 	    !drv->states[last_idx + 1].disabled &&
8862d6ae88SCarsten Emde 	    !dev->states_usage[last_idx + 1].disable &&
894f86d3a8SLen Brown 	    last_residency > last_state->threshold.promotion_time &&
9046bcfad7SDeepthi Dharwar 	    drv->states[last_idx + 1].exit_latency <= latency_req) {
914f86d3a8SLen Brown 		last_state->stats.promotion_count++;
924f86d3a8SLen Brown 		last_state->stats.demotion_count = 0;
934f86d3a8SLen Brown 		if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
944f86d3a8SLen Brown 			ladder_do_selection(ldev, last_idx, last_idx + 1);
954f86d3a8SLen Brown 			return last_idx + 1;
964f86d3a8SLen Brown 		}
974f86d3a8SLen Brown 	}
984f86d3a8SLen Brown 
994f86d3a8SLen Brown 	/* consider demotion */
100*dc2251bfSRafael J. Wysocki 	if (last_idx > first_idx &&
10166804c13SRafael J. Wysocki 	    (drv->states[last_idx].disabled ||
10266804c13SRafael J. Wysocki 	    dev->states_usage[last_idx].disable ||
10362d6ae88SCarsten Emde 	    drv->states[last_idx].exit_latency > latency_req)) {
10406d9e908Svenkatesh.pallipadi@intel.com 		int i;
10506d9e908Svenkatesh.pallipadi@intel.com 
106*dc2251bfSRafael J. Wysocki 		for (i = last_idx - 1; i > first_idx; i--) {
10746bcfad7SDeepthi 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 
114*dc2251bfSRafael J. Wysocki 	if (last_idx > first_idx &&
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
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;
137*dc2251bfSRafael 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 
142*dc2251bfSRafael J. Wysocki 	ldev->last_state_idx = first_idx;
1434f86d3a8SLen Brown 
144*dc2251bfSRafael 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)
1554f86d3a8SLen Brown 			lstate->threshold.promotion_time = state->exit_latency;
156*dc2251bfSRafael J. Wysocki 		if (i > first_idx)
1574f86d3a8SLen Brown 			lstate->threshold.demotion_time = state->exit_latency;
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 {
170229b6863SChristoph Lameter 	struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
171e978aa7dSDeepthi Dharwar 	if (index > 0)
172e978aa7dSDeepthi Dharwar 		ldev->last_state_idx = index;
173e978aa7dSDeepthi Dharwar }
174e978aa7dSDeepthi Dharwar 
1754f86d3a8SLen Brown static struct cpuidle_governor ladder_governor = {
1764f86d3a8SLen Brown 	.name =		"ladder",
1774f86d3a8SLen Brown 	.rating =	10,
1784f86d3a8SLen Brown 	.enable =	ladder_enable_device,
1794f86d3a8SLen Brown 	.select =	ladder_select_state,
180e978aa7dSDeepthi Dharwar 	.reflect =	ladder_reflect,
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 {
18866a5f6b6SJean Delvare 	/*
18966a5f6b6SJean Delvare 	 * When NO_HZ is disabled, or when booting with nohz=off, the ladder
19066a5f6b6SJean Delvare 	 * governor is better so give it a higher rating than the menu
19166a5f6b6SJean Delvare 	 * governor.
19266a5f6b6SJean Delvare 	 */
19366a5f6b6SJean Delvare 	if (!tick_nohz_enabled)
19466a5f6b6SJean Delvare 		ladder_governor.rating = 25;
19566a5f6b6SJean Delvare 
1964f86d3a8SLen Brown 	return cpuidle_register_governor(&ladder_governor);
1974f86d3a8SLen Brown }
1984f86d3a8SLen Brown 
199137b944eSDaniel Lezcano postcore_initcall(init_ladder);
200