haltpoll.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * haltpoll.c - haltpoll idle governor
  4. *
  5. * Copyright 2019 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2. See
  8. * the COPYING file in the top-level directory.
  9. *
  10. * Authors: Marcelo Tosatti <[email protected]>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/cpuidle.h>
  14. #include <linux/time.h>
  15. #include <linux/ktime.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/tick.h>
  18. #include <linux/sched.h>
  19. #include <linux/module.h>
  20. #include <linux/kvm_para.h>
  21. #include <trace/events/power.h>
  22. static unsigned int guest_halt_poll_ns __read_mostly = 200000;
  23. module_param(guest_halt_poll_ns, uint, 0644);
  24. /* division factor to shrink halt_poll_ns */
  25. static unsigned int guest_halt_poll_shrink __read_mostly = 2;
  26. module_param(guest_halt_poll_shrink, uint, 0644);
  27. /* multiplication factor to grow per-cpu poll_limit_ns */
  28. static unsigned int guest_halt_poll_grow __read_mostly = 2;
  29. module_param(guest_halt_poll_grow, uint, 0644);
  30. /* value in us to start growing per-cpu halt_poll_ns */
  31. static unsigned int guest_halt_poll_grow_start __read_mostly = 50000;
  32. module_param(guest_halt_poll_grow_start, uint, 0644);
  33. /* allow shrinking guest halt poll */
  34. static bool guest_halt_poll_allow_shrink __read_mostly = true;
  35. module_param(guest_halt_poll_allow_shrink, bool, 0644);
  36. /**
  37. * haltpoll_select - selects the next idle state to enter
  38. * @drv: cpuidle driver containing state data
  39. * @dev: the CPU
  40. * @stop_tick: indication on whether or not to stop the tick
  41. */
  42. static int haltpoll_select(struct cpuidle_driver *drv,
  43. struct cpuidle_device *dev,
  44. bool *stop_tick)
  45. {
  46. s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
  47. if (!drv->state_count || latency_req == 0) {
  48. *stop_tick = false;
  49. return 0;
  50. }
  51. if (dev->poll_limit_ns == 0)
  52. return 1;
  53. /* Last state was poll? */
  54. if (dev->last_state_idx == 0) {
  55. /* Halt if no event occurred on poll window */
  56. if (dev->poll_time_limit == true)
  57. return 1;
  58. *stop_tick = false;
  59. /* Otherwise, poll again */
  60. return 0;
  61. }
  62. *stop_tick = false;
  63. /* Last state was halt: poll */
  64. return 0;
  65. }
  66. static void adjust_poll_limit(struct cpuidle_device *dev, u64 block_ns)
  67. {
  68. unsigned int val;
  69. /* Grow cpu_halt_poll_us if
  70. * cpu_halt_poll_us < block_ns < guest_halt_poll_us
  71. */
  72. if (block_ns > dev->poll_limit_ns && block_ns <= guest_halt_poll_ns) {
  73. val = dev->poll_limit_ns * guest_halt_poll_grow;
  74. if (val < guest_halt_poll_grow_start)
  75. val = guest_halt_poll_grow_start;
  76. if (val > guest_halt_poll_ns)
  77. val = guest_halt_poll_ns;
  78. trace_guest_halt_poll_ns_grow(val, dev->poll_limit_ns);
  79. dev->poll_limit_ns = val;
  80. } else if (block_ns > guest_halt_poll_ns &&
  81. guest_halt_poll_allow_shrink) {
  82. unsigned int shrink = guest_halt_poll_shrink;
  83. val = dev->poll_limit_ns;
  84. if (shrink == 0)
  85. val = 0;
  86. else
  87. val /= shrink;
  88. trace_guest_halt_poll_ns_shrink(val, dev->poll_limit_ns);
  89. dev->poll_limit_ns = val;
  90. }
  91. }
  92. /**
  93. * haltpoll_reflect - update variables and update poll time
  94. * @dev: the CPU
  95. * @index: the index of actual entered state
  96. */
  97. static void haltpoll_reflect(struct cpuidle_device *dev, int index)
  98. {
  99. dev->last_state_idx = index;
  100. if (index != 0)
  101. adjust_poll_limit(dev, dev->last_residency_ns);
  102. }
  103. /**
  104. * haltpoll_enable_device - scans a CPU's states and does setup
  105. * @drv: cpuidle driver
  106. * @dev: the CPU
  107. */
  108. static int haltpoll_enable_device(struct cpuidle_driver *drv,
  109. struct cpuidle_device *dev)
  110. {
  111. dev->poll_limit_ns = 0;
  112. return 0;
  113. }
  114. static struct cpuidle_governor haltpoll_governor = {
  115. .name = "haltpoll",
  116. .rating = 9,
  117. .enable = haltpoll_enable_device,
  118. .select = haltpoll_select,
  119. .reflect = haltpoll_reflect,
  120. };
  121. static int __init init_haltpoll(void)
  122. {
  123. if (kvm_para_available())
  124. return cpuidle_register_governor(&haltpoll_governor);
  125. return 0;
  126. }
  127. postcore_initcall(init_haltpoll);