blob: f7e83613ae94abf925003feff989e21b4f1127a8 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +02002/*
3 * poll_state.c - Polling idle state
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +02004 */
5
6#include <linux/cpuidle.h>
7#include <linux/sched.h>
Rafael J. Wysockia37b9692018-03-14 15:08:57 +01008#include <linux/sched/clock.h>
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +02009#include <linux/sched/idle.h>
10
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020011#define POLL_IDLE_RELAX_COUNT 200
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010012
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020013static int __cpuidle poll_idle(struct cpuidle_device *dev,
14 struct cpuidle_driver *drv, int index)
15{
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010016 u64 time_start = local_clock();
17
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020018 dev->poll_time_limit = false;
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;
Marcelo Tosatti259231a2019-07-03 20:51:26 -030023 u64 limit;
Rafael J. Wysocki800fb342018-12-03 13:32:53 +010024
Marcelo Tosatti259231a2019-07-03 20:51:26 -030025 limit = cpuidle_poll_time(drv, dev);
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020026
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010027 while (!need_resched()) {
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020028 cpu_relax();
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020029 if (loop_count++ < POLL_IDLE_RELAX_COUNT)
30 continue;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010031
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020032 loop_count = 0;
Rafael J. Wysocki01bad1c2018-10-02 23:50:30 +020033 if (local_clock() - time_start > limit) {
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020034 dev->poll_time_limit = true;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010035 break;
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020036 }
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010037 }
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020038 }
39 current_clr_polling();
40
41 return index;
42}
43
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020044void cpuidle_poll_state_init(struct cpuidle_driver *drv)
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020045{
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;
Rafael J. Wysockic1d51f62019-11-07 15:25:12 +010052 state->exit_latency_ns = 0;
53 state->target_residency_ns = 0;
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020054 state->power_usage = -1;
55 state->enter = poll_idle;
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020056 state->flags = CPUIDLE_FLAG_POLLING;
57}
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020058EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);