blob: c8fa5f41dfc4cfdc5c0a33d091f748f747ad1b31 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * poll_state.c - Polling idle state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#include <linux/cpuidle.h>
7#include <linux/sched.h>
8#include <linux/sched/clock.h>
9#include <linux/sched/idle.h>
10
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#define POLL_IDLE_RELAX_COUNT 200
12
13static int __cpuidle poll_idle(struct cpuidle_device *dev,
14 struct cpuidle_driver *drv, int index)
15{
16 u64 time_start = local_clock();
17
David Brazdil0f672f62019-12-10 10:32:29 +000018 dev->poll_time_limit = false;
19
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020 local_irq_enable();
21 if (!current_set_polling_and_test()) {
22 unsigned int loop_count = 0;
David Brazdil0f672f62019-12-10 10:32:29 +000023 u64 limit;
24
25 limit = cpuidle_poll_time(drv, dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026
27 while (!need_resched()) {
28 cpu_relax();
29 if (loop_count++ < POLL_IDLE_RELAX_COUNT)
30 continue;
31
32 loop_count = 0;
David Brazdil0f672f62019-12-10 10:32:29 +000033 if (local_clock() - time_start > limit) {
34 dev->poll_time_limit = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 break;
David Brazdil0f672f62019-12-10 10:32:29 +000036 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 }
38 }
39 current_clr_polling();
40
41 return index;
42}
43
44void cpuidle_poll_state_init(struct cpuidle_driver *drv)
45{
46 struct cpuidle_state *state = &drv->states[0];
47
48 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
49 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
50 state->exit_latency = 0;
51 state->target_residency = 0;
52 state->power_usage = -1;
53 state->enter = poll_idle;
54 state->disabled = false;
55 state->flags = CPUIDLE_FLAG_POLLING;
56}
57EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);