ladder.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * ladder.c - the residency ladder algorithm
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
  7. *
  8. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  9. * Shaohua Li <shaohua.li@intel.com>
  10. * Adam Belay <abelay@novell.com>
  11. *
  12. * This code is licenced under the GPL.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/cpuidle.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/tick.h>
  18. #include <asm/io.h>
  19. #include <linux/uaccess.h>
  20. #define PROMOTION_COUNT 4
  21. #define DEMOTION_COUNT 1
  22. struct ladder_device_state {
  23. struct {
  24. u32 promotion_count;
  25. u32 demotion_count;
  26. u32 promotion_time;
  27. u32 demotion_time;
  28. } threshold;
  29. struct {
  30. int promotion_count;
  31. int demotion_count;
  32. } stats;
  33. };
  34. struct ladder_device {
  35. struct ladder_device_state states[CPUIDLE_STATE_MAX];
  36. int last_state_idx;
  37. };
  38. static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
  39. /**
  40. * ladder_do_selection - prepares private data for a state change
  41. * @ldev: the ladder device
  42. * @old_idx: the current state index
  43. * @new_idx: the new target state index
  44. */
  45. static inline void ladder_do_selection(struct ladder_device *ldev,
  46. int old_idx, int new_idx)
  47. {
  48. ldev->states[old_idx].stats.promotion_count = 0;
  49. ldev->states[old_idx].stats.demotion_count = 0;
  50. ldev->last_state_idx = new_idx;
  51. }
  52. /**
  53. * ladder_select_state - selects the next state to enter
  54. * @drv: cpuidle driver
  55. * @dev: the CPU
  56. * @dummy: not used
  57. */
  58. static int ladder_select_state(struct cpuidle_driver *drv,
  59. struct cpuidle_device *dev, bool *dummy)
  60. {
  61. struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
  62. struct ladder_device_state *last_state;
  63. int last_residency, last_idx = ldev->last_state_idx;
  64. int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
  65. int latency_req = cpuidle_governor_latency_req(dev->cpu);
  66. /* Special case when user has set very strict latency requirement */
  67. if (unlikely(latency_req == 0)) {
  68. ladder_do_selection(ldev, last_idx, 0);
  69. return 0;
  70. }
  71. last_state = &ldev->states[last_idx];
  72. last_residency = cpuidle_get_last_residency(dev) - drv->states[last_idx].exit_latency;
  73. /* consider promotion */
  74. if (last_idx < drv->state_count - 1 &&
  75. !drv->states[last_idx + 1].disabled &&
  76. !dev->states_usage[last_idx + 1].disable &&
  77. last_residency > last_state->threshold.promotion_time &&
  78. drv->states[last_idx + 1].exit_latency <= latency_req) {
  79. last_state->stats.promotion_count++;
  80. last_state->stats.demotion_count = 0;
  81. if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
  82. ladder_do_selection(ldev, last_idx, last_idx + 1);
  83. return last_idx + 1;
  84. }
  85. }
  86. /* consider demotion */
  87. if (last_idx > first_idx &&
  88. (drv->states[last_idx].disabled ||
  89. dev->states_usage[last_idx].disable ||
  90. drv->states[last_idx].exit_latency > latency_req)) {
  91. int i;
  92. for (i = last_idx - 1; i > first_idx; i--) {
  93. if (drv->states[i].exit_latency <= latency_req)
  94. break;
  95. }
  96. ladder_do_selection(ldev, last_idx, i);
  97. return i;
  98. }
  99. if (last_idx > first_idx &&
  100. last_residency < last_state->threshold.demotion_time) {
  101. last_state->stats.demotion_count++;
  102. last_state->stats.promotion_count = 0;
  103. if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
  104. ladder_do_selection(ldev, last_idx, last_idx - 1);
  105. return last_idx - 1;
  106. }
  107. }
  108. /* otherwise remain at the current state */
  109. return last_idx;
  110. }
  111. /**
  112. * ladder_enable_device - setup for the governor
  113. * @drv: cpuidle driver
  114. * @dev: the CPU
  115. */
  116. static int ladder_enable_device(struct cpuidle_driver *drv,
  117. struct cpuidle_device *dev)
  118. {
  119. int i;
  120. int first_idx = drv->states[0].flags & CPUIDLE_FLAG_POLLING ? 1 : 0;
  121. struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
  122. struct ladder_device_state *lstate;
  123. struct cpuidle_state *state;
  124. ldev->last_state_idx = first_idx;
  125. for (i = first_idx; i < drv->state_count; i++) {
  126. state = &drv->states[i];
  127. lstate = &ldev->states[i];
  128. lstate->stats.promotion_count = 0;
  129. lstate->stats.demotion_count = 0;
  130. lstate->threshold.promotion_count = PROMOTION_COUNT;
  131. lstate->threshold.demotion_count = DEMOTION_COUNT;
  132. if (i < drv->state_count - 1)
  133. lstate->threshold.promotion_time = state->exit_latency;
  134. if (i > first_idx)
  135. lstate->threshold.demotion_time = state->exit_latency;
  136. }
  137. return 0;
  138. }
  139. /**
  140. * ladder_reflect - update the correct last_state_idx
  141. * @dev: the CPU
  142. * @index: the index of actual state entered
  143. */
  144. static void ladder_reflect(struct cpuidle_device *dev, int index)
  145. {
  146. struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
  147. if (index > 0)
  148. ldev->last_state_idx = index;
  149. }
  150. static struct cpuidle_governor ladder_governor = {
  151. .name = "ladder",
  152. .rating = 10,
  153. .enable = ladder_enable_device,
  154. .select = ladder_select_state,
  155. .reflect = ladder_reflect,
  156. };
  157. /**
  158. * init_ladder - initializes the governor
  159. */
  160. static int __init init_ladder(void)
  161. {
  162. /*
  163. * When NO_HZ is disabled, or when booting with nohz=off, the ladder
  164. * governor is better so give it a higher rating than the menu
  165. * governor.
  166. */
  167. if (!tick_nohz_enabled)
  168. ladder_governor.rating = 25;
  169. return cpuidle_register_governor(&ladder_governor);
  170. }
  171. postcore_initcall(init_ladder);