blob: 3f86d23c592ec0cdce4b6e8019a02256f4146d78 [file] [log] [blame]
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +02001/*
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>
Rafael J. Wysockia37b9692018-03-14 15:08:57 +01009#include <linux/sched/clock.h>
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020010#include <linux/sched/idle.h>
11
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010012#define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16)
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020013#define POLL_IDLE_RELAX_COUNT 200
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010014
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020015static int __cpuidle poll_idle(struct cpuidle_device *dev,
16 struct cpuidle_driver *drv, int index)
17{
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010018 u64 time_start = local_clock();
19
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020020 local_irq_enable();
21 if (!current_set_polling_and_test()) {
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020022 unsigned int loop_count = 0;
23
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010024 while (!need_resched()) {
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020025 cpu_relax();
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020026 if (loop_count++ < POLL_IDLE_RELAX_COUNT)
27 continue;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010028
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020029 loop_count = 0;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010030 if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT)
31 break;
32 }
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020033 }
34 current_clr_polling();
35
36 return index;
37}
38
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020039void cpuidle_poll_state_init(struct cpuidle_driver *drv)
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020040{
41 struct cpuidle_state *state = &drv->states[0];
42
43 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
44 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
45 state->exit_latency = 0;
46 state->target_residency = 0;
47 state->power_usage = -1;
48 state->enter = poll_idle;
49 state->disabled = false;
50 state->flags = CPUIDLE_FLAG_POLLING;
51}
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020052EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);