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; |
Doug Smythies | 1617971 | 2019-01-30 08:28:22 -0800 | [diff] [blame] | 23 | u64 limit = TICK_NSEC; |
Rafael J. Wysocki | 800fb34 | 2018-12-03 13:32:53 +0100 | [diff] [blame] | 24 | 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. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 33 | |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 34 | while (!need_resched()) { |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 35 | cpu_relax(); |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 36 | if (loop_count++ < POLL_IDLE_RELAX_COUNT) |
| 37 | continue; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 38 | |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 39 | loop_count = 0; |
Rafael J. Wysocki | 01bad1c | 2018-10-02 23:50:30 +0200 | [diff] [blame] | 40 | if (local_clock() - time_start > limit) { |
Rafael J. Wysocki | 5f26bdc | 2018-10-02 23:42:02 +0200 | [diff] [blame] | 41 | dev->poll_time_limit = true; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 42 | break; |
Rafael J. Wysocki | 5f26bdc | 2018-10-02 23:42:02 +0200 | [diff] [blame] | 43 | } |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 44 | } |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 45 | } |
| 46 | current_clr_polling(); |
| 47 | |
| 48 | return index; |
| 49 | } |
| 50 | |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 51 | void cpuidle_poll_state_init(struct cpuidle_driver *drv) |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 52 | { |
| 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. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(cpuidle_poll_state_init); |