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/export.h> 8 #include <linux/irqflags.h> 9 #include <linux/sched.h> 10 #include <linux/sched/clock.h> 11 #include <linux/sched/idle.h> 12 #include <linux/sprintf.h> 13 #include <linux/types.h> 14 15 #define POLL_IDLE_RELAX_COUNT 200 16 17 static int __cpuidle poll_idle(struct cpuidle_device *dev, 18 struct cpuidle_driver *drv, int index) 19 { 20 u64 time_start; 21 22 time_start = local_clock_noinstr(); 23 24 dev->poll_time_limit = false; 25 26 raw_local_irq_enable(); 27 if (!current_set_polling_and_test()) { 28 unsigned int loop_count = 0; 29 u64 limit; 30 31 limit = cpuidle_poll_time(drv, dev); 32 33 while (!need_resched()) { 34 cpu_relax(); 35 if (loop_count++ < POLL_IDLE_RELAX_COUNT) 36 continue; 37 38 loop_count = 0; 39 if (local_clock_noinstr() - time_start > limit) { 40 dev->poll_time_limit = true; 41 break; 42 } 43 } 44 } 45 raw_local_irq_disable(); 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->exit_latency_ns = 0; 61 state->target_residency_ns = 0; 62 state->power_usage = -1; 63 state->enter = poll_idle; 64 state->flags = CPUIDLE_FLAG_POLLING; 65 } 66 EXPORT_SYMBOL_GPL(cpuidle_poll_state_init); 67