xref: /openbmc/linux/drivers/cpuidle/poll_state.c (revision ed1666f6)
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_RELAX_COUNT	200
13 
14 static int __cpuidle poll_idle(struct cpuidle_device *dev,
15 			       struct cpuidle_driver *drv, int index)
16 {
17 	u64 time_start = local_clock();
18 
19 	dev->poll_time_limit = false;
20 
21 	local_irq_enable();
22 	if (!current_set_polling_and_test()) {
23 		unsigned int loop_count = 0;
24 		u64 limit = TICK_NSEC;
25 		int i;
26 
27 		for (i = 1; i < drv->state_count; i++) {
28 			if (drv->states[i].disabled || dev->states_usage[i].disable)
29 				continue;
30 
31 			limit = (u64)drv->states[i].target_residency * NSEC_PER_USEC;
32 			break;
33 		}
34 
35 		while (!need_resched()) {
36 			cpu_relax();
37 			if (loop_count++ < POLL_IDLE_RELAX_COUNT)
38 				continue;
39 
40 			loop_count = 0;
41 			if (local_clock() - time_start > limit) {
42 				dev->poll_time_limit = true;
43 				break;
44 			}
45 		}
46 	}
47 	current_clr_polling();
48 
49 	return index;
50 }
51 
52 void cpuidle_poll_state_init(struct cpuidle_driver *drv)
53 {
54 	struct cpuidle_state *state = &drv->states[0];
55 
56 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
57 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
58 	state->exit_latency = 0;
59 	state->target_residency = 0;
60 	state->power_usage = -1;
61 	state->enter = poll_idle;
62 	state->disabled = false;
63 	state->flags = CPUIDLE_FLAG_POLLING;
64 }
65 EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);
66