Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 2 | /* |
| 3 | * poll_state.c - Polling idle state |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/cpuidle.h> |
| 7 | #include <linux/sched.h> |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 8 | #include <linux/sched/clock.h> |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 9 | #include <linux/sched/idle.h> |
| 10 | |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 11 | #define POLL_IDLE_RELAX_COUNT 200 |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 12 | |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 13 | static int __cpuidle poll_idle(struct cpuidle_device *dev, |
| 14 | struct cpuidle_driver *drv, int index) |
| 15 | { |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 16 | u64 time_start = local_clock(); |
| 17 | |
Rafael J. Wysocki | 5f26bdc | 2018-10-02 23:42:02 +0200 | [diff] [blame] | 18 | dev->poll_time_limit = false; |
| 19 | |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 20 | local_irq_enable(); |
| 21 | if (!current_set_polling_and_test()) { |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 22 | unsigned int loop_count = 0; |
Marcelo Tosatti | 259231a | 2019-07-03 20:51:26 -0300 | [diff] [blame] | 23 | u64 limit; |
Rafael J. Wysocki | 800fb34 | 2018-12-03 13:32:53 +0100 | [diff] [blame] | 24 | |
Marcelo Tosatti | 259231a | 2019-07-03 20:51:26 -0300 | [diff] [blame] | 25 | limit = cpuidle_poll_time(drv, dev); |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 26 | |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 27 | while (!need_resched()) { |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 28 | cpu_relax(); |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 29 | if (loop_count++ < POLL_IDLE_RELAX_COUNT) |
| 30 | continue; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 31 | |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 32 | loop_count = 0; |
Rafael J. Wysocki | 01bad1c | 2018-10-02 23:50:30 +0200 | [diff] [blame] | 33 | if (local_clock() - time_start > limit) { |
Rafael J. Wysocki | 5f26bdc | 2018-10-02 23:42:02 +0200 | [diff] [blame] | 34 | dev->poll_time_limit = true; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 35 | break; |
Rafael J. Wysocki | 5f26bdc | 2018-10-02 23:42:02 +0200 | [diff] [blame] | 36 | } |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 37 | } |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 38 | } |
| 39 | current_clr_polling(); |
| 40 | |
| 41 | return index; |
| 42 | } |
| 43 | |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 44 | void cpuidle_poll_state_init(struct cpuidle_driver *drv) |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 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; |
Rafael J. Wysocki | c1d51f6 | 2019-11-07 15:25:12 +0100 | [diff] [blame] | 52 | state->exit_latency_ns = 0; |
| 53 | state->target_residency_ns = 0; |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 54 | state->power_usage = -1; |
| 55 | state->enter = poll_idle; |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 56 | state->flags = CPUIDLE_FLAG_POLLING; |
| 57 | } |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(cpuidle_poll_state_init); |