Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * poll_state.c - Polling idle state |
| 3 | * |
| 4 | * This file is released under the GPLv2. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/cpuidle.h> |
| 8 | #include <linux/sched.h> |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 9 | #include <linux/sched/clock.h> |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 10 | #include <linux/sched/idle.h> |
| 11 | |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 12 | #define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16) |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 13 | #define POLL_IDLE_RELAX_COUNT 200 |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 14 | |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 15 | static int __cpuidle poll_idle(struct cpuidle_device *dev, |
| 16 | struct cpuidle_driver *drv, int index) |
| 17 | { |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 18 | u64 time_start = local_clock(); |
| 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; |
| 23 | |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 24 | while (!need_resched()) { |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 25 | cpu_relax(); |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 26 | if (loop_count++ < POLL_IDLE_RELAX_COUNT) |
| 27 | continue; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 28 | |
Rafael J. Wysocki | 4dc2375 | 2018-03-27 23:58:45 +0200 | [diff] [blame] | 29 | loop_count = 0; |
Rafael J. Wysocki | a37b969 | 2018-03-14 15:08:57 +0100 | [diff] [blame] | 30 | if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT) |
| 31 | break; |
| 32 | } |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 33 | } |
| 34 | current_clr_polling(); |
| 35 | |
| 36 | return index; |
| 37 | } |
| 38 | |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 39 | void cpuidle_poll_state_init(struct cpuidle_driver *drv) |
Rafael J. Wysocki | 34c2f65 | 2017-08-23 23:21:07 +0200 | [diff] [blame] | 40 | { |
| 41 | struct cpuidle_state *state = &drv->states[0]; |
| 42 | |
| 43 | snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); |
| 44 | snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); |
| 45 | state->exit_latency = 0; |
| 46 | state->target_residency = 0; |
| 47 | state->power_usage = -1; |
| 48 | state->enter = poll_idle; |
| 49 | state->disabled = false; |
| 50 | state->flags = CPUIDLE_FLAG_POLLING; |
| 51 | } |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 52 | EXPORT_SYMBOL_GPL(cpuidle_poll_state_init); |