1 /*
2  * poll_state.c - Polling idle state
3  *
4  * This file is released under the GPLv2.
5  */
6 
7 #include <linux/cpuidle.h>
8 #include <linux/sched.h>
9 #include <linux/sched/clock.h>
10 #include <linux/sched/idle.h>
11 
12 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
13 #define POLL_IDLE_RELAX_COUNT	200
14 
poll_idle(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)15 static int __cpuidle poll_idle(struct cpuidle_device *dev,
16 			       struct cpuidle_driver *drv, int index)
17 {
18 	u64 time_start = local_clock();
19 
20 	dev->poll_time_limit = false;
21 
22 	local_irq_enable();
23 	if (!current_set_polling_and_test()) {
24 		unsigned int loop_count = 0;
25 
26 		while (!need_resched()) {
27 			cpu_relax();
28 			if (loop_count++ < POLL_IDLE_RELAX_COUNT)
29 				continue;
30 
31 			loop_count = 0;
32 			if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT) {
33 				dev->poll_time_limit = true;
34 				break;
35 			}
36 		}
37 	}
38 	current_clr_polling();
39 
40 	return index;
41 }
42 
cpuidle_poll_state_init(struct cpuidle_driver * drv)43 void cpuidle_poll_state_init(struct cpuidle_driver *drv)
44 {
45 	struct cpuidle_state *state = &drv->states[0];
46 
47 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
48 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
49 	state->exit_latency = 0;
50 	state->target_residency = 0;
51 	state->power_usage = -1;
52 	state->enter = poll_idle;
53 	state->disabled = false;
54 	state->flags = CPUIDLE_FLAG_POLLING;
55 }
56 EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);
57