poll_state.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * poll_state.c - Polling idle state
  4. */
  5. #include <linux/cpuidle.h>
  6. #include <linux/sched.h>
  7. #include <linux/sched/clock.h>
  8. #include <linux/sched/idle.h>
  9. #define POLL_IDLE_RELAX_COUNT 200
  10. static int __cpuidle poll_idle(struct cpuidle_device *dev,
  11. struct cpuidle_driver *drv, int index)
  12. {
  13. u64 time_start;
  14. time_start = local_clock_noinstr();
  15. dev->poll_time_limit = false;
  16. raw_local_irq_enable();
  17. if (!current_set_polling_and_test()) {
  18. unsigned int loop_count = 0;
  19. u64 limit;
  20. limit = cpuidle_poll_time(drv, dev);
  21. while (!need_resched()) {
  22. cpu_relax();
  23. if (loop_count++ < POLL_IDLE_RELAX_COUNT)
  24. continue;
  25. loop_count = 0;
  26. if (local_clock_noinstr() - time_start > limit) {
  27. dev->poll_time_limit = true;
  28. break;
  29. }
  30. }
  31. }
  32. raw_local_irq_disable();
  33. current_clr_polling();
  34. return index;
  35. }
  36. void cpuidle_poll_state_init(struct cpuidle_driver *drv)
  37. {
  38. struct cpuidle_state *state = &drv->states[0];
  39. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  40. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  41. state->exit_latency = 0;
  42. state->target_residency = 0;
  43. state->exit_latency_ns = 0;
  44. state->target_residency_ns = 0;
  45. state->power_usage = -1;
  46. state->enter = poll_idle;
  47. state->flags = CPUIDLE_FLAG_POLLING;
  48. }
  49. EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);