blob: 02b9315a9e9680e8b077968931fa410c23e44b0f [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;
Doug Smythies16179712019-01-30 08:28:22 -080023 u64 limit = TICK_NSEC;
Rafael J. Wysocki800fb342018-12-03 13:32:53 +010024 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 }
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020033
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010034 while (!need_resched()) {
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020035 cpu_relax();
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020036 if (loop_count++ < POLL_IDLE_RELAX_COUNT)
37 continue;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010038
Rafael J. Wysocki4dc23752018-03-27 23:58:45 +020039 loop_count = 0;
Rafael J. Wysocki01bad1c2018-10-02 23:50:30 +020040 if (local_clock() - time_start > limit) {
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020041 dev->poll_time_limit = true;
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010042 break;
Rafael J. Wysocki5f26bdc2018-10-02 23:42:02 +020043 }
Rafael J. Wysockia37b9692018-03-14 15:08:57 +010044 }
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020045 }
46 current_clr_polling();
47
48 return index;
49}
50
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020051void cpuidle_poll_state_init(struct cpuidle_driver *drv)
Rafael J. Wysocki34c2f652017-08-23 23:21:07 +020052{
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}
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +020064EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);