poll_state.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 = local_clock();
  14. dev->poll_time_limit = false;
  15. local_irq_enable();
  16. if (!current_set_polling_and_test()) {
  17. unsigned int loop_count = 0;
  18. u64 limit;
  19. limit = cpuidle_poll_time(drv, dev);
  20. while (!need_resched()) {
  21. cpu_relax();
  22. if (loop_count++ < POLL_IDLE_RELAX_COUNT)
  23. continue;
  24. loop_count = 0;
  25. if (local_clock() - time_start > limit) {
  26. dev->poll_time_limit = true;
  27. break;
  28. }
  29. }
  30. }
  31. current_clr_polling();
  32. return index;
  33. }
  34. void cpuidle_poll_state_init(struct cpuidle_driver *drv)
  35. {
  36. struct cpuidle_state *state = &drv->states[0];
  37. snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
  38. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  39. state->exit_latency = 0;
  40. state->target_residency = 0;
  41. state->exit_latency_ns = 0;
  42. state->target_residency_ns = 0;
  43. state->power_usage = -1;
  44. state->enter = poll_idle;
  45. state->flags = CPUIDLE_FLAG_POLLING;
  46. }
  47. EXPORT_SYMBOL_GPL(cpuidle_poll_state_init);