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